|
1 | 1 | name: Auto-bump Version on Merge |
2 | 2 |
|
3 | | -# This triggers ONLY when code is pushed directly to master |
4 | | -# (which includes when a Pull Request is merged into it). |
5 | 3 | on: |
6 | 4 | push: |
7 | 5 | branches: |
8 | | - - master # If your default branch is 'main', change this to 'main' |
| 6 | + - master |
9 | 7 |
|
10 | 8 | jobs: |
11 | 9 | bump-version: |
12 | 10 | runs-on: ubuntu-latest |
13 | 11 | permissions: |
14 | | - contents: write # Required so the bot can push the new commit to your repo |
| 12 | + contents: write |
15 | 13 | steps: |
16 | 14 | - name: Checkout repository |
17 | 15 | uses: actions/checkout@v4 |
18 | 16 | with: |
19 | | - fetch-depth: 0 |
| 17 | + fetch-depth: 2 |
20 | 18 |
|
21 | 19 | - name: Calculate and Update Version |
| 20 | + id: calc |
22 | 21 | run: | |
23 | | - # 1. Extract current version from the DESCRIPTION file |
24 | | - CURRENT_VERSION=$(grep -i "^Version:" DESCRIPTION | awk '{print $2}') |
| 22 | + # Default to not skipping |
| 23 | + echo "SKIP_BUMP=false" >> "$GITHUB_ENV" |
25 | 24 | |
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'" |
| 25 | + # 1. Extract version from current commit |
| 26 | + RAW_VERSION=$(grep -i "^Version:" DESCRIPTION | awk '{print $2}' | tr -d '\r') |
| 27 | + |
| 28 | + # 2. STRICT FORMAT CHECK: Must be exactly x.y.z (numbers only) |
| 29 | + if [[ ! "$RAW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then |
| 30 | + echo "❌ ERROR: Version '$RAW_VERSION' is not in strictly x.y.z format." |
| 31 | + echo "Failing workflow to prevent malformed versioning." |
29 | 32 | exit 1 |
30 | 33 | fi |
31 | 34 | |
32 | | - # 2. Split the version string by periods into an array |
33 | | - IFS='.' read -r -a VERSION_PARTS <<< "$CURRENT_VERSION" |
| 35 | + # 3. Get version from previous commit safely |
| 36 | + OLD_ON_BRANCH=$(git show HEAD^:DESCRIPTION 2>/dev/null | grep -i "^Version:" | awk '{print $2}' | tr -d '\r' || true) |
34 | 37 | |
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 | + # 4. Do nothing if manual change is detected |
| 39 | + if [ "$RAW_VERSION" != "$OLD_ON_BRANCH" ] && [ -n "$OLD_ON_BRANCH" ]; then |
| 40 | + echo "Manual version bump detected ($OLD_ON_BRANCH -> $RAW_VERSION). Doing nothing." |
| 41 | + echo "SKIP_BUMP=true" >> "$GITHUB_ENV" |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +
|
| 45 | + # 5. z+1 |
| 46 | + IFS='.' read -r -a PARTS <<< "$RAW_VERSION" |
| 47 | + X=${PARTS[0]} |
| 48 | + Y=${PARTS[1]} |
| 49 | + Z=${PARTS[2]} |
38 | 50 | |
39 | | - # 4. Stitch the version string back together |
40 | | - NEW_VERSION=$(IFS='.'; echo "${VERSION_PARTS[*]}") |
| 51 | + Z=$((Z + 1)) |
| 52 | + NEW_VERSION="$X.$Y.$Z" |
41 | 53 | |
42 | | - echo "Bumping version from $CURRENT_VERSION to $NEW_VERSION" |
| 54 | + echo "Bumping version from $RAW_VERSION to $NEW_VERSION" |
43 | 55 | |
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 |
| 56 | + # 6. Write to file (safely overwrites whatever comes after "Version: ") |
| 57 | + sed -i "s/^Version:.*/Version: $NEW_VERSION/i" DESCRIPTION |
46 | 58 | |
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 |
| 59 | + # Export for next step |
| 60 | + echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_ENV" |
51 | 61 |
|
52 | 62 | - name: Commit and Push |
| 63 | + if: env.SKIP_BUMP == 'false' |
53 | 64 | run: | |
54 | | - # Configure Git to act as the official GitHub Actions bot |
55 | 65 | git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com" |
56 | 66 | git config --local user.name "github-actions[bot]" |
57 | 67 | |
58 | | - # Stage the changed file |
59 | 68 | git add DESCRIPTION |
60 | 69 | |
61 | | - # Check if there are actually changes to commit (prevents errors if version didn't change) |
62 | 70 | 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 | 71 | git commit -m "chore: auto-bump version to ${{ env.NEW_VERSION }} [skip ci]" |
66 | 72 | git push |
67 | | - echo "✅ Successfully bumped version and pushed to master." |
| 73 | + echo "✅ Successfully bumped version to ${{ env.NEW_VERSION }}." |
68 | 74 | else |
69 | 75 | echo "No changes needed." |
70 | 76 | fi |
0 commit comments