Sub topic 6
Build once, promote the same artifact
This chapter assumes you can read a basic GitHub Actions workflow. You should know what a job and a step are, and that npm ci and npm run build produce a build output. It follows the chapter on pipeline anatomy and leads into continuous delivery.
What an artifact is
A build artifact is a file or set of files that a pipeline produces and later stages consume. Examples include a compiled binary, a dist/ folder of bundled JavaScript, a Python wheel, a JAR file, or a container image.
Source code is not an artifact. It is the input. The artifact is the packaged result of one specific build of one specific commit.
An artifact is the thing you deploy. Everything before it exists to produce it, and everything after it exists to check it, move it, and run it.
The problem with rebuilding per environment
Some pipelines build separately for each environment. A staging job runs npm run build and deploys. Later, a production job runs npm run build again and deploys that result.
This looks harmless, but the second build is a different build. Several things can change between the two runs:
- A dependency range such as
^4.2.0resolves to a newer patch release. - The build runner image gets updated, so the compiler or Node version differs.
- A package registry is briefly unavailable and a fallback mirror serves a different file.
- Someone merges to
mainbetween the two builds, and the production job checks out a newer commit. - Build-time environment variables differ and get compiled into the output.
If any of these happens, you tested one thing in staging and shipped another to production. The staging tests give you no evidence about the production build.
The rule: build once, promote
The fix is a simple rule. Build the artifact one time, store it, and move that same artifact through every environment. Moving it forward is called promotion.
Promotion does not rebuild or recompile anything. It changes where the existing artifact runs. Each stage downstream of the build receives the same bytes.
The benefit is traceability. If the artifact passed tests in staging, you have direct evidence about what runs in production, because it is the same file.
What makes an artifact safe to promote
Four properties make promotion trustworthy.
- Immutable. Once stored, an artifact is never overwritten. If you need a change, you build a new artifact with a new identity.
- Uniquely identified. Use a version number, the commit SHA, or a content digest. Avoid names like
latestas the only identifier, becauselatestpoints at different things over time. - Traceable. You can go from a running artifact back to the commit and pipeline run that produced it. Embedding the commit SHA in the artifact or its metadata is a common way to do this.
- Environment-independent. The artifact contains no settings that belong to one environment. Database URLs, API endpoints, and credentials are supplied at deploy time or at runtime.
The fourth property is where most teams struggle. If a staging URL is compiled into the bundle, you cannot promote that bundle. Keep configuration outside the artifact. Typical approaches are environment variables, a config file mounted at deploy time, or a secrets manager. The twelve-factor app guidance describes the same idea: strict separation of config from code.
A worked example
This workflow builds once, uploads the result, and then deploys the same artifact to staging and production. The script scripts/deploy.sh is a placeholder for your own deployment script. It takes the environment name and the artifact directory.
name: build-once-promote
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test
- run: npm run build
- name: Record which commit produced this build
run: echo "${{ github.sha }}" > dist/BUILD_SHA
- uses: actions/upload-artifact@v4
with:
name: dist-${{ github.sha }}
path: dist/
retention-days: 30
deploy-staging:
needs: build
runs-on: ubuntu-latest
environment: staging
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: dist-${{ github.sha }}
path: dist/
- run: ./scripts/deploy.sh staging dist/
env:
API_URL: ${{ vars.API_URL }}
deploy-production:
needs: deploy-staging
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: dist-${{ github.sha }}
path: dist/
- run: ./scripts/deploy.sh production dist/
env:
API_URL: ${{ vars.API_URL }}Walk through what this does:
- Only the
buildjob runsnpm run build. Neither deploy job has a build step. - The artifact name includes
github.sha, so each commit gets its own uniquely named artifact. A later run cannot be confused with an earlier one. - Both deploy jobs download the artifact built in this same run. They use the same name, so they receive the same files.
API_URLcomes from thevarscontext of each GitHub environment. Staging and production each define their own value, while the artifact stays identical. This keeps configuration out of the build.deploy-productionneedsdeploy-staging, so production never runs if staging fails. You can also configure required reviewers on theproductionenvironment in the repository settings. Production then waits for approval, which is the manual gate that makes this continuous delivery.
The checkout step in the deploy jobs gets the deploy script. It does not get source to compile.
Where artifacts are stored
The workflow artifact store works for simple cases. For anything long-lived, a dedicated registry is usually a better fit.
| Storage | Typical use | Limitation |
|---|---|---|
| CI workflow artifacts | Passing files between jobs in one run | Tied to the run, expires after the retention period |
| Container registry | Docker or OCI images | Needs tag and digest discipline |
| Package registry | npm, PyPI, Maven, NuGet packages | Versions are usually permanent once published |
| Object storage such as S3 | Bundles, archives, static sites | You must design naming and access control yourself |
In a container workflow, the image is the artifact. Deploy by digest, such as myapp@sha256:..., rather than by a mutable tag like v1 or latest. A digest identifies exact content, while a tag can be moved to point at a different image.
Common errors
- Rebuilding in the deploy job. A
npm run buildstep in a deploy job breaks the rule, often by accident when someone copies a job. Review deploy jobs for build commands. - Mutable tags. Overwriting
latestorreleasemeans the name no longer identifies one artifact. Rollbacks become unreliable because the old version is gone. - Baked-in configuration. Environment URLs or keys inside the artifact force a rebuild per environment. They can also leak secrets to anyone who can read the artifact.
- Artifacts that expire too soon. If retention is shorter than your release cycle, you cannot roll back to a previous artifact or promote one that waited for approval. Set retention to match how long a build may wait or may need to be restored.
- No link to the commit. If you cannot tell which commit built the running artifact, debugging production becomes guesswork. Record the SHA in the artifact or its metadata.
- Different artifacts for different jobs. If tests run against a source checkout while the deployed artifact is built in a different job, the tests did not verify the artifact. Where it matters, run smoke tests against the artifact itself.
Relation to the rest of the pipeline
Build once, promote is the link between continuous integration and continuous delivery. Continuous integration produces a verified artifact. Continuous delivery keeps that artifact always ready to release, because the file that passed the tests is the file that ships. Deployment strategies such as blue/green and canary, and rollbacks, depend on being able to redeploy a specific known artifact. They are covered in later chapters.
Exercise
A team's pipeline has a deploy-production job that runs npm ci && npm run build before deploying, because "it is cheap and always fresh". Staging passed all tests an hour earlier. Give two reasons this is risky, and describe how to change the pipeline.
Show answer
The production build is a new build, so it can differ from the one tested in staging. A dependency may resolve to a newer version, the runner image may have changed, or a newer commit may be checked out. The staging tests then say nothing reliable about what ships. To fix it, remove the build commands from the deploy job. Build and test once in an earlier job, upload the output as a uniquely named artifact (for example including the commit SHA), and have both the staging and production jobs download and deploy that artifact. Keep environment-specific settings out of the artifact and supply them at deploy time.
Exercise
Your container image tag myapp:stable is rebuilt and overwritten after every release. Why does this hurt rollbacks, and what identifier should deployments use instead?
Show answer
Overwriting the tag destroys the name that pointed at the previous version, so you cannot reliably redeploy it. Deploy by an immutable identifier such as the image digest (myapp@sha256:...) or a unique version tag tied to the commit SHA, and never reuse it.