|
| 1 | +--- |
| 2 | +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json |
| 3 | +name: Build |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_call: |
| 7 | + inputs: |
| 8 | + release: |
| 9 | + type: boolean |
| 10 | + description: Release |
| 11 | + required: true |
| 12 | + |
| 13 | +jobs: |
| 14 | + plan: |
| 15 | + name: Plan |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + app: ${{ steps.opts.outputs.app }} |
| 19 | + platforms: ${{ steps.opts.outputs.platforms }} |
| 20 | + version: ${{ steps.opts.outputs.version }} |
| 21 | + steps: |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 24 | + with: |
| 25 | + persist-credentials: false |
| 26 | + |
| 27 | + - name: Get Bake Vars |
| 28 | + uses: ./.github/actions/bake-vars |
| 29 | + id: opts |
| 30 | + |
| 31 | + - name: Build Image Metadata |
| 32 | + uses: docker/metadata-action@80c7e94dd9b9319bd5eb7a0e0fe9291e23a2a2e9 # v6.1.0 |
| 33 | + id: meta |
| 34 | + env: |
| 35 | + DOCKER_METADATA_ANNOTATIONS_LEVELS: manifest,index |
| 36 | + with: |
| 37 | + images: ghcr.io/${{ github.repository_owner }}/${{ steps.opts.outputs.app }} |
| 38 | + flavor: latest=false |
| 39 | + tags: | |
| 40 | + type=raw,value=latest,enable=${{ inputs.release }} |
| 41 | + type=raw,value=${{ steps.opts.outputs.version }},enable=${{ inputs.release }} |
| 42 | + type=raw,value=sandbox,enable=${{ ! inputs.release }} |
| 43 | +
|
| 44 | + - name: Upload Bake Metadata |
| 45 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 |
| 46 | + with: |
| 47 | + name: bake-metadata |
| 48 | + path: ${{ steps.meta.outputs.bake-file }} |
| 49 | + if-no-files-found: error |
| 50 | + retention-days: 1 |
| 51 | + |
| 52 | + build: |
| 53 | + name: Build (${{ matrix.platform }}) |
| 54 | + needs: |
| 55 | + - plan |
| 56 | + strategy: |
| 57 | + fail-fast: false |
| 58 | + matrix: |
| 59 | + platform: ${{ fromJson(needs.plan.outputs.platforms) }} |
| 60 | + runs-on: ${{ startsWith(matrix.platform, 'linux/arm') && 'ubuntu-24.04-arm' || 'ubuntu-latest' }} |
| 61 | + steps: |
| 62 | + - name: Checkout |
| 63 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 64 | + with: |
| 65 | + persist-credentials: false |
| 66 | + |
| 67 | + - name: Get Target Architecture |
| 68 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 69 | + id: target |
| 70 | + with: |
| 71 | + script: |- |
| 72 | + core.setOutput('arch', '${{ matrix.platform }}'.split('/').pop()); |
| 73 | +
|
| 74 | + - name: Login to GitHub Container Registry |
| 75 | + uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0 |
| 76 | + with: |
| 77 | + registry: ghcr.io |
| 78 | + username: ${{ github.actor }} |
| 79 | + password: ${{ github.token }} |
| 80 | + |
| 81 | + - name: Download Bake Metadata |
| 82 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 83 | + with: |
| 84 | + name: bake-metadata |
| 85 | + path: ${{ runner.temp }} |
| 86 | + |
| 87 | + - name: Setup Docker Buildx |
| 88 | + uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 |
| 89 | + |
| 90 | + - name: Build Image |
| 91 | + uses: docker/bake-action@6614cfa25eff9a0b2b2697efb0b6159e7680d584 # v7.2.0 |
| 92 | + id: bake |
| 93 | + with: |
| 94 | + files: | |
| 95 | + ./docker-bake.hcl |
| 96 | + cwd://${{ runner.temp }}/docker-metadata-action-bake.json |
| 97 | + targets: image |
| 98 | + provenance: false |
| 99 | + set: | |
| 100 | + *.platform=${{ matrix.platform }} |
| 101 | + *.cache-from=${{ format('type=registry,ref=ghcr.io/{0}/{1}-cache:{2}', github.repository_owner, needs.plan.outputs.app, steps.target.outputs.arch) }} |
| 102 | + *.cache-to=${{ inputs.release && format('type=registry,ref=ghcr.io/{0}/{1}-cache:{2},mode=max,compression=zstd,force-compression=true', github.repository_owner, needs.plan.outputs.app, steps.target.outputs.arch) || '' }} |
| 103 | + *.labels.org.opencontainers.image.title=${{ needs.plan.outputs.app }} |
| 104 | + *.labels.org.opencontainers.image.version=${{ needs.plan.outputs.version }} |
| 105 | + *.labels.org.opencontainers.image.revision=${{ github.sha }} |
| 106 | + *.labels.org.opencontainers.image.vendor=${{ github.repository_owner }} |
| 107 | + *.output=type=image,name=ghcr.io/${{ github.repository_owner }}/${{ needs.plan.outputs.app }},push-by-digest=true,name-canonical=true,push=true,compression=zstd,force-compression=true |
| 108 | + *.tags= |
| 109 | +
|
| 110 | + - name: Export Digest |
| 111 | + env: |
| 112 | + METADATA: ${{ steps.bake.outputs.metadata }} |
| 113 | + run: |- |
| 114 | + mkdir -p ${{ runner.temp }}/digests |
| 115 | + DIGEST=$(jq -r '.image["containerimage.digest"]' <<<"$METADATA") |
| 116 | + test -n "$DIGEST" || { echo "::error::bake metadata had no digest"; jq . <<<"$METADATA"; exit 1; } |
| 117 | + touch "${{ runner.temp }}/digests/${DIGEST#sha256:}" |
| 118 | +
|
| 119 | + - name: Upload Digest |
| 120 | + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 |
| 121 | + with: |
| 122 | + name: digests-${{ steps.target.outputs.arch }} |
| 123 | + path: ${{ runner.temp }}/digests/* |
| 124 | + if-no-files-found: error |
| 125 | + retention-days: 1 |
| 126 | + |
| 127 | + merge: |
| 128 | + name: Merge |
| 129 | + runs-on: ubuntu-latest |
| 130 | + needs: |
| 131 | + - plan |
| 132 | + - build |
| 133 | + outputs: |
| 134 | + digest: ${{ steps.digest.outputs.digest }} |
| 135 | + steps: |
| 136 | + - name: Download Bake Metadata |
| 137 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 138 | + with: |
| 139 | + name: bake-metadata |
| 140 | + path: ${{ runner.temp }} |
| 141 | + |
| 142 | + - name: Download Digests |
| 143 | + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1 |
| 144 | + with: |
| 145 | + path: ${{ runner.temp }}/digests |
| 146 | + pattern: digests-* |
| 147 | + merge-multiple: true |
| 148 | + |
| 149 | + - name: Login to GitHub Container Registry |
| 150 | + uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0 |
| 151 | + with: |
| 152 | + registry: ghcr.io |
| 153 | + username: ${{ github.actor }} |
| 154 | + password: ${{ github.token }} |
| 155 | + |
| 156 | + - name: Create Manifest List and Push |
| 157 | + working-directory: ${{ runner.temp }}/digests |
| 158 | + env: |
| 159 | + APP: ${{ needs.plan.outputs.app }} |
| 160 | + OWNER: ${{ github.repository_owner }} |
| 161 | + META: ${{ runner.temp }}/docker-metadata-action-bake.json |
| 162 | + run: |- |
| 163 | + docker buildx imagetools create \ |
| 164 | + $(jq --raw-output --compact-output ".target.\"docker-metadata-action\".tags | map(select(startswith(\"ghcr.io/$OWNER/$APP\")) | \"-t \" + .) | join(\" \")" "$META") \ |
| 165 | + $(printf "ghcr.io/$OWNER/$APP@sha256:%s " *) |
| 166 | +
|
| 167 | + - name: Inspect Image |
| 168 | + env: |
| 169 | + APP: ${{ needs.plan.outputs.app }} |
| 170 | + OWNER: ${{ github.repository_owner }} |
| 171 | + META: ${{ runner.temp }}/docker-metadata-action-bake.json |
| 172 | + run: |- |
| 173 | + TAG=$(jq --raw-output '.target."docker-metadata-action".args.DOCKER_META_VERSION' "$META") |
| 174 | + docker buildx imagetools inspect "ghcr.io/$OWNER/$APP:$TAG" |
| 175 | +
|
| 176 | + - name: Export Digest |
| 177 | + id: digest |
| 178 | + env: |
| 179 | + APP: ${{ needs.plan.outputs.app }} |
| 180 | + OWNER: ${{ github.repository_owner }} |
| 181 | + META: ${{ runner.temp }}/docker-metadata-action-bake.json |
| 182 | + run: |- |
| 183 | + TAG=$(jq --raw-output '.target."docker-metadata-action".args.DOCKER_META_VERSION' "$META") |
| 184 | + DIGEST=$(docker buildx imagetools inspect "ghcr.io/$OWNER/$APP:$TAG" --format '{{ json . }}' | jq --raw-output '.manifest.digest') |
| 185 | + echo "digest=$DIGEST" >>"$GITHUB_OUTPUT" |
| 186 | +
|
| 187 | + attest: |
| 188 | + name: Attest |
| 189 | + needs: |
| 190 | + - plan |
| 191 | + - merge |
| 192 | + runs-on: ubuntu-latest |
| 193 | + steps: |
| 194 | + - name: Login to GitHub Container Registry |
| 195 | + uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0 |
| 196 | + with: |
| 197 | + registry: ghcr.io |
| 198 | + username: ${{ github.actor }} |
| 199 | + password: ${{ github.token }} |
| 200 | + |
| 201 | + - name: Upload Dependency Snapshot |
| 202 | + uses: anchore/sbom-action@e22c389904149dbc22b58101806040fa8d37a610 # v0.24.0 |
| 203 | + with: |
| 204 | + dependency-snapshot: true |
| 205 | + image: ghcr.io/${{ github.repository_owner }}/${{ needs.plan.outputs.app }}@${{ needs.merge.outputs.digest }} |
| 206 | + |
| 207 | + - name: Attestation |
| 208 | + uses: actions/attest@59d89421af93a897026c735860bf21b6eb4f7b26 # v4.1.0 |
| 209 | + with: |
| 210 | + push-to-registry: false |
| 211 | + subject-name: ghcr.io/${{ github.repository_owner }}/${{ needs.plan.outputs.app }} |
| 212 | + subject-digest: ${{ needs.merge.outputs.digest }} |
| 213 | + |
| 214 | + - name: Verify Attestation |
| 215 | + env: |
| 216 | + GITHUB_TOKEN: ${{ github.token }} |
| 217 | + run: |- |
| 218 | + gh attestation verify \ |
| 219 | + --repo ${{ github.repository }} \ |
| 220 | + oci://ghcr.io/${{ github.repository_owner }}/${{ needs.plan.outputs.app }}@${{ needs.merge.outputs.digest }} |
| 221 | +
|
| 222 | + test: |
| 223 | + if: ${{ ! inputs.release }} |
| 224 | + name: Test |
| 225 | + needs: |
| 226 | + - plan |
| 227 | + - merge |
| 228 | + - attest |
| 229 | + runs-on: ubuntu-latest |
| 230 | + steps: |
| 231 | + - name: Checkout |
| 232 | + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 |
| 233 | + with: |
| 234 | + persist-credentials: false |
| 235 | + |
| 236 | + - name: Setup Go |
| 237 | + uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0 |
| 238 | + with: |
| 239 | + go-version-file: go.mod |
| 240 | + |
| 241 | + - name: Run Tests |
| 242 | + env: |
| 243 | + TEST_IMAGE: ghcr.io/${{ github.repository_owner }}/${{ needs.plan.outputs.app }}@${{ needs.merge.outputs.digest }} |
| 244 | + run: |- |
| 245 | + go test -v -timeout 15m ./tests/... |
0 commit comments