|
| 1 | +name: Auto-bump Version on Merge |
| 2 | + |
| 3 | +# This triggers ONLY when code is pushed directly to master |
| 4 | +# (which includes when a Pull Request is merged into it). |
| 5 | +on: |
| 6 | + push: |
| 7 | + branches: |
| 8 | + - master # If your default branch is 'main', change this to 'main' |
| 9 | + |
| 10 | +jobs: |
| 11 | + bump-version: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + contents: write # Required so the bot can push the new commit to your repo |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Calculate and Update Version |
| 22 | + run: | |
| 23 | + # 1. Extract current version from the DESCRIPTION file |
| 24 | + CURRENT_VERSION=$(grep -i "^Version:" DESCRIPTION | awk '{print $2}') |
| 25 | + |
| 26 | + # SECURITY CHECK: Ensure it strictly matches a version number format (e.g., 0.17.0 or 0.17.0.9000) |
| 27 | + if [[ ! "$CURRENT_VERSION" =~ ^[0-9]+(\.[0-9]+)+$ ]]; then |
| 28 | + echo "❌ Error: Invalid version format in DESCRIPTION: '$CURRENT_VERSION'" |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | + |
| 32 | + # 2. Split the version string by periods into an array |
| 33 | + IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION" |
| 34 | + |
| 35 | + # 3. Increment the very last number by 1 |
| 36 | + LAST_INDEX=$((${#VERSION_PARTS[@]} - 1)) |
| 37 | + VERSION_PARTS[$LAST_INDEX]=$((VERSION_PARTS[$LAST_INDEX] + 1)) |
| 38 | + |
| 39 | + # 4. Stitch the version string back together |
| 40 | + NEW_VERSION=$(IFS='.'; echo "${VERSION_PARTS[*]}") |
| 41 | + |
| 42 | + echo "Bumping version from $CURRENT_VERSION to $NEW_VERSION" |
| 43 | + |
| 44 | + # 5. Safely replace the old version with the new version in the file |
| 45 | + sed -i "s/^Version: [0-9.]\+/Version: $NEW_VERSION/i" DESCRIPTION |
| 46 | + |
| 47 | + # 6. Securely pass the new version to the next step for the commit message |
| 48 | + cat <<EOF >> "$GITHUB_ENV" |
| 49 | + NEW_VERSION=$NEW_VERSION |
| 50 | + EOF |
| 51 | +
|
| 52 | + - name: Commit and Push |
| 53 | + run: | |
| 54 | + # Configure Git to act as the official GitHub Actions bot |
| 55 | + git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 56 | + git config --local user.name "github-actions[bot]" |
| 57 | + |
| 58 | + # Stage the changed file |
| 59 | + git add DESCRIPTION |
| 60 | + |
| 61 | + # Check if there are actually changes to commit (prevents errors if version didn't change) |
| 62 | + if ! git diff-index --quiet HEAD; then |
| 63 | + # The [skip ci] flag is CRITICAL. It tells GitHub NOT to trigger |
| 64 | + # another workflow run from this automated commit, preventing infinite loops. |
| 65 | + git commit -m "chore: auto-bump version to ${{ env.NEW_VERSION }} [skip ci]" |
| 66 | + git push |
| 67 | + echo "✅ Successfully bumped version and pushed to master." |
| 68 | + else |
| 69 | + echo "No changes needed." |
| 70 | + fi |
0 commit comments