|
1 | | -name: Build and Push Docker Image on Release |
| 1 | +name: Release and Docker Push |
2 | 2 |
|
3 | 3 | on: |
4 | | - release: |
5 | | - types: [published] |
6 | | - workflow_dispatch: |
| 4 | + push: |
| 5 | + branches: [v3] |
| 6 | + paths: |
| 7 | + - package.json |
| 8 | + workflow_dispatch: |
7 | 9 |
|
8 | 10 | env: |
9 | | - REGISTRY: ghcr.io |
10 | | - IMAGE_NAME: ${{ github.repository }} |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_NAME: ${{ github.repository }} |
11 | 13 |
|
12 | 14 | jobs: |
13 | | - build-and-push: |
14 | | - runs-on: ubuntu-latest |
15 | | - permissions: |
16 | | - contents: read |
17 | | - packages: write |
18 | | - |
19 | | - steps: |
20 | | - - name: Checkout repository |
21 | | - uses: actions/checkout@v4 |
22 | | - |
23 | | - - name: Set up Docker Buildx |
24 | | - uses: docker/setup-buildx-action@v3 |
25 | | - |
26 | | - - name: Log in to GitHub Container Registry |
27 | | - uses: docker/login-action@v3 |
28 | | - with: |
29 | | - registry: ${{ env.REGISTRY }} |
30 | | - username: ${{ github.actor }} |
31 | | - password: ${{ secrets.GITHUB_TOKEN }} |
32 | | - |
33 | | - - name: Extract metadata |
34 | | - id: meta |
35 | | - uses: docker/metadata-action@v5 |
36 | | - with: |
37 | | - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
38 | | - tags: | |
39 | | - type=semver,pattern={{version}} |
40 | | - type=semver,pattern={{major}}.{{minor}} |
41 | | - type=raw,value=latest,enable={{is_default_branch}} |
42 | | -
|
43 | | - - name: Build and push Docker image |
44 | | - uses: docker/build-push-action@v5 |
45 | | - with: |
46 | | - context: . |
47 | | - push: true |
48 | | - tags: ${{ steps.meta.outputs.tags }} |
49 | | - labels: ${{ steps.meta.outputs.labels }} |
50 | | - cache-from: type=gha |
51 | | - cache-to: type=gha,mode=max |
| 15 | + release-and-build: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + permissions: |
| 18 | + contents: write |
| 19 | + packages: write |
| 20 | + |
| 21 | + steps: |
| 22 | + - name: Checkout repository |
| 23 | + uses: actions/checkout@v4 |
| 24 | + with: |
| 25 | + fetch-depth: 0 |
| 26 | + |
| 27 | + - name: Read current version |
| 28 | + id: current |
| 29 | + run: | |
| 30 | + VERSION=$(node -p "require('./package.json').version") |
| 31 | + echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 32 | +
|
| 33 | + - name: Read previous version |
| 34 | + id: previous |
| 35 | + run: | |
| 36 | + PREV_VERSION=$(git show HEAD~1:package.json 2>/dev/null | node -p "JSON.parse(require('fs').readFileSync(0,'utf8')).version" || echo "") |
| 37 | + echo "version=$PREV_VERSION" >> "$GITHUB_OUTPUT" |
| 38 | +
|
| 39 | + - name: Check if version increased |
| 40 | + id: check |
| 41 | + run: | |
| 42 | + CUR="${{ steps.current.outputs.version }}" |
| 43 | + PREV="${{ steps.previous.outputs.version }}" |
| 44 | + |
| 45 | + if [ -z "$PREV" ]; then |
| 46 | + echo "should_release=false" >> "$GITHUB_OUTPUT" |
| 47 | + exit 0 |
| 48 | + fi |
| 49 | +
|
| 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 | + process.stdout.write('should_release=' + should); |
| 59 | + " >> "$GITHUB_OUTPUT" |
| 60 | +
|
| 61 | + - name: Create GitHub Release |
| 62 | + if: steps.check.outputs.should_release == 'true' |
| 63 | + uses: softprops/action-gh-release@v2 |
| 64 | + with: |
| 65 | + tag_name: v${{ steps.current.outputs.version }} |
| 66 | + name: v${{ steps.current.outputs.version }} |
| 67 | + generate_release_notes: false |
| 68 | + |
| 69 | + - name: Set up Docker Buildx |
| 70 | + if: steps.check.outputs.should_release == 'true' |
| 71 | + uses: docker/setup-buildx-action@v3 |
| 72 | + |
| 73 | + - name: Log in to GitHub Container Registry |
| 74 | + if: steps.check.outputs.should_release == 'true' |
| 75 | + uses: docker/login-action@v3 |
| 76 | + with: |
| 77 | + registry: ${{ env.REGISTRY }} |
| 78 | + username: ${{ github.actor }} |
| 79 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 80 | + |
| 81 | + - name: Extract metadata |
| 82 | + if: steps.check.outputs.should_release == 'true' |
| 83 | + id: meta |
| 84 | + uses: docker/metadata-action@v5 |
| 85 | + with: |
| 86 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 87 | + tags: | |
| 88 | + type=raw,value=${{ steps.current.outputs.version }} |
| 89 | + type=raw,value=latest |
| 90 | +
|
| 91 | + - name: Build and push Docker image |
| 92 | + if: steps.check.outputs.should_release == 'true' |
| 93 | + uses: docker/build-push-action@v5 |
| 94 | + with: |
| 95 | + context: . |
| 96 | + push: true |
| 97 | + tags: ${{ steps.meta.outputs.tags }} |
| 98 | + labels: ${{ steps.meta.outputs.labels }} |
| 99 | + cache-from: type=gha |
| 100 | + cache-to: type=gha,mode=max |
0 commit comments