Sub topic 9
Signing from a GitHub Actions workflow
This chapter shows how to sign a container image from a GitHub Actions job with keyless signing, and how to verify it so the check ties the image to one workflow file on one branch. It assumes you have read the chapters on digests, keyless signing with Fulcio, and identity verification. You should also be comfortable reading a GitHub Actions workflow file.
Why sign in CI
A developer laptop is a weak place to sign release images. The signing key sits on one machine, and the signature says only that someone with that key signed the image. A CI job is a better signer for three reasons:
- The job builds the image, so it knows the exact digest it just pushed.
- The job can get a short-lived OIDC token from GitHub, so no long-lived signing secret needs to exist.
- The signing identity is a workflow file on a branch, which you can review and protect, instead of a person's email address.
Keyless signing in CI therefore removes a whole class of problems: leaked key files, forgotten rotations, and secrets copied between repositories.
How the flow works
GitHub acts as an OIDC identity provider for each workflow run. When the job asks for a token, GitHub issues a signed JWT that says which repository, workflow, and ref the job is running for. Cosign sends that token to Fulcio, which checks it and returns a certificate valid for about ten minutes. Cosign signs the image digest, pushes the signature to the registry, and records the event in the Rekor transparency log.
The private key exists only in the job's memory and is discarded when the job ends. Later verification relies on the certificate, which Rekor's timestamp shows was valid when the signature was made.
The workflow
Save this as .github/workflows/build.yml. The file name matters, because it becomes part of the signing identity.
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 }}Permissions
The permissions block is the part people get wrong most often.
id-token: writelets the job request an OIDC token. Without it, Cosign cannot find an identity to use. It may then fall back to an interactive browser login, which fails on a CI runner.packages: writelets the job push to GHCR.contents: readis enough to check out the code.
Setting permissions explicitly turns off every permission you did not list. That follows least privilege. If other jobs in the file do not need to sign, move id-token: write from the top level into the signing job only.
Sign by digest
The build-push-action step exposes the pushed manifest digest as steps.build.outputs.digest. The signing step uses repo@sha256:... instead of repo:latest. If you signed the tag, Cosign would resolve it to a digest anyway and print a warning. Another push could move the tag between the build and the sign step, so the digest from the build step is the only value you can be sure refers to the image you built.
The --yes flag skips the confirmation prompt that Cosign shows before it writes the signing event to the public Rekor log. A CI job cannot answer prompts, so it needs the flag.
Signing several tags
A digest can have many tags. You sign the digest once, and the signature covers every tag that points at it. If you push 1.4.0 and latest in the same build, you need one cosign sign call. Consumers should still deploy by digest, or verify the tag's current digest before they trust it.
Multi-architecture images
For a multi-platform build, the digest output is that of the image index (the manifest list). Signing it covers the index. Cosign does not sign each platform manifest separately unless you pass --recursive. Verifying the index digest is enough if you always pull through the index.
What ends up in the certificate
Fulcio copies claims from the GitHub token into the certificate. Verification checks these fields.
| Certificate field | Value for a GitHub Actions run |
|---|---|
| Subject alternative name (identity) | `https://github.com/OWNER/REPO/.github/workflows/build.yml@refs/heads/main` |
| OIDC issuer | `https://token.actions.githubusercontent.com` |
| Repository, ref, commit SHA, trigger | Stored in certificate extensions |
The identity is a workflow path plus a ref. It names code you control, and a branch rule can protect that code. The ref changes with how the job was triggered. A run from a tag has refs/tags/v1.4.0 in the identity, and a run from a pull request has refs/pull/N/merge. A policy that expects refs/heads/main therefore rejects images signed from other triggers.
Verifying the result
Run this from anywhere that can reach the registry and the public Sigstore infrastructure:
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>Cosign checks the signature against the certificate, the certificate chain against the Fulcio root, the Rekor entry, and your two pinned values. If any of them fails, the command exits with an error.
Both flags matter. The issuer says who vouches for the identity. Without it, another OIDC provider could mint an identity string that looks the same. Pinning the issuer and an exact identity is the strictest form.
When one policy must cover several refs, for example every release tag, use the regular-expression forms and anchor them:
cosign verify \
--certificate-identity-regexp '^https://github\.com/OWNER/REPO/\.github/workflows/build\.yml@refs/(heads/main|tags/v.*)$' \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
ghcr.io/OWNER/REPO@sha256:<digest>An unanchored regular expression is a common mistake. Without ^ and $, an attacker's repository named OWNER/REPO-evil could match your pattern.
Failure modes and gotchas
- Missing
id-token: write. The job fails, or Cosign asks for an interactive login. Check thepermissionsblock first. Reusable workflows and composite actions inherit permissions from the caller, so grant it there. - Pull requests from forks. GitHub does not give fork-triggered runs an OIDC token with write rights. Build and test these runs, but do not sign them. Sign only on pushes to protected branches or on tags.
- Uppercase in the repository name.
github.repositorykeeps the case of the owner and repository, but registry names must be lowercase. If you haveMyOrg/MyApp, lowercase the value in a step before building and signing. The certificate identity keeps the original case, so use that case when you verify. - Reusable workflows. The identity in the certificate is the reusable workflow's path, not the caller's. A policy that expects the caller's file name will reject the signature. Verify against the shared workflow's path and ref.
- Private repositories and the public log. Keyless signing writes the identity, which includes the repository and workflow path, to the public Rekor instance. If those names are sensitive, use a private Sigstore deployment or a key-based approach.
- Signing a tag. A signature made from a tag name still binds to a digest. Re-verify what the tag points at before you rely on it, or use the digest in deployment files.
- Registry permissions. The signature is stored as another artifact in the same repository, so the job needs push access there. A
403on the sign step with a successful build step usually means the token can push images but not the signature artifact.
Hardening the pipeline
The signature is only as trustworthy as the workflow that creates it. Anyone who can change build.yml on main, or who can push a branch the verification policy accepts, can produce valid signatures. Reduce that risk:
- Protect the branch or tag pattern in the identity, and require review for changes to
.github/workflows/. - Pin third-party actions to a full commit SHA instead of a movable tag such as
@v3, because those actions run in the job that holds the OIDC token. - Keep
id-token: writeout of jobs that run untrusted input, such as pull request builds. - Verify with exact identities in deployment policies, not broad patterns.
Exercise
Your workflow signs successfully on main, but a teammate's tag build produces an image that fails cosign verify with the identity https://github.com/OWNER/REPO/.github/workflows/build.yml@refs/heads/main. Both builds ran the same workflow file. Why does verification fail, and how would you fix the policy without accepting every ref?
Show answer
The certificate identity includes the ref that triggered the run. The tag build has @refs/tags/v1.4.0 in its identity, so the exact refs/heads/main string does not match. Use --certificate-identity-regexp with an anchored pattern such as ^https://github\.com/OWNER/REPO/\.github/workflows/build\.yml@refs/(heads/main|tags/v.*)$, and keep the issuer pinned to https://token.actions.githubusercontent.com.
Exercise
A signing job fails on a GitHub-hosted runner because Cosign cannot get an identity token. The workflow has permissions: contents: read and packages: write. What is missing, and why does adding packages: write earlier not help?
Show answer
The job needs id-token: write. Setting permissions explicitly disables all permissions that are not listed, and packages: write only covers registry pushes. Only id-token: write lets the job request an OIDC token from GitHub.