|
| 1 | +name: Auto Bump Version on PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + branches: |
| 7 | + - development |
| 8 | + |
| 9 | +jobs: |
| 10 | + bump-version: |
| 11 | + # Skip if commit message contains [skip ci] or [version bump] |
| 12 | + if: | |
| 13 | + !contains(github.event.head_commit.message, '[skip ci]') && |
| 14 | + !contains(github.event.head_commit.message, '[version bump]') |
| 15 | +
|
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + permissions: |
| 19 | + contents: write # Required to push to PR branch |
| 20 | + pull-requests: write # Required to comment on PR |
| 21 | + |
| 22 | + steps: |
| 23 | + - name: Checkout PR branch |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + ref: ${{ github.head_ref }} # Check out the PR branch, not merge commit |
| 27 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 28 | + fetch-depth: 0 |
| 29 | + |
| 30 | + - name: Setup Node.js |
| 31 | + uses: actions/setup-node@v4 |
| 32 | + with: |
| 33 | + node-version: "20" |
| 34 | + |
| 35 | + - name: Check if version needs bumping |
| 36 | + id: check |
| 37 | + run: | |
| 38 | + # Fetch the base branch to compare against |
| 39 | + git fetch origin ${{ github.base_ref }} |
| 40 | + BASE_SHA=$(git rev-parse origin/${{ github.base_ref }}) |
| 41 | +
|
| 42 | + echo "Comparing against base branch: ${{ github.base_ref }} ($BASE_SHA)" |
| 43 | +
|
| 44 | + # Get current date in YYYYMMDD format |
| 45 | + TODAY=$(date +%Y%m%d) |
| 46 | + echo "Today's date: $TODAY" |
| 47 | +
|
| 48 | + # Check if package.json was modified in this PR by a non-bot user |
| 49 | + CHANGED_FILES=$(git diff --name-only $BASE_SHA HEAD) |
| 50 | +
|
| 51 | + if echo "$CHANGED_FILES" | grep -qE "^package\.json$|^configure/package\.json$"; then |
| 52 | + echo "✓ Version files were modified in this PR" |
| 53 | +
|
| 54 | + # Check if the modification was made by the bot (exclude bot commits) |
| 55 | + PKG_JSON_COMMITS=$(git log --format="%an|%ae|%s" $BASE_SHA..HEAD -- package.json configure/package.json) |
| 56 | +
|
| 57 | + # Check if ALL package.json commits are from the bot |
| 58 | + NON_BOT_COMMITS=$(echo "$PKG_JSON_COMMITS" | grep -v "github-actions\[bot\]" | grep -v "\[version bump\]" || true) |
| 59 | +
|
| 60 | + if [ -z "$NON_BOT_COMMITS" ]; then |
| 61 | + echo "✓ All version changes were made by the bot, checking if date is current..." |
| 62 | +
|
| 63 | + # Get current version and extract date |
| 64 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 65 | + VERSION_DATE=$(echo $CURRENT_VERSION | grep -oE '[0-9]{8}$' || echo "") |
| 66 | +
|
| 67 | + if [ "$VERSION_DATE" == "$TODAY" ]; then |
| 68 | + echo "✓ Version date is current ($TODAY)" |
| 69 | + echo "needs_bump=false" >> $GITHUB_OUTPUT |
| 70 | + echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 71 | + else |
| 72 | + echo "✗ Version date is outdated ($VERSION_DATE vs $TODAY), will re-bump with today's date" |
| 73 | + echo "needs_bump=true" >> $GITHUB_OUTPUT |
| 74 | + fi |
| 75 | + else |
| 76 | + echo "✓ Version was manually updated by a user" |
| 77 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 78 | + echo "needs_bump=false" >> $GITHUB_OUTPUT |
| 79 | + echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT |
| 80 | + fi |
| 81 | + else |
| 82 | + echo "✗ Version files not modified, will auto-bump" |
| 83 | + echo "needs_bump=true" >> $GITHUB_OUTPUT |
| 84 | + fi |
| 85 | +
|
| 86 | + - name: Bump version |
| 87 | + if: steps.check.outputs.needs_bump == 'true' |
| 88 | + id: bump |
| 89 | + run: | |
| 90 | + # Get current date in YYYYMMDD format |
| 91 | + DATE=$(date +%Y%m%d) |
| 92 | +
|
| 93 | + # Read current version from package.json |
| 94 | + CURRENT_VERSION=$(node -p "require('./package.json').version") |
| 95 | + echo "Current version: $CURRENT_VERSION" |
| 96 | +
|
| 97 | + # Strip any existing date suffix (e.g., 4.1.1-20251022 -> 4.1.1) |
| 98 | + BASE_VERSION=$(echo $CURRENT_VERSION | sed 's/-[0-9]\{8\}$//') |
| 99 | +
|
| 100 | + # Split version into parts |
| 101 | + MAJOR=$(echo $BASE_VERSION | cut -d. -f1) |
| 102 | + MINOR=$(echo $BASE_VERSION | cut -d. -f2) |
| 103 | + PATCH=$(echo $BASE_VERSION | cut -d. -f3) |
| 104 | +
|
| 105 | + # Increment patch version |
| 106 | + PATCH=$((PATCH + 1)) |
| 107 | +
|
| 108 | + # Create new version with date suffix |
| 109 | + NEW_VERSION="$MAJOR.$MINOR.$PATCH-$DATE" |
| 110 | + echo "New version: $NEW_VERSION" |
| 111 | +
|
| 112 | + # Validate the new version format |
| 113 | + if ! echo "$NEW_VERSION" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+-[0-9]{8}$'; then |
| 114 | + echo "Error: Generated invalid version format: $NEW_VERSION" |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | +
|
| 118 | + # Update package.json |
| 119 | + node -e " |
| 120 | + const fs = require('fs'); |
| 121 | + const pkg = require('./package.json'); |
| 122 | + pkg.version = '$NEW_VERSION'; |
| 123 | + fs.writeFileSync('./package.json', JSON.stringify(pkg, null, 2) + '\n'); |
| 124 | + " |
| 125 | + echo "✓ Updated package.json" |
| 126 | +
|
| 127 | + # Update configure/package.json (with error handling) |
| 128 | + if [ -f configure/package.json ]; then |
| 129 | + node -e " |
| 130 | + const fs = require('fs'); |
| 131 | + const pkg = require('./configure/package.json'); |
| 132 | + pkg.version = '$NEW_VERSION'; |
| 133 | + fs.writeFileSync('./configure/package.json', JSON.stringify(pkg, null, 2) + '\n'); |
| 134 | + " |
| 135 | + echo "✓ Updated configure/package.json" |
| 136 | + else |
| 137 | + echo "ℹ configure/package.json not found, skipping" |
| 138 | + fi |
| 139 | +
|
| 140 | + # Export new version for commit message and PR comment |
| 141 | + echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 142 | +
|
| 143 | + - name: Commit version bump to PR branch |
| 144 | + if: steps.check.outputs.needs_bump == 'true' |
| 145 | + run: | |
| 146 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 147 | + git config --local user.name "github-actions[bot]" |
| 148 | +
|
| 149 | + git add package.json configure/package.json |
| 150 | +
|
| 151 | + # Check if there are actually changes to commit |
| 152 | + if git diff --staged --quiet; then |
| 153 | + echo "⚠ No changes to commit" |
| 154 | + exit 0 |
| 155 | + fi |
| 156 | +
|
| 157 | + git commit -m "chore: bump version to ${{ steps.bump.outputs.NEW_VERSION }} [version bump]" |
| 158 | +
|
| 159 | + echo "✓ Committed version bump" |
| 160 | +
|
| 161 | + - name: Push to PR branch |
| 162 | + if: steps.check.outputs.needs_bump == 'true' |
| 163 | + run: | |
| 164 | + # Push to the PR branch (head_ref is the source branch of the PR) |
| 165 | + git push origin HEAD:${{ github.head_ref }} |
| 166 | +
|
| 167 | + echo "✓ Pushed version bump to PR branch: ${{ github.head_ref }}" |
| 168 | +
|
| 169 | + - name: Comment on PR - Version Auto-Bumped |
| 170 | + if: steps.check.outputs.needs_bump == 'true' |
| 171 | + uses: actions/github-script@v7 |
| 172 | + with: |
| 173 | + script: | |
| 174 | + const comment = `## 🤖 Version Auto-Bumped |
| 175 | +
|
| 176 | + The version has been automatically incremented to **\`${{ steps.bump.outputs.NEW_VERSION }}\`** |
| 177 | +
|
| 178 | + This commit was added to your PR branch. When you merge this PR, the new version will be included. |
| 179 | +
|
| 180 | + --- |
| 181 | + <sub>If you want a different version, update \`package.json\` manually and push to this PR.</sub>`; |
| 182 | +
|
| 183 | + // Check for existing bot comment to avoid spam |
| 184 | + const { data: comments } = await github.rest.issues.listComments({ |
| 185 | + owner: context.repo.owner, |
| 186 | + repo: context.repo.repo, |
| 187 | + issue_number: context.issue.number, |
| 188 | + }); |
| 189 | +
|
| 190 | + const botComment = comments.find(comment => |
| 191 | + comment.user.type === 'Bot' && |
| 192 | + comment.body.includes('Version Auto-Bumped') |
| 193 | + ); |
| 194 | +
|
| 195 | + if (botComment) { |
| 196 | + // Update existing comment |
| 197 | + await github.rest.issues.updateComment({ |
| 198 | + owner: context.repo.owner, |
| 199 | + repo: context.repo.repo, |
| 200 | + comment_id: botComment.id, |
| 201 | + body: comment |
| 202 | + }); |
| 203 | + console.log('Updated existing comment'); |
| 204 | + } else { |
| 205 | + // Create new comment |
| 206 | + await github.rest.issues.createComment({ |
| 207 | + owner: context.repo.owner, |
| 208 | + repo: context.repo.repo, |
| 209 | + issue_number: context.issue.number, |
| 210 | + body: comment |
| 211 | + }); |
| 212 | + console.log('Created new comment'); |
| 213 | + } |
| 214 | +
|
| 215 | + - name: Comment on PR - Version Already Updated |
| 216 | + if: steps.check.outputs.needs_bump == 'false' |
| 217 | + uses: actions/github-script@v7 |
| 218 | + with: |
| 219 | + script: | |
| 220 | + const comment = `## ✅ Version Already Updated |
| 221 | +
|
| 222 | + This PR includes a manual version update to **\`${{ steps.check.outputs.current_version }}\`** |
| 223 | +
|
| 224 | + No automatic version bump needed.`; |
| 225 | +
|
| 226 | + const { data: comments } = await github.rest.issues.listComments({ |
| 227 | + owner: context.repo.owner, |
| 228 | + repo: context.repo.repo, |
| 229 | + issue_number: context.issue.number, |
| 230 | + }); |
| 231 | +
|
| 232 | + const botComment = comments.find(comment => |
| 233 | + comment.user.type === 'Bot' && |
| 234 | + (comment.body.includes('Version Already Updated') || comment.body.includes('Version Auto-Bumped')) |
| 235 | + ); |
| 236 | +
|
| 237 | + if (botComment) { |
| 238 | + await github.rest.issues.updateComment({ |
| 239 | + owner: context.repo.owner, |
| 240 | + repo: context.repo.repo, |
| 241 | + comment_id: botComment.id, |
| 242 | + body: comment |
| 243 | + }); |
| 244 | + } else { |
| 245 | + await github.rest.issues.createComment({ |
| 246 | + owner: context.repo.owner, |
| 247 | + repo: context.repo.repo, |
| 248 | + issue_number: context.issue.number, |
| 249 | + body: comment |
| 250 | + }); |
| 251 | + } |
| 252 | +
|
| 253 | + - name: Summary |
| 254 | + if: always() |
| 255 | + run: | |
| 256 | + if [ "${{ steps.check.outputs.needs_bump }}" == "true" ]; then |
| 257 | + echo "### 🤖 Version Auto-Bumped" >> $GITHUB_STEP_SUMMARY |
| 258 | + echo "Automatically bumped to version **${{ steps.bump.outputs.NEW_VERSION }}**" >> $GITHUB_STEP_SUMMARY |
| 259 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 260 | + echo "The version bump has been committed to the PR branch." >> $GITHUB_STEP_SUMMARY |
| 261 | + else |
| 262 | + echo "### ✅ Version Already Updated" >> $GITHUB_STEP_SUMMARY |
| 263 | + echo "PR already includes version **${{ steps.check.outputs.current_version }}**" >> $GITHUB_STEP_SUMMARY |
| 264 | + fi |
0 commit comments