chore: Fix versioning workflows

This commit is contained in:
Mandresy Randrianarinjaka 2025-02-05 09:16:35 +03:00
parent 814142d789
commit 73fdf8e669

View File

@ -1,74 +1,46 @@
name: versioning-and-changelog name: versioning-and-changelog
# Trigger the workflow when a pull request is closed and targets the master branch.
on: on:
pull_request: pull_request:
types: types:
- closed - merged # Gitea utilise 'merged' et non 'closed'
branches: branches:
- master - master
sdfq
jobs: jobs:
versioning_and_changelog: 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 runs-on: ubuntu-latest
steps: steps:
# Step 1: Checkout the full repository, including all commit history. - name: Checkout repository
- name: Checkout full repository
uses: actions/checkout@v4 uses: actions/checkout@v4
with: with:
fetch-depth: 0 fetch-depth: 0
# Step 2: Set up Flutter environment with the stable channel.
- name: Set up Flutter - name: Set up Flutter
uses: subosito/flutter-action@v2 uses: subosito/flutter-action@v2
with: with:
channel: stable channel: stable
flutter-version-file: pubspec.yaml flutter-version-file: pubspec.yaml
# Step 3: Extract the commits from the merged pull request.
- name: Find commits from merged pull request - name: Find commits from merged pull request
id: get_commits id: get_commits
run: | run: |
COMMITS=$(git log HEAD^..HEAD --oneline --no-merges) COMMITS=$(git log HEAD^..HEAD --oneline --no-merges)
echo "COMMITS=$COMMITS" >> "$GITHUB_ENV"
if [ -z "$COMMITS" ]; then - name: Extract current version from pubspec.yaml
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 id: versioning
run: | run: |
PUBSPEC_FILE="pubspec.yaml" CURRENT_VERSION=$(grep 'version:' pubspec.yaml | awk -F ': ' '{print $2}')
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) VERSION=$(echo "$CURRENT_VERSION" | cut -d'+' -f1)
BUILD=$(echo "$CURRENT_VERSION" | cut -d'+' -f2) BUILD=$(echo "$CURRENT_VERSION" | cut -d'+' -f2)
echo "Current version: $VERSION+$BUILD"
echo "CURRENT_VERSION=$CURRENT_VERSION" >> "$GITHUB_ENV" echo "CURRENT_VERSION=$CURRENT_VERSION" >> "$GITHUB_ENV"
echo "VERSION=$VERSION" >> "$GITHUB_ENV" echo "VERSION=$VERSION" >> "$GITHUB_ENV"
echo "BUILD=$BUILD" >> "$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
- name: Determine increment type from commits
id: increment_type id: increment_type
run: | run: |
COMMITS="${{ env.COMMITS }}" COMMITS="${{ env.COMMITS }}"
@ -80,129 +52,67 @@ jobs:
INCREMENT_TYPE="minor" INCREMENT_TYPE="minor"
elif echo "$COMMITS" | grep -iq "fix:"; then elif echo "$COMMITS" | grep -iq "fix:"; then
INCREMENT_TYPE="patch" 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" echo "INCREMENT_TYPE=$INCREMENT_TYPE" >> "$GITHUB_ENV"
# Step 6: Increment the version number and update pubspec.yaml. - name: Increment version in pubspec.yaml
- name: Increment version and update pubspec.yaml
id: increment_version id: increment_version
run: | run: |
increment_version() { increment_version() {
local version=$1 local version=$1
local index=$2 local index=$2
IFS='.' read -r -a parts <<< "$version" 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)) parts[$index]=$((parts[$index] + 1))
for ((i = index + 1; i < ${#parts[@]}; i++)); do for ((i = index + 1; i < ${#parts[@]}; i++)); do parts[$i]=0; done
parts[$i]=0
done
echo "${parts[*]}" | tr ' ' '.' echo "${parts[*]}" | tr ' ' '.'
} }
CURRENT_VERSION="${{ env.CURRENT_VERSION }}"
VERSION="${{ env.VERSION }}" VERSION="${{ env.VERSION }}"
BUILD="${{ env.BUILD }}" BUILD="${{ env.BUILD }}"
INCREMENT_TYPE="${{ env.INCREMENT_TYPE }}" INCREMENT_TYPE="${{ env.INCREMENT_TYPE }}"
NEW_BUILD=$((BUILD + 1)) NEW_BUILD=$((BUILD + 1))
echo "Incrementing version using type: $INCREMENT_TYPE"
echo "Old Version: $CURRENT_VERSION"
case $INCREMENT_TYPE in case $INCREMENT_TYPE in
major) major) NEW_VERSION=$(increment_version "$VERSION" 0) ;;
NEW_VERSION=$(increment_version "$VERSION" 0) minor) NEW_VERSION=$(increment_version "$VERSION" 1) ;;
;; patch) NEW_VERSION=$(increment_version "$VERSION" 2) ;;
minor)
NEW_VERSION=$(increment_version "$VERSION" 1)
;;
patch)
NEW_VERSION=$(increment_version "$VERSION" 2)
;;
*)
echo "Error: Unknown increment type: $INCREMENT_TYPE"
exit 1
;;
esac esac
sed -i.bak "s/^version: .*/version: $NEW_VERSION+$NEW_BUILD/" pubspec.yaml sed -i "s/^version: .*/version: $NEW_VERSION+$NEW_BUILD/" pubspec.yaml
echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_ENV" echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_ENV"
echo "NEW_BUILD=$NEW_BUILD" >> "$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
- name: Generate Changelog for merged PR
run: | run: |
CHANGELOG_FILE="CHANGELOG.md"
DATE=$(date +"%Y-%m-%d") DATE=$(date +"%Y-%m-%d")
COMMITS="${{ env.COMMITS }}" COMMITS="${{ env.COMMITS }}"
NEW_VERSION="${{ env.NEW_VERSION }}" NEW_VERSION="${{ env.NEW_VERSION }}"
NEW_BUILD="${{ env.NEW_BUILD }}" NEW_BUILD="${{ env.NEW_BUILD }}"
CHANGELOG_FILE="CHANGELOG.md"
if [ ! -f "$CHANGELOG_FILE" ]; then [ ! -f "$CHANGELOG_FILE" ] && echo "# Changelog" > "$CHANGELOG_FILE"
touch "$CHANGELOG_FILE"
echo "# Changelog" > "$CHANGELOG_FILE"
fi
TEMP_CHANGELOG="CHANGELOG_TEMP.md" TEMP_CHANGELOG="CHANGELOG_TEMP.md"
{ {
echo "## $NEW_VERSION+$NEW_BUILD ($DATE)" echo "## $NEW_VERSION+$NEW_BUILD ($DATE)"
echo "" echo ""
echo "### Breaking Changes" echo "### Features"
BREAKING=$(echo "$COMMITS" | grep "breaking:" | sed -E 's/^([a-f0-9]+) breaking: /- (#\1) /g') echo "$COMMITS" | grep "feat:" || echo "No new features"
echo "${BREAKING:-No breaking changes found.}" echo ""
echo "" echo "### Bug Fixes"
echo "### Features" echo "$COMMITS" | grep "fix:" || echo "No bug fixes"
FEATURES=$(echo "$COMMITS" | grep "feat:" | sed -E 's/^([a-f0-9]+) feat: /- (#\1) /g') echo ""
echo "${FEATURES:-No features found.}" cat "$CHANGELOG_FILE"
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" } > "$TEMP_CHANGELOG"
mv "$TEMP_CHANGELOG" "$CHANGELOG_FILE" mv "$TEMP_CHANGELOG" "$CHANGELOG_FILE"
echo "Changelog updated."
# Step 8: Commit the updated version and changelog to the master branch. - name: Commit and Push Changes
- name: Commit version and changelog to master
run: | run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com" git config --global user.email "gitea-actions@bot.com"
git config --global user.name "github-actions" git config --global user.name "Gitea Actions"
git add pubspec.yaml CHANGELOG.md git add pubspec.yaml CHANGELOG.md
git commit -m "chore: increment version $NEW_VERSION+$NEW_BUILD and update changelog" git commit -m "chore: bump version to $NEW_VERSION+$NEW_BUILD and update changelog"
git push origin master git push https://${{ secrets.GITEA_ACCESS_TOKEN }}@gitea.example.com/owner/repo.git master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}