Mandresy Randrianarinjaka 494919b58f
Some checks failed
versioning-and-changelog / versioning_and_changelog (pull_request) Failing after 19s
chore: Add gitea versioning workflows
2025-02-04 16:20:53 +01:00

209 lines
7.4 KiB
YAML

name: versioning-and-changelog
# Trigger the workflow when a pull request is closed and targets the master branch.
on:
pull_request:
types:
- closed
branches:
- master
jobs:
versioning_and_changelog:
# Run this job only if the pull request has been merged.
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the full repository, including all commit history.
- name: Checkout full repository
uses: actions/checkout@v4
with:
fetch-depth: 0
# Step 2: Set up Flutter environment with the stable channel.
- name: Set up Flutter
uses: subosito/flutter-action@v2
with:
channel: stable
flutter-version-file: pubspec.yaml
# Step 3: Extract the commits from the merged pull request.
- name: Find commits from merged pull request
id: get_commits
run: |
COMMITS=$(git log HEAD^..HEAD --oneline --no-merges)
if [ -z "$COMMITS" ]; then
echo "No commits found in the merged PR."
exit 0
fi
echo "Commits from merged PR:"
echo "$COMMITS"
echo "COMMITS<<EOF" >> "$GITHUB_ENV"
echo "$COMMITS" >> "$GITHUB_ENV"
echo "EOF" >> "$GITHUB_ENV"
# Step 4: Extract the current version from pubspec.yaml.
- name: Extract current version
id: versioning
run: |
PUBSPEC_FILE="pubspec.yaml"
if [ ! -f "$PUBSPEC_FILE" ]; then
echo "Error: $PUBSPEC_FILE not found."
exit 1
fi
CURRENT_VERSION=$(grep 'version:' $PUBSPEC_FILE | awk -F ': ' '{print $2}')
VERSION=$(echo "$CURRENT_VERSION" | cut -d'+' -f1)
BUILD=$(echo "$CURRENT_VERSION" | cut -d'+' -f2)
echo "Current version: $VERSION+$BUILD"
echo "CURRENT_VERSION=$CURRENT_VERSION" >> "$GITHUB_ENV"
echo "VERSION=$VERSION" >> "$GITHUB_ENV"
echo "BUILD=$BUILD" >> "$GITHUB_ENV"
# Step 5: Determine the version increment type (major, minor, or patch) based on commit messages.
- name: Determine increment type from commits
id: increment_type
run: |
COMMITS="${{ env.COMMITS }}"
INCREMENT_TYPE="patch"
if echo "$COMMITS" | grep -iq "breaking:"; then
INCREMENT_TYPE="major"
elif echo "$COMMITS" | grep -iq "feat:"; then
INCREMENT_TYPE="minor"
elif echo "$COMMITS" | grep -iq "fix:"; then
INCREMENT_TYPE="patch"
elif echo "$COMMITS" | grep -iq "refactor:"; then
INCREMENT_TYPE="patch"
elif echo "$COMMITS" | grep -iq "style:"; then
INCREMENT_TYPE="patch"
elif echo "$COMMITS" | grep -iq "chore:"; then
INCREMENT_TYPE="patch"
else
INCREMENT_TYPE="patch"
fi
echo "INCREMENT_TYPE=$INCREMENT_TYPE" >> "$GITHUB_ENV"
# Step 6: Increment the version number and update pubspec.yaml.
- name: Increment version and update pubspec.yaml
id: increment_version
run: |
increment_version() {
local version=$1
local index=$2
IFS='.' read -r -a parts <<< "$version"
if [[ ${#parts[@]} -le $index ]]; then
echo "Error: Invalid version format or index."
exit 1
fi
parts[$index]=$((parts[$index] + 1))
for ((i = index + 1; i < ${#parts[@]}; i++)); do
parts[$i]=0
done
echo "${parts[*]}" | tr ' ' '.'
}
CURRENT_VERSION="${{ env.CURRENT_VERSION }}"
VERSION="${{ env.VERSION }}"
BUILD="${{ env.BUILD }}"
INCREMENT_TYPE="${{ env.INCREMENT_TYPE }}"
NEW_BUILD=$((BUILD + 1))
echo "Incrementing version using type: $INCREMENT_TYPE"
echo "Old Version: $CURRENT_VERSION"
case $INCREMENT_TYPE in
major)
NEW_VERSION=$(increment_version "$VERSION" 0)
;;
minor)
NEW_VERSION=$(increment_version "$VERSION" 1)
;;
patch)
NEW_VERSION=$(increment_version "$VERSION" 2)
;;
*)
echo "Error: Unknown increment type: $INCREMENT_TYPE"
exit 1
;;
esac
sed -i.bak "s/^version: .*/version: $NEW_VERSION+$NEW_BUILD/" pubspec.yaml
echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_ENV"
echo "NEW_BUILD=$NEW_BUILD" >> "$GITHUB_ENV"
echo "New Version: $NEW_VERSION+$NEW_BUILD"
# Step 7: Generate a changelog based on the commits in the merged pull request.
- name: Generate Changelog for merged PR
run: |
CHANGELOG_FILE="CHANGELOG.md"
DATE=$(date +"%Y-%m-%d")
COMMITS="${{ env.COMMITS }}"
NEW_VERSION="${{ env.NEW_VERSION }}"
NEW_BUILD="${{ env.NEW_BUILD }}"
if [ ! -f "$CHANGELOG_FILE" ]; then
touch "$CHANGELOG_FILE"
echo "# Changelog" > "$CHANGELOG_FILE"
fi
TEMP_CHANGELOG="CHANGELOG_TEMP.md"
{
echo "## $NEW_VERSION+$NEW_BUILD ($DATE)"
echo ""
echo "### Breaking Changes"
BREAKING=$(echo "$COMMITS" | grep "breaking:" | sed -E 's/^([a-f0-9]+) breaking: /- (#\1) /g')
echo "${BREAKING:-No breaking changes found.}"
echo ""
echo "### Features"
FEATURES=$(echo "$COMMITS" | grep "feat:" | sed -E 's/^([a-f0-9]+) feat: /- (#\1) /g')
echo "${FEATURES:-No features found.}"
echo ""
echo "### Bug Fixes"
BUG_FIXES=$(echo "$COMMITS" | grep "fix:" | sed -E 's/^([a-f0-9]+) fix: /- (#\1) /g')
echo "${BUG_FIXES:-No bug fixes found.}"
echo ""
echo "### Refactors"
REFACTOR=$(echo "$COMMITS" | grep "refactor:" | sed -E 's/^([a-f0-9]+) refactor: /- (#\1) /g')
echo "${REFACTOR:-No refactors found.}"
echo ""
echo "### Style Changes"
STYLE=$(echo "$COMMITS" | grep "style:" | sed -E 's/^([a-f0-9]+) style: /- (#\1) /g')
echo "${STYLE:-No style changes found.}"
echo ""
echo "### Chores"
CHORES=$(echo "$COMMITS" | grep "chore:" | sed -E 's/^([a-f0-9]+) chore: /- (#\1) /g')
echo "${CHORES:-No chores found.}"
echo ""
echo "### Miscellaneous"
MISC=$(echo "$COMMITS" | grep -v -E "breaking:|feat:|fix:|refactor:|style:|chore:" | sed -E 's/^([a-f0-9]+) /- (#\1) /g')
echo "${MISC:-No miscellaneous changes found.}"
echo ""
cat "$CHANGELOG_FILE"
} > "$TEMP_CHANGELOG"
mv "$TEMP_CHANGELOG" "$CHANGELOG_FILE"
echo "Changelog updated."
# Step 8: Commit the updated version and changelog to the master branch.
- name: Commit version and changelog to master
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions"
git add pubspec.yaml CHANGELOG.md
git commit -m "chore: increment version $NEW_VERSION+$NEW_BUILD and update changelog"
git push origin master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}