Sub topic 12
Pinning reusable workflows and Docker images by digest
This chapter assumes you can read a workflow file, know that uses: appears in both steps and jobs, and have pinned an action to a commit SHA as in earlier chapters. Here you apply the same idea to two other reference types: reusable workflows and Docker images.
Reusable workflows use the same rule as actions
A reusable workflow is called at the job level with uses:. The reference has the form owner/repo/path/to/workflow.yml@ref, and the ref can be a tag, a branch, or a SHA. Tags and branches can move, so the risk is the same as for an action. A full-length commit SHA fixes the called workflow to one commit.
name: ci
on: [push]
jobs:
build:
# Mutable: the v1 tag can be repointed
# uses: my-org/shared-workflows/.github/workflows/build.yml@v1
# Pinned to one commit, with the version in a comment
uses: my-org/shared-workflows/.github/workflows/build.yml@0123456789abcdef0123456789abcdef01234567 # v1.4.0
with:
node-version: "20"Resolve the SHA the same way as before, with git ls-remote on the tag and its ^{} form. A workflow in the same repository, such as ./.github/workflows/build.yml, takes no @ref. It always runs at the caller's commit, so it needs no pin.
The pin does not reach inside the called workflow
A pinned reusable workflow is one fixed file. That file can still contain uses: some-org/some-action@v3 steps, and those resolve by tag at run time. This is the transitive gap from the previous chapter. Pinning the outer reference freezes the workflow's text but not the actions it calls, unless its author pinned them too.
flowchart TDThe diagram below shows the layers.
Docker images: tags versus digests
An image tag such as node:20 is a mutable label, like a Git tag. A digest is the SHA-256 hash of the image manifest. A manifest that hashes to a given digest cannot be altered without changing that digest, so image@sha256:... always yields the same content.
Docker images appear in workflows in three places:
| Where | Pinned form |
|---|---|
| Step that runs an image as an action | `uses: docker://alpine@sha256:<digest>` |
| Job container | `container: image: node@sha256:<digest>` |
| Service container | `services: db: image: postgres@sha256:<digest>` |
A Docker action's own action.yml can also set image: docker://... with a tag. That reference sits inside the action, so you can only pin it by pinning the action to a SHA whose action.yml already uses a digest.
jobs:
test:
runs-on: ubuntu-latest
container:
# node:20-slim
image: node@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- run: node --versionThe digest above is a placeholder. Replace it with a real one. To look it up:
docker buildx imagetools inspect node:20-slimThe Digest: line in the output is the digest of the top-level manifest. For a multi-architecture image this is the index digest, and the runner picks the matching platform from it. Pin that value. It stays valid across architectures.
Gotchas
- Keep the tag in a comment.
node@sha256:...hides which version you meant. Write# node:20-slimnext to it, as with action SHAs. - Digests do not update themselves. A pinned image stops receiving security patches. Dependabot supports the
dockerecosystem for Dockerfiles and thegithub-actionsecosystem foruses:references. Check whether your Dependabot configuration covers workflowcontainer:images before you rely on it. - A tag plus a digest is not verified. In
image: node:20@sha256:..., the digest decides what runs and the tag is ignored. A stale tag can mislead readers.
Exercise
A caller workflow pins my-org/shared/.github/workflows/deploy.yml to a full commit SHA. The called file contains uses: some-org/setup@v2. Is the setup action pinned, and how would you fix that?
Show answer
No. The SHA fixes only the text of deploy.yml. The inner some-org/setup@v2 still resolves by its mutable tag at run time. Fix it by changing the inner reference to a full-length SHA in the shared repository, then updating the caller's pin to the new commit. A dependency lockfile, covered earlier, records the resolved SHA of such nested references.