Sub topic 1
Why you build once and promote the same build
Prerequisites
This chapter assumes you know the following:
- Basic Git and branching, including what a commit SHA is.
- Continuous integration concepts, such as a pipeline that builds and tests on each push.
- What a container image or other packaged artifact is.
- How to read YAML configuration.
The problem with rebuilding per environment
Many teams build their software separately for each environment. A push to a branch builds and deploys to staging. Later, a second build runs for production. Both builds start from the same source code, so they look equivalent.
They are not equivalent. A rebuild can differ from the first build in several ways:
- A dependency released a new version between the two builds.
- The base image behind a
latesttag changed. - The build machine had a different tool version or a different cached state.
- Someone built from a different commit than the one that was tested.
When this happens, production runs software that nobody tested. The staging results describe a different build from the one users get.
The build-once principle
Build once, promote many means the pipeline builds the release a single time. It stores the result as an artifact. An artifact is the packaged output of a build, such as a container image. The pipeline then moves that same artifact through each environment in turn. Moving it forward is called promotion.
The artifact does not change as it moves. Only the target environment changes. What you tested in staging is the exact thing you run in production.
This gives you three benefits:
- Trustworthy test results. A passing check in staging is evidence about the production build, because it is the same build.
- Faster, lower-risk releases. Promotion skips compiling and packaging, so there is less that can fail at release time.
- Clear accountability. If production breaks, you know the code was the same as in staging. You can look for a difference in configuration or data instead.
A worked example
The following GitHub Actions workflow builds one image and passes its reference to both deploy jobs.
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 }}"Three details make this a build-once pipeline:
- Only the
buildjob runsdocker build. The deploy jobs never build anything. - The
buildjob publishes its image reference as an output. Both deploy jobs read it throughneeds.build.outputs.image. deploy-productionlistsdeploy-staginginneeds. Production can start only after staging succeeds.
The image tag is the commit SHA, so the reference points to one specific build. The deploy.sh script receives that reference as an argument. It does not pick its own version.
Common mistakes
- Rebuilding inside the deploy job. A deploy job that runs
docker buildcreates a new artifact. Keep builds in the build job only. - Deploying a moving tag. A tag such as
latestcan point to a different image tomorrow. Deploy a SHA or a digest instead. - Building per branch. If a
stagingbranch and aproductionbranch each trigger their own build, you are rebuilding. Promote the artifact, not the branch. - Baking environment values into the image. If the image contains staging settings, you must rebuild it for production. Later chapters show how to keep configuration outside the artifact.
Key takeaways
- Rebuilding for each environment means production can run something that was never tested.
- Build once, store the artifact, and promote that same artifact through each stage.
- Pass a specific artifact reference between jobs, and never rebuild in a deploy job.
Exercise
A pipeline builds an image on every push to main and deploys it to staging. For production, an operator runs a second job that checks out the same commit, builds the image again, and deploys it. Both builds use the same commit. Why is this still unsafe, and what is the fix?
Show answer
The two builds can differ even from the same commit. A dependency version, a base image tag, or the build environment may have changed between runs, so production may run an artifact that was never tested in staging. The fix is to build once, tag the image with the commit SHA, and have the production job deploy the image already built and tested, without running a build.
Exercise
In the workflow above, which line makes production wait for staging, and which expression gives the production job the exact image that staging used?
Show answer
needs: [build, deploy-staging] makes production wait for staging to succeed. The expression ${{ needs.build.outputs.image }} gives both deploy jobs the same image reference, which the build job published.