|
| 1 | +name: Auto Release on package.json version bump |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [v3] |
| 6 | + paths: |
| 7 | + - package.json |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + release: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + with: |
| 20 | + fetch-depth: 0 |
| 21 | + |
| 22 | + - name: Read current version |
| 23 | + id: current |
| 24 | + run: | |
| 25 | + VERSION=$(node -p "require('./package.json').version") |
| 26 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 27 | +
|
| 28 | + - name: Read previous version (from previous commit) |
| 29 | + id: previous |
| 30 | + run: | |
| 31 | + PREV_VERSION=$(git show HEAD~1:package.json 2>/dev/null | node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).version" || echo "") |
| 32 | + echo "version=$PREV_VERSION" >> "$GITHUB_OUTPUT" |
| 33 | +
|
| 34 | + - name: Check if version increased |
| 35 | + id: check |
| 36 | + run: | |
| 37 | + CUR="${{ steps.current.outputs.version }}" |
| 38 | + PREV="${{ steps.previous.outputs.version }}" |
| 39 | +
|
| 40 | + echo "Current: $CUR" |
| 41 | + echo "Previous: $PREV" |
| 42 | +
|
| 43 | + # if previous doesn't exist (first commit containing package.json), skip |
| 44 | + if [ -z "$PREV" ]; then |
| 45 | + echo "should_release=false" >> "$GITHUB_OUTPUT" |
| 46 | + exit 0 |
| 47 | + fi |
| 48 | +
|
| 49 | + # Compare semver using node's semver package if available; fallback to simple inequality |
| 50 | + node -e " |
| 51 | + const cur='${CUR}'; |
| 52 | + const prev='${PREV}'; |
| 53 | + let should = cur !== prev; |
| 54 | + try { |
| 55 | + const semver = require('semver'); |
| 56 | + should = semver.gt(cur, prev); |
| 57 | + } catch (_) {} |
| 58 | + console.log('should_release=' + should); |
| 59 | + " >> "$GITHUB_OUTPUT" |
| 60 | +
|
| 61 | + - name: Stop if no bump |
| 62 | + if: steps.check.outputs.should_release != 'true' |
| 63 | + run: echo "No version increase detected." |
| 64 | + |
| 65 | + - name: Create tag + GitHub Release |
| 66 | + if: steps.check.outputs.should_release == 'true' |
| 67 | + uses: softprops/action-gh-release@v2 |
| 68 | + with: |
| 69 | + tag_name: v${{ steps.current.outputs.version }} |
| 70 | + name: v${{ steps.current.outputs.version }} |
| 71 | + generate_release_notes: true |
0 commit comments