Sub topic 9
Signing from a GitHub Actions workflow
This chapter shows how to sign an image from a CI job without storing a signing key. It assumes you can build and push images, know what a digest is, and have seen keyless signing with OIDC and Fulcio in the previous chapters.
Why sign in CI
A signature made on a laptop proves that a person ran a command. A signature made by your build pipeline proves something more useful: this image came from this workflow, in this repository, on this branch. GitHub Actions can issue each job an OIDC token that names the workflow. Cosign trades that token for a short-lived Fulcio certificate, so no private key has to be stored as a secret.
The workflow
This workflow builds an image, pushes it to GHCR, and signs it by digest.
name: build-and-sign
on:
push:
branches: [main]
permissions:
contents: read
packages: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
id: build
with:
push: true
tags: ghcr.io/${{ github.repository }}:latest
- uses: sigstore/cosign-installer@v3
- name: Sign the image by digest
run: cosign sign --yes ghcr.io/${{ github.repository }}@${{ steps.build.outputs.digest }}Four details matter here.
id-token: writelets the job request an OIDC token. Without it, Cosign cannot get a certificate. This is the most common cause of a failed signing step.packages: writelets the job push the image and the signature to GHCR.--yesskips the interactive confirmation. CI has no terminal to answer it.- The signing step uses
steps.build.outputs.digest, the digest thatbuild-push-actionreports for the image it pushed. The signature is therefore bound to that exact content, not to thelatesttag, which can later point somewhere else.
Verifying the result
The certificate Fulcio issued contains the workflow identity. A verifier pins that identity and the issuer:
cosign verify \
--certificate-identity https://github.com/OWNER/REPO/.github/workflows/build.yml@refs/heads/main \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
ghcr.io/OWNER/REPO@sha256:<digest>The identity is the workflow file path plus the Git ref. The issuer is always https://token.actions.githubusercontent.com for GitHub Actions. Replace OWNER/REPO and the workflow filename with your own. The filename must match the workflow that actually signed, so a workflow named build-and-sign.yml needs that name in the identity.
If you need to accept several branches or tags, --certificate-identity-regexp takes a regular expression. Anchor it tightly. A loose pattern such as .* accepts any workflow, which defeats the check.
Common errors
| Symptom | Likely cause |
|---|---|
| Signing step fails to obtain a token | `id-token: write` is missing from `permissions` |
| `403` when pushing the signature | `packages: write` is missing, or the package grants the repository no write access |
| Verification reports no matching signatures | The signature was made against a different digest than the one you verify |
| Verification fails on identity | The workflow filename or ref in `--certificate-identity` does not match the real one |
Exercise
A teammate changes the signing step to cosign sign --yes ghcr.io/${{ github.repository }}:latest and removes id-token: write from permissions. Name the two problems this causes.
Show answer
Without id-token: write the job cannot get an OIDC token, so keyless signing fails. Signing the latest tag instead of the digest also binds the signature to a mutable reference, so it is not tied to the exact content that was built. Cosign resolves the tag to a digest when signing, but the workflow should sign the digest it just pushed so the intent is explicit and no other push can interfere.