|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: # Allows manual triggering |
| 5 | + |
| 6 | +jobs: |
| 7 | + release: |
| 8 | + name: Release to npm and GitHub |
| 9 | + runs-on: ubuntu-latest |
| 10 | + # Permissions needed for semantic-release to commit/tag/release |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + issues: write |
| 14 | + pull-requests: write |
| 15 | + # id-token: write # Needed for OIDC trusted publishing (if not using NPM_TOKEN) |
| 16 | + outputs: |
| 17 | + # Output whether a new release was published |
| 18 | + new_release_published: ${{ steps.semantic.outputs.new_release_published }} |
| 19 | + new_release_version: ${{ steps.semantic.outputs.new_release_version }} |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + # Need fetch-depth: 0 for semantic-release to analyze all relevant commits |
| 23 | + # and commit package.json/CHANGELOG.md changes |
| 24 | + uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Set up Node.js |
| 29 | + uses: actions/setup-node@v4 |
| 30 | + with: |
| 31 | + node-version: '>=20.0.0' # Match engines requirement in package.json |
| 32 | + registry-url: 'https://registry.npmjs.org' # Specify npm registry |
| 33 | + cache: 'npm' |
| 34 | + |
| 35 | + - name: Install dependencies |
| 36 | + run: npm ci |
| 37 | + |
| 38 | + - name: Run build |
| 39 | + run: npm run build |
| 40 | + |
| 41 | + - name: Run semantic-release |
| 42 | + id: semantic # Give step an ID to reference its outputs |
| 43 | + uses: cycjimmy/semantic-release-action@v4 |
| 44 | + env: |
| 45 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 46 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 47 | + |
| 48 | + docker_publish: |
| 49 | + name: Build and Push Docker Image to GHCR |
| 50 | + # Run only after the release job completes successfully |
| 51 | + needs: release |
| 52 | + # Run only if semantic-release actually published a new version |
| 53 | + if: needs.release.outputs.new_release_published == 'true' |
| 54 | + runs-on: ubuntu-latest |
| 55 | + permissions: |
| 56 | + contents: read # Needed to check out the code |
| 57 | + packages: write # Needed to push Docker image to GHCR |
| 58 | + attestations: write # Needed for build attestations |
| 59 | + id-token: write # Needed for OIDC (good practice) |
| 60 | + |
| 61 | + steps: |
| 62 | + - name: Checkout code |
| 63 | + # Checkout the specific commit tagged by semantic-release |
| 64 | + uses: actions/checkout@v4 |
| 65 | + with: |
| 66 | + # Use the tag name determined by the release job |
| 67 | + ref: v${{ needs.release.outputs.new_release_version }} |
| 68 | + |
| 69 | + - name: Set up Docker Buildx |
| 70 | + uses: docker/setup-buildx-action@v3 |
| 71 | + |
| 72 | + - name: Log in to GitHub Container Registry |
| 73 | + uses: docker/login-action@v3 |
| 74 | + with: |
| 75 | + registry: ghcr.io |
| 76 | + username: ${{ github.actor }} |
| 77 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + |
| 79 | + - name: Extract Docker metadata |
| 80 | + id: meta |
| 81 | + uses: docker/metadata-action@v5 |
| 82 | + with: |
| 83 | + images: ghcr.io/${{ github.repository }} |
| 84 | + # Use the version from the semantic-release output |
| 85 | + tags: | |
| 86 | + type=raw,value=${{ needs.release.outputs.new_release_version }} # e.g., v1.4.1 |
| 87 | + type=semver,pattern={{version}},value=${{ needs.release.outputs.new_release_version }} # e.g., 1.4.1 |
| 88 | + type=semver,pattern=v{{major}}.{{minor}},value=${{ needs.release.outputs.new_release_version }} # e.g., v1.4 |
| 89 | + type=semver,pattern=v{{major}},value=${{ needs.release.outputs.new_release_version }} # e.g., v1 |
| 90 | + type=raw,value=latest,enable=true # Always tag latest on main branch release |
| 91 | +
|
| 92 | + - name: Build and push Docker image |
| 93 | + id: push |
| 94 | + uses: docker/build-push-action@v6 |
| 95 | + with: |
| 96 | + context: . |
| 97 | + push: true |
| 98 | + tags: ${{ steps.meta.outputs.tags }} |
| 99 | + labels: ${{ steps.meta.outputs.labels }} |
| 100 | + cache-from: type=gha |
| 101 | + cache-to: type=gha,mode=max |
| 102 | + |
| 103 | + - name: Generate artifact attestation |
| 104 | + uses: actions/attest-build-provenance@v1 |
| 105 | + with: |
| 106 | + subject-name: ghcr.io/${{ github.repository }} |
| 107 | + subject-digest: ${{ steps.push.outputs.digest }} |
| 108 | + push-to-registry: true |
0 commit comments