Sub topic 2
Making the artifact immutable and identifiable
This chapter assumes you can read a GitHub Actions workflow, build a container image, and push it to a registry. It covers the two properties that make "build once, promote many" work: the artifact never changes after it is built, and you can always tell exactly which build it is.
What immutable means
An artifact is immutable when its contents cannot change after the build finishes. Staging and production then receive the same bytes. If staging passes, that result says something true about production, because nothing differs between them except configuration.
Immutability depends on two things:
- The artifact has a name that points to one build only.
- Nothing is allowed to overwrite that name.
Choosing an identifier
A good identifier is unique per build and traceable to source. These are the common options:
| Identifier | Example | Strength | Weakness |
|---|---|---|---|
| Commit SHA | `app:3f9c2ab...` | Maps directly to source | Says nothing about order or meaning |
| Build number | `app:482` | Easy to read and sort | Needs a link back to the commit |
| Semantic version | `app:1.8.0` | Meaningful to humans | Easy to reuse by mistake |
| `latest` or `main` | `app:latest` | Convenient | Moves over time, so it is not an identity |
Tags like latest are mutable pointers. Two deployments of app:latest an hour apart can run different code. Never promote by a moving tag.
Tags versus digests
Even a unique tag is only a label in the registry. Someone with push access can retag a different image under the same name. A digest is stronger. It is a hash of the image content, written as sha256:..., so it changes whenever the content changes.
docker pull registry.example.com/app:3f9c2ab
docker inspect --format '{{index .RepoDigests 0}}' registry.example.com/app:3f9c2abThe second command prints a reference such as registry.example.com/app@sha256:9b1d.... Deploying by digest guarantees the exact content, even if a tag is moved later.
A practical approach is to tag with the commit SHA for readability and record the digest as well. Many registries can also be set to make tags immutable, so a push to an existing tag is rejected.
Passing one reference through the pipeline
The release workflow below builds once, then hands the same image reference to every deploy job.
name: release
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
outputs:
image: ${{ steps.meta.outputs.image }}
steps:
- uses: actions/checkout@v4
- id: meta
run: echo "image=registry.example.com/app:${GITHUB_SHA}" >> "$GITHUB_OUTPUT"
# Registry login omitted for brevity.
- run: docker build -t "${{ steps.meta.outputs.image }}" .
- run: docker push "${{ steps.meta.outputs.image }}"
deploy-staging:
needs: build
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh staging "${{ needs.build.outputs.image }}"
deploy-production:
needs: [build, deploy-staging]
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- run: ./deploy.sh production "${{ needs.build.outputs.image }}"The build job exposes one output, image. Both deploy jobs read needs.build.outputs.image, so neither can build its own copy or pick a different tag. The tag is the commit SHA, so every image links to one commit.
Common mistakes
- Rebuilding in each environment. A second build can pull newer dependencies or a different base image, so production may run code staging never saw.
- Promoting a moving tag.
latestor a branch name can point to a new image between the staging and production deploys. - Baking environment settings into the image. Hostnames or feature flags inside the image force a rebuild per environment. The next chapter covers keeping configuration outside it.
- Pinning nothing in the build. If the Dockerfile uses
FROM node:latest, two builds of the same commit can differ. Pin base images by version or digest. - Reusing a version tag. Pushing
1.8.0twice with different content breaks the link between tag and build.
Keeping the record
Once each image has a unique identity, you can record it. Label the image with its source commit so anyone can inspect it later.
ARG GIT_SHA
LABEL org.opencontainers.image.revision=$GIT_SHAPass the value with --build-arg GIT_SHA=${GITHUB_SHA} at build time. Later chapters use this identity to answer which build is running in each environment.
Exercise
Your team deploys registry.example.com/app:latest to staging on Monday and to production on Wednesday. A new commit was pushed to main on Tuesday. Why is this a problem, and what two changes fix it?
Show answer
The latest tag moved on Tuesday, so production receives a different image from the one tested in staging. Fix it by tagging each build with a unique identifier such as the commit SHA, and by deploying the same reference (ideally the digest) to both environments. Enabling tag immutability in the registry prevents accidental overwrites.