Sub topic 4
Tagging images with the commit SHA
This chapter assumes you can read a GitHub Actions workflow, know what a container registry is, and have used kubectl set image. It covers why the deploy pipeline tags each image with the commit SHA and how to do it correctly.
Why mutable tags break deploys
A tag such as latest or main is a pointer that you can move. Pushing a new image under the same tag changes what the tag refers to. That causes three problems in a deploy pipeline:
- Kubernetes sees no change. If the Deployment already uses
web:latest, runningkubectl set imagewith the same string produces no new rollout. The Pod template is identical. - Pods can differ from each other. With
imagePullPolicy: Always, a Pod that restarts pulls whateverlatestpoints to at that moment. Replicas can then run different code. - Rollback is unreliable.
kubectl rollout undorestores the previous Pod template. If that template saysweb:latest, you get the same moving tag back, not the old build.
An immutable tag is one you never reassign. The commit SHA is a natural choice because it is unique per commit and maps directly to your source history.
Using the SHA in the workflow
GitHub exposes the full 40-character commit hash as github.sha. The build job from the pipeline example uses it as the tag:
env:
IMAGE: ghcr.io/${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.IMAGE }}:${{ github.sha }}The deploy job then references the same value. In a run step the environment variable GITHUB_SHA holds it:
kubectl set image deployment/web web="$IMAGE:$GITHUB_SHA" -n shopBecause the build and deploy jobs both read github.sha from the same workflow run, they always agree on which image to use. Each push to main produces a new tag, so the Pod template changes and Kubernetes starts a rollout.
Choices and pitfalls
You may add more tags for convenience, but the deploy step should only use the SHA tag.
| Tag | Use it for | Deploy with it? |
|---|---|---|
| Full SHA | Exact traceability and deploys | Yes |
| Short SHA (7 characters) | Readable logs and dashboards | Possible, but collisions are more likely in large repositories |
| `latest` or `main` | Humans pulling a current build | No |
| Semver such as `v1.4.2` | Releases created from Git tags | Yes, if you never move the tag |
Watch for these errors:
- Pull request builds. On
pull_requestevents,github.shais the SHA of a temporary merge commit, not the head of your branch. That is fine for testing, but the tag will not match any commit you can check out. Deploy only frompushtomain. - Re-runs. Re-running a workflow reuses the same SHA and overwrites the image under that tag. If the Dockerfile pulls unpinned base images, the rebuilt image can differ. Pin base image versions to keep rebuilds consistent.
- Deploying before the push finishes. The deploy job must declare
needs: build. Without it, the cluster can try to pull a tag that does not exist yet and reportImagePullBackOff. - Pinning by digest. For the strongest guarantee, deploy by digest (
image@sha256:...). A SHA tag can still be overwritten by someone with push access. The digest cannot.
Exercise
A teammate changes the pipeline to push web:latest and run kubectl set image deployment/web web=web:latest. The next deploy finishes instantly and no Pods change. Why, and what is the fix?
Show answer
The Deployment's Pod template already contains web:latest, so setting the same string produces no change and Kubernetes starts no rollout. Tag each build with a unique value, such as ${{ github.sha }}, and pass that tag to kubectl set image. The template then changes on every commit, which triggers a rollout and keeps kubectl rollout undo meaningful.
Exercise
Why should you deploy only from push events to main and not from pull_request events when tagging with github.sha?
Show answer
On pull_request events, github.sha is the SHA of a temporary merge commit. The resulting tag does not correspond to a commit on your branch, so you cannot trace a running image back to source. A push to main gives a real commit SHA.