Sub topic 6
Build once, promote the same artifact
This chapter assumes you can read a basic GitHub Actions workflow, know what a CI job does, and have run npm test and npm run build on a small project. It covers build artifacts and why a pipeline should build exactly once.
What an artifact is
A build artifact is the packaged output of a build. It can be a dist/ folder, a JAR file, a container image, or a ZIP archive. The artifact is what you deploy. The source code is not.
Promotion means moving that same artifact through environments, such as staging and then production, without rebuilding it.
Why build once
Suppose a pipeline rebuilds the code for each environment. Staging gets build A and production gets build B. The two builds can differ even from identical source:
- A dependency version floats and resolves differently on each run.
- The base image or build tool changes between runs.
- Environment variables are baked in at build time.
The tests passed on build A, so they say nothing certain about build B. Building once removes this gap. What you tested is what you ship.
Passing an artifact between jobs
Each job in GitHub Actions runs on a fresh machine. Files from one job do not exist in the next. The upload and download artifact actions carry them across.
name: ci-cd
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
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
deploy:
needs: build
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- run: ./scripts/deploy.sh dist/Note these details:
- The
buildjob runs the tests and the build. It uploadsdist/under the namedist. needs: buildmakesdeploywait, and it runs only ifbuildsucceeded.- The
deployjob never runsnpm run build. It downloads the file thatbuildproduced. scripts/deploy.shis a placeholder for your own deploy script.
The deploy job still checks out the repository. That is only to get scripts/deploy.sh. The code it ships comes from the artifact.
Configuration stays outside the artifact
If the artifact is identical everywhere, environment differences must come from somewhere else. Pass them at deploy time, through environment variables, a config service, or settings attached to the environment. Do not bake a production database URL into the build.
Common errors
- Rebuilding in the deploy job. It looks harmless but breaks the guarantee. Watch for a stray
npm run buildin a deploy step. - Mutable tags. A container tag like
latestcan point to different images over time. Prefer tags based on the commit SHA, or reference the image digest. - Forgetting retention. Pipeline artifacts expire after a retention period. For long-term rollback, store releases in a registry or artifact repository.
- Wrong path. If
pathin the upload step matches nothing, the step warns or fails. Confirm the build really writes todist/.
Exercise
A teammate adds npm run build to the deploy job "so production always gets a fresh build." Why does this weaken the pipeline, and what should the deploy job do instead?
Show answer
The build in the deploy job can produce a different result than the build that was tested, for example because a dependency resolved differently. The tests then no longer describe what is deployed. The deploy job should download the artifact uploaded by the build job and deploy that file unchanged.
Exercise
Staging needs API_URL=https://staging.example.com and production needs API_URL=https://api.example.com. How do you handle this without building twice?
Show answer
Keep the artifact free of environment-specific values and supply API_URL at deploy time, for example through environment-level variables or a runtime config file. The same artifact then runs in both environments.