Skip to content

Commit 598aa21

Browse files
Update auto-bump-version.yml
1 parent 38cf2c4 commit 598aa21

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed
Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,76 @@
11
name: Auto-bump Version on Merge
22

3-
# This triggers ONLY when code is pushed directly to master
4-
# (which includes when a Pull Request is merged into it).
53
on:
64
push:
75
branches:
8-
- master # If your default branch is 'main', change this to 'main'
6+
- master
97

108
jobs:
119
bump-version:
1210
runs-on: ubuntu-latest
1311
permissions:
14-
contents: write # Required so the bot can push the new commit to your repo
12+
contents: write
1513
steps:
1614
- name: Checkout repository
1715
uses: actions/checkout@v4
1816
with:
19-
fetch-depth: 0
17+
fetch-depth: 2
2018

2119
- name: Calculate and Update Version
20+
id: calc
2221
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"
2524
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."
2932
exit 1
3033
fi
3134
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)
3437
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]}
3850
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"
4153
42-
echo "Bumping version from $CURRENT_VERSION to $NEW_VERSION"
54+
echo "Bumping version from $RAW_VERSION to $NEW_VERSION"
4355
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
4658
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"
5161
5262
- name: Commit and Push
63+
if: env.SKIP_BUMP == 'false'
5364
run: |
54-
# Configure Git to act as the official GitHub Actions bot
5565
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
5666
git config --local user.name "github-actions[bot]"
5767
58-
# Stage the changed file
5968
git add DESCRIPTION
6069
61-
# Check if there are actually changes to commit (prevents errors if version didn't change)
6270
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.
6571
git commit -m "chore: auto-bump version to ${{ env.NEW_VERSION }} [skip ci]"
6672
git push
67-
echo "✅ Successfully bumped version and pushed to master."
73+
echo "✅ Successfully bumped version to ${{ env.NEW_VERSION }}."
6874
else
6975
echo "No changes needed."
7076
fi

0 commit comments

Comments
 (0)