How to Sign a Container Image With Cosign Keyless in CI
Cosign keyless signing uses the workflow OIDC identity, so there is no signing key to manage or leak.
Install cosign with sigstore/cosign-installer, grant id-token: write, then run cosign sign against the pushed image digest with COSIGN_EXPERIMENTAL keyless mode.
Steps
- Add
permissions: id-token: writeandpackages: write. - Install cosign with
sigstore/cosign-installer. - Run
cosign sign --yes <image>@<digest>after the push.
Workflow
.github/workflows/ci.yml
permissions:
id-token: write
packages: write
steps:
- uses: sigstore/cosign-installer@v3
- id: build
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:latest
- run: cosign sign --yes ghcr.io/${{ github.repository }}@${{ steps.build.outputs.digest }}Gotchas
- Sign the digest, not a mutable tag, so the signature stays valid.
- Verify with
cosign verify --certificate-identity-regexp ... --certificate-oidc-issuer https://token.actions.githubusercontent.com.
Related guides
How to Attach an SBOM and Provenance to an Image in CIGenerate and attach an SBOM and build provenance to a container image in GitHub Actions using the sbom and pr…
How to Scan an Image for Vulnerabilities on Push in CIScan a container image for CVEs in GitHub Actions with aquasecurity/trivy-action, failing the build on high o…