Sub topic 8
Attesting and Verifying Container Images
This chapter assumes you can write a GitHub Actions workflow with jobs, steps and permissions. It also assumes you can push an image to a registry and use the GitHub CLI. You should already know how actions/attest-build-provenance attests a file. Here you apply the same action to a container image.
Why images need a different subject
A file attestation names a path such as dist/app. An image has no single file on the runner. It is a manifest in a registry, and the registry identifies it by a digest. A digest is the SHA-256 hash of the manifest, written as sha256:....
A tag such as v1.2.0 is a movable label. Someone can push a different image under the same tag. So the attestation must bind to the digest, not the tag. The signed statement then says "this exact manifest was built by this workflow".
Generating the attestation
The job needs three permissions beyond reading the code. id-token: write lets the job request an OIDC token for signing. attestations: write lets it save the attestation. packages: write lets it push to GitHub Container Registry (GHCR).
Add these steps to a job with those permissions:
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.ref_name }}
- name: Attest image provenance
uses: actions/attest-build-provenance@v2
with:
subject-name: ghcr.io/${{ github.repository }}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: trueThree details matter here.
- The build step has
id: push. The attest step readssteps.push.outputs.digest, which is the digest the registry reported after the push. subject-namehas no tag. It is the image name only, and the digest identifies the content.push-to-registry: truestores the attestation in the registry as an OCI artifact linked to the image. Without it, the attestation is kept only by GitHub.
Verifying an image
Use the oci:// prefix so the CLI knows the subject is an image. The CLI fetches the image digest from the registry, finds the matching attestation, and checks the signature and the signing identity.
gh attestation verify oci://ghcr.io/OWNER/IMAGE:TAG --owner OWNER --format json--owner accepts any repository under that account or organization. Use --repo OWNER/REPO to accept one repository only. You can add --signer-workflow to require a particular workflow file, as with file artifacts.
The JSON output lists the verified attestation, including the certificate fields and the statement. A script can read the source repository, the commit and the workflow from it.
You can verify by tag, but the CLI resolves the tag to a digest first. The check then covers that digest only. For deployments, pin the digest in your manifests so what you verified is what runs:
gh attestation verify oci://ghcr.io/OWNER/IMAGE@sha256:DIGEST --repo OWNER/REPOCommon errors
| Symptom | Likely cause |
|---|---|
| `no attestations found` | The attest step ran with the wrong digest, or `push-to-registry` was off and the client is not logged in to GitHub. |
| Attest step fails with a permissions error | `id-token: write` or `attestations: write` is missing from the job. |
| Verification passes for the wrong image | You verified a tag that later moved. Verify and deploy by digest. |
| Registry push of the attestation is denied | `packages: write` is missing, or the image name has capitals. |
| Digest output is empty | The build step has no `id`, or it did not push. |
A passing check proves the image digest was attested by the identity you named. It does not prove the code was free of bugs or that the build was reviewed. Later chapters cover those limits.
Exercise
Your workflow pushes ghcr.io/acme/web:v2. A teammate moves the v2 tag to a different image after the build. Your attestation was created with subject-digest from the build step. What does gh attestation verify oci://ghcr.io/acme/web:v2 --repo acme/web do, and how do you make deployments safe against this?
Show answer
The CLI resolves the tag to the new image's digest, finds no attestation for that digest, and fails. This is the correct outcome, because the new image was not built by the attested workflow. To stay safe, deploy by digest (ghcr.io/acme/web@sha256:...) and verify that same digest before deploying.