Sub topic 11
Tracing what runs in each environment
This chapter assumes you know Git basics, CI concepts, container images and YAML. It also assumes you have read the earlier chapters on immutable artifacts and the promotion pipeline. When an incident starts, the first question is "what is running in production right now, and where did it come from?" This chapter shows how to answer that in seconds.
Why traceability follows from build-once
Traceability depends on two earlier decisions. The artifact is immutable and uniquely identified, so one identifier names one set of bytes. The pipeline promotes that artifact in stages, so each environment holds a known identifier at any moment. Together these give you a chain you can follow in both directions:
- Forward: from a commit to the environments where its build runs.
- Backward: from a running service to the commit, the pipeline run and the person who approved it.
If you rebuild per environment, this chain breaks. Two builds from the same commit can differ, so a commit SHA no longer tells you what is running.
Record the identity at build time
The pipeline in this tutorial tags the image with the commit SHA. That tag is the primary key for tracing. Add metadata so the image describes itself. OCI image labels are the standard place for it:
ARG GIT_SHA
ARG BUILD_URL
LABEL org.opencontainers.image.revision="${GIT_SHA}" \
org.opencontainers.image.source="https://git.example.com/org/app" \
org.opencontainers.image.url="${BUILD_URL}"Pass the values with --build-arg GIT_SHA="${GITHUB_SHA}" in the build step. Anyone with the image can then run docker inspect and see which commit produced it.
Record the identity at deploy time
The image knows its origin. You also need each environment to say which image it runs. In Kubernetes, annotate the Deployment during the deploy step. Add these lines to deploy.sh after the set image command:
kubectl --namespace "$env" annotate deployment/app \
"release.example.com/commit=${image##*:}" \
"release.example.com/deployed-at=$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--overwriteThe expression ${image##*:} removes everything up to the last colon, which leaves the tag, the commit SHA. The annotation works for a quick check. The image reference on the Deployment is still the authoritative record, because it is what the cluster actually runs.
Ask the environment, not the pipeline
A pipeline log tells you what the pipeline tried to do. The cluster tells you what is true now. Someone may have run kubectl set image by hand, or a rollback may have happened. Query the live state:
for env in staging production; do
printf '%s: ' "$env"
kubectl --namespace "$env" get deployment/app \
-o jsonpath='{.spec.template.spec.containers[0].image}{"\n"}'
doneTypical output:
staging: registry.example.com/app:9f2c1ab
production: registry.example.com/app:4be07d3Here staging is ahead of production by at least one build. That is normal while a release waits for approval. Production behind staging is a state you can reason about. An unknown value in either place is a problem.
Let the running service report its version
Cluster queries show what was requested. A version endpoint shows what is actually serving traffic. Expose the commit SHA from the application, set at build time or through an environment variable that the deploy step passes in:
curl -s https://app.example.com/version{"commit":"4be07d3","built_at":"2026-12-02T09:14:00Z"}Automated verification can compare this value with the expected SHA after each deploy. If they differ, a stale pod or a cached route is serving old code. The deploy job should fail instead of reporting success.
Common errors
| Problem | Effect | Fix |
|---|---|---|
| Deploying the `latest` tag | The tag points at different bytes over time, so you cannot tell what runs | Deploy the SHA tag, or better, the image digest |
| Trusting pipeline logs only | Manual changes and rollbacks go unnoticed | Query the cluster and the version endpoint |
| No deploy history | You cannot tell when a version arrived | Use deployment annotations or a deploy log, and keep the rollout history |
| Version endpoint set at runtime from a branch name | The value does not identify the build | Bake in the commit SHA at build time |
Exercise
Production shows app:4be07d3 on the Deployment, but the /version endpoint returns commit 9f2c1ab. Name two plausible causes and the check you would run for each.
Show answer
First, a rollout is incomplete or stuck, so old pods still serve traffic. Run kubectl rollout status deployment/app and list the pods with kubectl get pods -o wide to check their images. Second, the version value was not set from the build, for example it was injected from a stale configuration value. Compare the image label org.opencontainers.image.revision from docker inspect with the endpoint output. A third possibility is a cache or a second service in front of the app returning an old response, which you check by calling a pod directly.