Sign and verify container images

  • Tier: Premium, Ultimate
  • Offering: GitLab.com
  • Status: Beta

Sign a container image to prove which pipeline built it. Before a deploy, verify the signature to confirm the image came from your project and was not changed.

Your pipeline signs the image with Cosign. Artifact Central stores the signature and any attestations next to the image, in the same repository.

With keyless signing, Cosign uses the job’s OpenID Connect (OIDC) ID token as the signing identity. You do not create, store, or rotate a signing key.

Prerequisites

  • A Docker or OCI hosted repository. For more information, see supported artifacts.
  • The Artifact Contributor role or higher on the repository for the user who runs the signing job. The signature is pushed to the repository.
  • The Artifact Viewer role or higher on the repository for the user who runs the verify job.
  • A hidden job that exchanges CI_JOB_TOKEN for an Artifact Central token, such as .artifact-registry. For more information, see use Artifact Central in CI/CD.

In the examples on this page, replace the following placeholders:

  • <registry_host>: Enter the hostname from your repository’s setup instructions. To find it, go to your repository and select More actions > View setup instructions.
  • <registry_handle>: Enter your organization’s registry handle.
  • <repository_name>: Enter the name of your Docker or OCI repository.

Sign an image

Sign the image by its digest, not by its tag. A tag can move to a different image later.

The build job records the digest right after the push. The signing job signs that digest and never reads the tag, so it can’t sign an image that someone else pushed to the same tag.

To sign an image:

  1. In .gitlab-ci.yml, add build, test, and sign stages. Then add a job that pushes the image with the full commit SHA as its tag, and saves the pushed digest in a dotenv report:

    yaml
    stages:
      - build
      - test
      - sign
    
    build:docker:
      extends: .artifact-registry
      stage: build
      image: docker:27
      services:
        - docker:27-dind
      variables:
        DOCKER_TLS_CERTDIR: ""
        DOCKER_HOST: tcp://docker:2375
        IMAGE: <registry_host>/<registry_handle>/container/<repository_name>/app
      before_script:
        - apk add --no-cache curl jq
        - !reference [.artifact-registry, before_script]
      script:
        - echo "$AR_TOKEN" | docker login <registry_host> -u __token__ --password-stdin
        - docker buildx build --push --metadata-file metadata.json -t "$IMAGE:$CI_COMMIT_SHA" .
        - DIGEST=$(jq -er '."containerimage.digest"' metadata.json)
        - echo "DIGEST=$DIGEST" > build.env
      artifacts:
        reports:
          dotenv: build.env
  2. Add a signing job that runs after the push. The job requests an ID token with the sigstore audience, then signs the digest from the build job:

    yaml
    sign:docker:
      extends: .artifact-registry
      stage: sign
      image: alpine:3.22
      needs:
        - job: build:docker
          artifacts: true
      id_tokens:
        SIGSTORE_ID_TOKEN:
          aud: sigstore
      variables:
        IMAGE: <registry_host>/<registry_handle>/container/<repository_name>/app
      before_script:
        - apk add --no-cache curl cosign crane
        - !reference [.artifact-registry, before_script]
      script:
        - echo "$AR_TOKEN" | crane auth login <registry_host> -u __token__ --password-stdin
        - cosign sign --yes "$IMAGE@$DIGEST"

Cosign reads the ID token from the SIGSTORE_ID_TOKEN variable. It uses the registry credential that crane auth login stores, so the job logs in once.

The signature is stored in the same repository as a tag named after the image digest, such as sha256-<digest>.sig. In this tag, <digest> is the hex part of the image digest, without the sha256: prefix.

Attach an attestation

An attestation is a signed statement about an image, such as a software bill of materials (SBOM) or build provenance. Attach it with cosign attest in the signing job.

For example, Container Scanning writes a CycloneDX SBOM to gl-sbom-report.cdx.json. To attach that SBOM to the image, add container_scanning to the signing job’s needs:artifacts, next to build:docker. Then add this line to the job’s script:

shell
cosign attest --yes --type cyclonedx --predicate gl-sbom-report.cdx.json "$IMAGE@$DIGEST"

The attestation is stored as a tag such as sha256-<digest>.att.

The .sig and .att tags show in the repository like any other tag. Deleting the image doesn’t delete them.

Verify an image

Verify the signature before you deploy or promote an image. The command checks that the image was signed by a pipeline in your project, on your default branch.

shell
cosign verify \
  --certificate-identity "https://gitlab.com/<project_path>//.gitlab-ci.yml@refs/heads/main" \
  --certificate-oidc-issuer "https://gitlab.com" \
  "<registry_host>/<registry_handle>/container/<repository_name>/app@sha256:<digest>"

Replace <project_path> with the full path of the project that signed the image, and main with its default branch. Replace <digest> with the hex part of the image digest. The command already includes the sha256: prefix.

To verify in a pipeline, run the same command in the job that deploys or promotes the image, after crane auth login. Use the CI/CD variables CI_PROJECT_PATH and CI_DEFAULT_BRANCH for the identity. If the verify fails, the job fails, and the image is not deployed.

To verify an attestation, use cosign verify-attestation with the same flags and --type cyclonedx.

Known issues

  • Artifact Central does not show signatures or attestations in its UI. Signing support in the UI is planned in issue 634075.
  • Keyless signing records each signature in the public Sigstore transparency log. The entry includes the project path, the Git ref, and other details of the pipeline. If your project path must stay private, sign with a key instead. For more information, see the Cosign signing guide.