|
| 1 | +name: Version Bump |
| 2 | +on: |
| 3 | + workflow_dispatch: |
| 4 | + |
| 5 | +permissions: |
| 6 | + contents: write |
| 7 | + pull-requests: write |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: ${{ github.workflow }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +defaults: |
| 14 | + run: |
| 15 | + shell: bash |
| 16 | + |
| 17 | +jobs: |
| 18 | + version-bump: |
| 19 | + name: Version Bump |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - name: Git Checkout |
| 23 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # 6.0.2 |
| 24 | + with: |
| 25 | + submodules: true |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Git Config |
| 29 | + env: |
| 30 | + GH_TOKEN: ${{ github.token }} |
| 31 | + run: | |
| 32 | + USER_NAME=$(gh api "/users/${{ github.actor }}" --jq '.name // "${{ github.actor }}"') |
| 33 | + USER_EMAIL="${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com" |
| 34 | + git config --global user.email "$USER_EMAIL" |
| 35 | + git config --global user.name "$USER_NAME" |
| 36 | +
|
| 37 | + - name: Determine branch name |
| 38 | + id: branch |
| 39 | + run: | |
| 40 | + BASE_BRANCH="chore/release/version-bump/$(date -u +'%Y-%m-%d')" |
| 41 | +
|
| 42 | + # Check if base branch already exists on remote |
| 43 | + if git ls-remote --exit-code --heads origin "$BASE_BRANCH" > /dev/null 2>&1; then |
| 44 | + # Base branch exists, find the next available suffix |
| 45 | + N=2 |
| 46 | + while git ls-remote --exit-code --heads origin "${BASE_BRANCH}__${N}" > /dev/null 2>&1; do |
| 47 | + N=$((N + 1)) |
| 48 | + done |
| 49 | + BRANCH="${BASE_BRANCH}__${N}" |
| 50 | + else |
| 51 | + BRANCH="$BASE_BRANCH" |
| 52 | + fi |
| 53 | +
|
| 54 | + echo "branch=$BRANCH" >> "$GITHUB_OUTPUT" |
| 55 | + echo "Using branch: $BRANCH" |
| 56 | +
|
| 57 | + - name: Create branch |
| 58 | + run: | |
| 59 | + git checkout -b "${{ steps.branch.outputs.branch }}" |
| 60 | +
|
| 61 | + - name: Setup Flutter |
| 62 | + uses: subosito/flutter-action@44ac965b96f18d999802d4b807e3256d5a3f9fa1 # v2.16.0 |
| 63 | + with: |
| 64 | + channel: stable |
| 65 | + |
| 66 | + - name: Setup aft |
| 67 | + run: dart pub global activate -spath packages/aft |
| 68 | + |
| 69 | + - name: Verify clean working tree |
| 70 | + run: | |
| 71 | + if [ -n "$(git status --porcelain)" ]; then |
| 72 | + echo "::error::Working tree is not clean. Unexpected changes detected before running aft version-bump." |
| 73 | + git status |
| 74 | + exit 1 |
| 75 | + fi |
| 76 | +
|
| 77 | + - name: Run aft version-bump |
| 78 | + run: | |
| 79 | + # Unset GitHub Actions refs which are empty strings for workflow_dispatch |
| 80 | + # and would cause aft to pass "" to git, crashing with "cannot be an empty string" |
| 81 | + unset GITHUB_BASE_REF |
| 82 | + unset GITHUB_HEAD_REF |
| 83 | + aft version-bump |
| 84 | +
|
| 85 | + - name: Collect version changes and prepare PR body |
| 86 | + run: | |
| 87 | + SUMMARY="" |
| 88 | + # Find all pubspec.yaml files that were modified |
| 89 | + for pubspec in $(git diff --name-only -- '**/pubspec.yaml'); do |
| 90 | + # Get the package name from the pubspec |
| 91 | + PKG_NAME=$(grep -m1 '^name:' "$pubspec" | sed 's/name: *//') |
| 92 | + # Get the old version from git |
| 93 | + OLD_VERSION=$(git show HEAD:"$pubspec" 2>/dev/null | grep -m1 '^version:' | sed 's/version: *//') |
| 94 | + # Get the new version from the working tree |
| 95 | + NEW_VERSION=$(grep -m1 '^version:' "$pubspec" | sed 's/version: *//') |
| 96 | + if [ -n "$OLD_VERSION" ] && [ -n "$NEW_VERSION" ] && [ "$OLD_VERSION" != "$NEW_VERSION" ]; then |
| 97 | + SUMMARY="${SUMMARY}- \`${PKG_NAME}\`: \`${OLD_VERSION}\` → \`${NEW_VERSION}\`"$'\n' |
| 98 | + fi |
| 99 | + done |
| 100 | +
|
| 101 | + if [ -z "$SUMMARY" ]; then |
| 102 | + SUMMARY="No version changes detected." |
| 103 | + fi |
| 104 | +
|
| 105 | + # Write PR body to a file to avoid bash interpreting backticks |
| 106 | + cat > /tmp/pr_body.md <<'HEREDOC_END' |
| 107 | + Automated version bump created by the **Version Bump** workflow. |
| 108 | + HEREDOC_END |
| 109 | +
|
| 110 | + { |
| 111 | + echo "" |
| 112 | + echo "**Branch:** \`${{ steps.branch.outputs.branch }}\`" |
| 113 | + echo "**Triggered by:** @${{ github.actor }}" |
| 114 | + echo "" |
| 115 | + echo "### Affected Packages" |
| 116 | + echo "" |
| 117 | + echo "$SUMMARY" |
| 118 | + } >> /tmp/pr_body.md |
| 119 | +
|
| 120 | + - name: Commit and push changes |
| 121 | + run: | |
| 122 | + CHANGED_FILES=$(git status --porcelain) |
| 123 | +
|
| 124 | + if [ -z "$CHANGED_FILES" ]; then |
| 125 | + echo "::warning::No files were changed by aft version-bump. Nothing to commit." |
| 126 | + exit 0 |
| 127 | + fi |
| 128 | +
|
| 129 | + echo "Files changed by aft version-bump:" |
| 130 | + echo "$CHANGED_FILES" |
| 131 | +
|
| 132 | + git add -A |
| 133 | + git commit -m "chore(version): Version bump" |
| 134 | + git push origin "${{ steps.branch.outputs.branch }}" |
| 135 | +
|
| 136 | + - name: Create Pull Request |
| 137 | + env: |
| 138 | + GH_TOKEN: ${{ github.token }} |
| 139 | + run: | |
| 140 | + gh pr create \ |
| 141 | + --title "chore(version): Version bump" \ |
| 142 | + --body-file /tmp/pr_body.md \ |
| 143 | + --base "${{ github.ref_name }}" \ |
| 144 | + --head "${{ steps.branch.outputs.branch }}" \ |
| 145 | + --assignee "${{ github.actor }}" |
0 commit comments