Sub topic 5
Designing the promotion pipeline
This chapter assumes you can read GitHub Actions-style YAML, know how a CI job produces an artifact, and understand what a container image and its registry are. The examples use GitHub Actions and Kubernetes, but the design applies to any CI/CD system.
The working model
A promotion pipeline is a chain of stages. The first stage builds the artifact. Each later stage deploys that same artifact to one environment and checks the result. A stage starts only after the previous stage succeeds.
Three rules define the design:
- Only the build stage creates an artifact.
- Later stages receive a reference to that artifact (for example an image tag) and never rebuild it.
- A stage cannot run unless the stage before it has passed.
A pipeline in YAML
The workflow below implements the model. The build job pushes one image tagged with the commit SHA and exposes its name as a job output. Both deploy jobs read that output.
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 }}"What each part does
outputsonbuildis how the artifact identity travels. Downstream jobs readneeds.build.outputs.image, so no job has to guess or recompute the tag.needscreates the ordering.deploy-productionlistsdeploy-staging, so a failed staging job leaves production untouched. Listingbuildas well is only needed because the job reads its outputs.environmentattaches each job to a named environment. That is where per-environment secrets, variables and approval rules are configured, which later chapters cover.actions/checkoutin the deploy jobs fetches the repository only to getdeploy.sh. It does not rebuild anything.
The deploy script changes only the image reference. Everything else comes from the target environment's own configuration.
#!/usr/bin/env bash
# deploy.sh <environment> <image>
set -euo pipefail
env="$1"
image="$2"
kubectl --namespace "$env" set image deployment/app app="$image"
kubectl --namespace "$env" rollout status deployment/app --timeout=120srollout status blocks until the rollout finishes. With set -e, a timeout returns a non-zero exit code, so the job fails and the pipeline stops. Without that wait, the job could report success while the new pods were still crash-looping.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Rebuilding in the production job | Production runs a different binary than the one tested | Pass the image reference through job outputs |
| Tagging with `latest` | The tag points to different content over time, so you cannot tell what was tested | Tag with the commit SHA |
| Production job missing `needs: deploy-staging` | Production can deploy while staging fails or is still running | Declare the dependency explicitly |
| Deploy script that does not wait for rollout | A broken release is reported as success | Wait for rollout status and fail on timeout |
| Two pipeline runs deploying at once | An older run can overwrite a newer release | Serialize deploys, for example with a `concurrency` group per environment |
Design decisions to make
- Trigger. Run the full chain on pushes to
mainonly. Pull requests should run tests and may build, but should not deploy to shared environments. - Granularity. Add a stage only when it gives different information. A stage that repeats the previous stage's checks adds delay without adding safety.
- Failure behavior. Decide what happens when a stage fails. The usual choice is to stop, keep the artifact, and let the next commit produce a new build.
Exercise
A teammate changes the production job so it runs docker build again with the same commit SHA as the tag, "to be safe". Give two reasons this weakens the pipeline.
Show answer
First, the rebuilt image can differ from the one tested in staging, because base images, dependency downloads or build tools may have changed since the first build. Production would then run code nobody verified. Second, pushing to the same tag overwrites the image that staging used, so the SHA no longer identifies one specific artifact and traceability is lost.