Sub topic 12
Pinning reusable workflows and Docker images by digest
This chapter assumes you can read GitHub Actions workflow syntax and know the difference between a Git tag and a commit. It also assumes you have seen the earlier chapters on pinning uses: references for actions to full-length commit SHAs. Here the same idea is applied to two other kinds of reference: reusable workflows and Docker images.
Where else a mutable reference hides
A workflow can pull in outside code in more places than steps[*].uses. Each of the following places names something that someone else can change:
- A job-level
uses:that calls a reusable workflow in another repository. - A step that runs a Docker image directly with
uses: docker://.... - A job
container:or aservices:entry, which names an image for the whole job. - An action whose
action.ymldeclaresruns: using: docker, which in turn names an image or a Dockerfile.
The first is a Git reference, so the SHA rules you already know apply. The others are container registry references, which use a different identifier called a digest.
Pinning reusable workflows
A reusable workflow is a workflow file with an on: workflow_call trigger. A caller invokes it with uses: at the job level, not inside steps. The reference has the form owner/repo/.github/workflows/file.yml@ref, and ref can be a branch, a tag, or a commit SHA.
name: ci
on: [push]
jobs:
# Mutable: the maintainers can move v2 or push to main
build-floating:
uses: my-org/shared-workflows/.github/workflows/build.yml@v2
# Pinned: one commit, with the version in a comment for humans
build-pinned:
uses: my-org/shared-workflows/.github/workflows/build.yml@0123456789abcdef0123456789abcdef01234567 # v2.3.1
with:
node-version: "20"
secrets: inheritThe risk is larger than for an ordinary action. A reusable workflow runs as a whole job, and secrets: inherit hands it every secret the caller can see. Whoever controls the reference controls code that runs with those secrets.
Pin it the same way you pin an action. Resolve the tag to a commit with git ls-remote, take the peeled line (^{}) if the tag is annotated, and confirm the commit belongs to the intended repository.
git ls-remote https://github.com/my-org/shared-workflows 'refs/tags/v2.3.1' 'refs/tags/v2.3.1^{}'Two cases are handled differently:
- Same-repository calls.
uses: ./.github/workflows/build.ymlhas no ref. It runs the file from the same commit as the caller, so there is nothing to pin and no separate trust boundary. - Calls inside the called workflow. The file at your pinned SHA may itself contain
uses:lines. Those lines are fixed by that commit, but if they say@v1, they still follow a mutable tag. The pin on the outer reference does not rewrite them. The next chapters on transitive dependencies and lockfiles cover this gap.
Why Docker images need a digest
A Docker tag such as node:20 is a pointer in a registry. The image owner, or anyone who gains push access to the repository, can push a different image under the same tag. Registries do this routinely, to ship security patches. You get different bits on different days, with no change in your repository.
A digest identifies the content. It is the SHA-256 hash of the image manifest, written sha256: followed by 64 hex characters. The Docker client checks the downloaded content against the digest, so a registry cannot serve different content for a digest you named. This is the same property a commit SHA gives you for Git.
Tag and digest are different kinds of identifier, and the digest wins. A reference can carry both:
node:20-slim@sha256:<digest>When both are present, Docker resolves by the digest and ignores the tag. That makes the tag a comment that can silently become wrong. If someone edits the digest and leaves the tag, nothing complains.
Finding a digest
You can read a digest from the registry without pulling the image. Either of these works:
# crane, from the go-containerregistry project
crane digest node:20-slim
# Docker Buildx: look for the "Digest:" line at the top of the output
docker buildx imagetools inspect node:20-slimBoth commands report the digest of the top-level object the tag points to. For a multi-architecture image, that object is an index (a manifest list) that points to one manifest per platform. Prefer the index digest. A runner on ubuntu-latest (x86-64) and a developer on an ARM laptop then both resolve the same pin to the right platform image. If you copy the digest of a single platform manifest, the image fails to run, or runs the wrong architecture, on other hardware.
Pinning each image reference
For a Docker step, add the digest after the image name:
steps:
- name: Lint with a pinned container
uses: docker://alpine:3.20@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
with:
args: echo helloFor a job container and its service containers, use the same syntax in image::
jobs:
test:
runs-on: ubuntu-latest
container:
image: node:20-slim@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
services:
redis:
image: redis:7@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
ports:
- 6379:6379
steps:
- run: node --versionThe digests above are placeholders. Replace them with values you resolve yourself. For a registry other than Docker Hub, include the host, for example ghcr.io/owner/image:1.2.3@sha256:....
Docker-based actions deserve a separate check. When you pin an action to a commit SHA, you fix its action.yml and any Dockerfile in that commit. If action.yml says image: Dockerfile, the action builds from that file, and the FROM line inside it may use a floating tag. If it says image: docker://..., the same question applies to that reference. Read the pinned commit's files to see what the image is. If the action is yours, use digests in its FROM lines and in runs.image.
What a digest does not give you
A digest proves that the bytes are the ones you named. It does not show that the bytes are safe or that they came from the publisher you think they did. If you resolve the digest from a compromised tag, you pin the compromised image. Pinning has three limits:
- Provenance. To verify who built an image, use signatures or attestations, for example verifying with
cosignagainst the publisher's identity. That is a separate step from pinning. - Staleness. A pinned image never receives base-image security patches until you change the digest.
- Impostor commits. A reusable workflow SHA has the same fork-network caveat as an action SHA. GitHub can resolve a commit that exists only in a fork of the repository. Confirm that the commit is reachable from a branch or tag in the repository you intended, not just that the URL resolves.
Keeping pins current
Pins you never update become a liability, so automate the updates.
For reusable workflows, the github-actions ecosystem in Dependabot handles uses: references to reusable workflows as well as actions. The configuration you already have covers them, including SHA-pinned ones when a version comment is present:
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"Images are less uniform. Dependabot's docker ecosystem updates tags and digests in Dockerfiles, so it covers the FROM lines of a Docker action you maintain. It does not necessarily cover image references written inside workflow YAML. Check the current Dependabot documentation for your case before you rely on it. Other tools, such as Renovate, have managers that read container:, services:, and docker:// references in workflows and can propose digest updates. Whatever tool you use, test one update end to end, so that you know a stale digest will produce a pull request instead of silence.
Enforcement and its limits
The repository and organization policy that requires full-length commit SHAs applies to Git references in uses:. It covers actions and reusable workflows, and a workflow that uses a tag or branch for either will fail. A container digest is not a Git commit, so do not assume the policy checks images. Enforce image digests with a separate check, such as a linter or a search in CI that fails on any image: or docker:// value lacking @sha256:.
| Reference | Mutable form | Pinned form | Updated by |
|---|---|---|---|
| Action | `owner/repo@v4` | `owner/repo@<40-hex SHA> # v4.2.2` | Dependabot `github-actions` |
| Reusable workflow | `owner/repo/.github/workflows/f.yml@v2` | same path with `@<40-hex SHA> # v2.3.1` | Dependabot `github-actions` |
| `docker://` step | `docker://alpine:3.20` | `docker://alpine:3.20@sha256:<digest>` | Renovate, or manually |
| Job `container:` or `services:` | `image: node:20` | `image: node:20@sha256:<digest>` | Renovate, or manually |
| Dockerfile in an action | `FROM node:20` | `FROM node:20@sha256:<digest>` | Dependabot `docker` |
Common errors
- Pinning to the digest of one platform instead of the index, which breaks on a different CPU architecture.
- Leaving the tag next to the digest and trusting the tag. The tag is ignored, so it can drift from what actually runs.
- Pinning the outer reusable workflow and assuming everything inside it is fixed. Inner
uses:lines still follow their own refs. - Pinning once and never updating, so the pinned image or workflow stops receiving fixes.
Exercise
A workflow's job container is image: node:20-slim@sha256:aaaa.... A teammate updates the tag to node:22-slim and leaves the digest unchanged. Which Node version runs, and why? What check would catch this mistake?
Show answer
Node 20 still runs, because Docker resolves the reference by the digest and ignores the tag when both are present. The tag no longer describes the image. A CI check could resolve the tag's current digest (for example with crane digest node:22-slim) and fail when it differs from the digest in the file. Alternatively, an update tool such as Renovate rewrites the tag and the digest together, so they cannot drift apart.
Exercise
You pin a reusable workflow call to a verified commit SHA. The called file contains uses: some-org/some-action@v1 in one of its steps. Is that step now fixed? What would you do about it?
Show answer
No. The pin fixes the called workflow file, but the inner reference still follows the v1 tag, which the action's maintainers can move. If you own the called workflow, pin the inner action to a SHA in that file. If you do not, ask the owner to do so, fork the workflow into your control and pin it, or use a lockfile that records the resolved SHA of each nested dependency.