|
1 | | -name: Auto Release on package.json version bump |
| 1 | +name: Release and Docker Push |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
5 | 5 | branches: [v3] |
6 | 6 | paths: |
7 | 7 | - package.json |
| 8 | + workflow_dispatch: |
8 | 9 |
|
9 | 10 | permissions: |
10 | 11 | contents: write |
| 12 | + packages: write |
| 13 | + |
| 14 | +env: |
| 15 | + REGISTRY: ghcr.io |
| 16 | + IMAGE_NAME: ${{ github.repository }} |
11 | 17 |
|
12 | 18 | jobs: |
13 | | - release: |
| 19 | + deploy: |
14 | 20 | runs-on: ubuntu-latest |
15 | | - |
16 | 21 | steps: |
17 | 22 | - name: Checkout |
18 | 23 | uses: actions/checkout@v4 |
19 | 24 | with: |
20 | 25 | fetch-depth: 0 |
21 | 26 |
|
22 | | - - name: Read current version |
23 | | - id: current |
| 27 | + - name: Read versions |
| 28 | + id: versions |
24 | 29 | run: | |
25 | | - VERSION=$(node -p "require('./package.json').version") |
26 | | - echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 30 | + set -euo pipefail |
| 31 | + CUR_VERSION=$(node -p "require('./package.json').version") |
| 32 | + echo "current=$CUR_VERSION" >> "$GITHUB_OUTPUT" |
| 33 | + echo "LOG: Current version found in package.json: $CUR_VERSION" |
27 | 34 |
|
28 | | - - name: Read previous version (from previous commit) |
29 | | - id: previous |
30 | | - run: | |
31 | 35 | 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" |
| 36 | + echo "previous=$PREV_VERSION" >> "$GITHUB_OUTPUT" |
| 37 | + if [ -z "$PREV_VERSION" ]; then |
| 38 | + echo "LOG: No previous version found in git history" |
| 39 | + else |
| 40 | + echo "LOG: Previous version found in git history: $PREV_VERSION" |
| 41 | + fi |
33 | 42 |
|
34 | | - - name: Check if version increased |
| 43 | + - name: Version comparison |
35 | 44 | id: check |
36 | 45 | run: | |
37 | | - CUR="${{ steps.current.outputs.version }}" |
38 | | - PREV="${{ steps.previous.outputs.version }}" |
| 46 | + set -euo pipefail |
| 47 | + echo "LOG: Running semantic version comparison" |
| 48 | + node - <<'NODE' >> "$GITHUB_OUTPUT" |
| 49 | + const cur = process.env.CUR; |
| 50 | + const prev = process.env.PREV; |
39 | 51 |
|
40 | | - echo "Current: $CUR" |
41 | | - echo "Previous: $PREV" |
| 52 | + function parse(v) { |
| 53 | + if (!v) return null; |
| 54 | + const m = String(v).trim().replace(/^v/, "").match(/^(\d+)\.(\d+)\.(\d+)(?:[-+].*)?$/); |
| 55 | + return m ? m.slice(1, 4).map(Number) : null; |
| 56 | + } |
42 | 57 |
|
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 |
| 58 | + function gt(a, b) { |
| 59 | + for (let i = 0; i < 3; i++) { |
| 60 | + if (a[i] !== b[i]) return a[i] > b[i]; |
| 61 | + } |
| 62 | + return false; |
| 63 | + } |
| 64 | +
|
| 65 | + const a = parse(cur); |
| 66 | + const b = parse(prev); |
| 67 | +
|
| 68 | + let should = false; |
| 69 | + if (!prev && a) { |
| 70 | + should = true; |
| 71 | + } else if (a && b) { |
| 72 | + should = gt(a, b); |
| 73 | + } |
| 74 | +
|
| 75 | + console.log(`should_release=${should}`); |
| 76 | + NODE |
| 77 | + env: |
| 78 | + CUR: ${{ steps.versions.outputs.current }} |
| 79 | + PREV: ${{ steps.versions.outputs.previous }} |
| 80 | + |
| 81 | + - name: Logging decision |
| 82 | + run: | |
| 83 | + echo "LOG: Release decision is ${{ steps.check.outputs.should_release }}" |
48 | 84 |
|
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 |
| 85 | + - name: GitHub Release |
66 | 86 | if: steps.check.outputs.should_release == 'true' |
67 | 87 | uses: softprops/action-gh-release@v2 |
68 | 88 | with: |
69 | | - tag_name: v${{ steps.current.outputs.version }} |
70 | | - name: v${{ steps.current.outputs.version }} |
| 89 | + tag_name: v${{ steps.versions.outputs.current }} |
| 90 | + name: v${{ steps.versions.outputs.current }} |
71 | 91 | generate_release_notes: true |
| 92 | + |
| 93 | + - name: Set up Docker Buildx |
| 94 | + if: steps.check.outputs.should_release == 'true' |
| 95 | + uses: docker/setup-buildx-action@v3 |
| 96 | + |
| 97 | + - name: Log in to GHCR |
| 98 | + if: steps.check.outputs.should_release == 'true' |
| 99 | + uses: docker/login-action@v3 |
| 100 | + with: |
| 101 | + registry: ${{ env.REGISTRY }} |
| 102 | + username: ${{ github.actor }} |
| 103 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 104 | + |
| 105 | + - name: Build and push Docker image |
| 106 | + if: steps.check.outputs.should_release == 'true' |
| 107 | + uses: docker/build-push-action@v5 |
| 108 | + with: |
| 109 | + context: . |
| 110 | + push: true |
| 111 | + tags: | |
| 112 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.versions.outputs.current }} |
| 113 | + ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest |
| 114 | + cache-from: type=gha |
| 115 | + cache-to: type=gha,mode=max |
0 commit comments