|
| 1 | +name: CI |
| 2 | +# |
| 3 | +# Pipeline overview |
| 4 | +# ================= |
| 5 | +# |
| 6 | +# Release-please and Docker delivery are combined in one workflow because |
| 7 | +# GITHUB_TOKEN-created tags do not trigger new workflow runs. This means a |
| 8 | +# versioned Docker image must be built in the same run that release-please |
| 9 | +# creates the release, using its job outputs to pass the version through. |
| 10 | +# |
| 11 | + |
| 12 | +on: |
| 13 | + push: |
| 14 | + branches: |
| 15 | + - main |
| 16 | + pull_request: |
| 17 | + |
| 18 | +jobs: |
| 19 | + |
| 20 | + # Create a GitHub release (and tag) when conventional commits warrant one. |
| 21 | + release-please: |
| 22 | + name: Release Please |
| 23 | + if: github.ref == 'refs/heads/main' |
| 24 | + runs-on: ubuntu-latest |
| 25 | + permissions: |
| 26 | + contents: write |
| 27 | + pull-requests: write |
| 28 | + outputs: |
| 29 | + release_created: ${{ steps.release.outputs.release_created }} |
| 30 | + version: ${{ steps.release.outputs.version }} |
| 31 | + steps: |
| 32 | + - uses: googleapis/release-please-action@v4 |
| 33 | + id: release |
| 34 | + with: |
| 35 | + release-type: node |
| 36 | + |
| 37 | + # Build each platform on its native runner and push by digest (no tag yet). |
| 38 | + build: |
| 39 | + name: Build (${{ matrix.name }}) |
| 40 | + runs-on: ${{ matrix.runner }} |
| 41 | + permissions: |
| 42 | + contents: read |
| 43 | + packages: write |
| 44 | + strategy: |
| 45 | + fail-fast: false |
| 46 | + matrix: |
| 47 | + include: |
| 48 | + - platform: linux/amd64 |
| 49 | + runner: ubuntu-24.04 |
| 50 | + name: amd64 |
| 51 | + - platform: linux/arm64 |
| 52 | + runner: ubuntu-24.04-arm |
| 53 | + name: arm64 |
| 54 | + steps: |
| 55 | + - name: Checkout repository |
| 56 | + uses: actions/checkout@v6 |
| 57 | + - name: Set up Docker Buildx |
| 58 | + uses: docker/setup-buildx-action@v4 |
| 59 | + - name: Log in to GHCR |
| 60 | + uses: docker/login-action@v4 |
| 61 | + with: |
| 62 | + registry: ghcr.io |
| 63 | + username: ${{ github.actor }} |
| 64 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 65 | + - name: Build and push by digest |
| 66 | + id: build |
| 67 | + uses: docker/build-push-action@v7 |
| 68 | + with: |
| 69 | + context: . |
| 70 | + file: docker/Dockerfile |
| 71 | + platforms: ${{ matrix.platform }} |
| 72 | + outputs: type=image,name=ghcr.io/${{ github.repository }},push-by-digest=true,name-canonical=true,push=true |
| 73 | + cache-from: type=gha |
| 74 | + cache-to: type=gha,mode=max |
| 75 | + - name: Export digest |
| 76 | + run: | |
| 77 | + mkdir -p /tmp/digests |
| 78 | + digest="${{ steps.build.outputs.digest }}" |
| 79 | + touch "/tmp/digests/${digest#sha256:}" |
| 80 | + - name: Upload digest |
| 81 | + uses: actions/upload-artifact@v7 |
| 82 | + with: |
| 83 | + name: digest-${{ matrix.name }} |
| 84 | + path: /tmp/digests/* |
| 85 | + if-no-files-found: error |
| 86 | + retention-days: 1 |
| 87 | + |
| 88 | + # Merge platform digests into a single multi-platform manifest and apply tags. |
| 89 | + # - push to main (no release) → ghcr.io/.../postguard-website:edge |
| 90 | + # - push to main (release) → ghcr.io/.../postguard-website:edge + :1.2.3 |
| 91 | + # - pull request → ghcr.io/.../postguard-website:pr-123 |
| 92 | + finalize: |
| 93 | + name: Finalize Docker manifest |
| 94 | + needs: [build, release-please] |
| 95 | + if: always() && needs.build.result == 'success' |
| 96 | + runs-on: ubuntu-latest |
| 97 | + permissions: |
| 98 | + contents: read |
| 99 | + packages: write |
| 100 | + steps: |
| 101 | + - name: Download digests |
| 102 | + uses: actions/download-artifact@v8 |
| 103 | + with: |
| 104 | + path: /tmp/digests |
| 105 | + pattern: digest-* |
| 106 | + merge-multiple: true |
| 107 | + - name: Docker metadata |
| 108 | + id: meta |
| 109 | + uses: docker/metadata-action@v6 |
| 110 | + with: |
| 111 | + images: ghcr.io/${{ github.repository }} |
| 112 | + tags: | |
| 113 | + type=edge,branch=main |
| 114 | + type=ref,event=pr |
| 115 | + type=raw,value=${{ needs.release-please.outputs.version }},enable=${{ needs.release-please.outputs.release_created == 'true' }} |
| 116 | + - name: Set up Docker Buildx |
| 117 | + uses: docker/setup-buildx-action@v4 |
| 118 | + - name: Log in to GHCR |
| 119 | + uses: docker/login-action@v4 |
| 120 | + with: |
| 121 | + registry: ghcr.io |
| 122 | + username: ${{ github.actor }} |
| 123 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 124 | + - name: Create and push manifest |
| 125 | + working-directory: /tmp/digests |
| 126 | + run: | |
| 127 | + docker buildx imagetools create \ |
| 128 | + $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 129 | + $(printf 'ghcr.io/${{ github.repository }}@sha256:%s ' *) |
0 commit comments