Build Verification & Fast Feedback
The build job answers two questions: does it build, and is the result usable? A build that "succeeds" but produces a broken package is worse than a red build, because it travels further before anybody notices. This lesson covers checking the artifact, making it reproducible, and keeping the pipeline fast with caches.
Verify the artifact, not just the exit code
A build job should end with a smoke test of the artifact it produced: unpack it, start it, ask it its version. The session packages a small Python tool, then installs it into a fresh directory and runs it from there, the way a user would:
mkdir -p shop/src/shopcli && cd shop
cat > src/shopcli/__main__.py <<'PY'
import sys
VERSION = "1.4.0"
if "--version" in sys.argv:
print(f"shopcli {VERSION}")
else:
print("usage: shopcli [--version]")
PY
build() {
mkdir -p dist
tar --sort=name --mtime=@"$SOURCE_DATE_EPOCH" --owner=0 --group=0 --numeric-owner \
-C src -czf dist/shopcli-1.4.0.tar.gz shopcli
}
smoke() {
local tmp; tmp=$(mktemp -d)
tar -xzf dist/shopcli-1.4.0.tar.gz -C "$tmp"
(cd "$tmp" && python3 -m shopcli --version) | grep -qx "shopcli 1.4.0"
}
build && smoke && echo "smoke test passed"
sha256sum dist/shopcli-1.4.0.tar.gz | cut -c1-16 > first.sum
sleep 1 && rm -rf dist && build # a second later: timestamps must not leak in
sha256sum dist/shopcli-1.4.0.tar.gz | cut -c1-16 | diff - first.sum && echo "rebuild is bit-for-bit identical"
echo "gzip header timestamp:$(od -A n -t x1 -j 4 -N 4 dist/shopcli-1.4.0.tar.gz)"smoke test passed
rebuild is bit-for-bit identical
gzip header timestamp: 00 00 00 00
The second half is a reproducibility check: building twice from the same commit gives the same bytes, because the archive has a fixed file order (--sort=name), fixed timestamps (SOURCE_DATE_EPOCH), fixed owners, and a gzip header whose timestamp field is zero: gzip only stores a time when it compresses a named file, and GNU tar feeds it through a pipe. Plain gzip file would store the file's mtime (use gzip -n), and zip stores timestamps too; check your tools, as the session did, instead of assuming. Reproducible builds let anyone verify that an artifact matches its source; Reproducible Builds has the full checklist.
Feedback time is a feature
Developers wait for CI dozens of times a day. Where the time goes in a typical slow pipeline, and the usual fix:
| Time sink | Fix |
|---|---|
| downloading dependencies every run | cache the package manager's download directory, keyed by the lockfile hash |
| rebuilding unchanged layers | Docker layer caching (BuildKit --cache-from/--cache-to, type=gha or type=registry) |
| one long test job | split into parallel shards by timing data |
| running everything for a docs change | path filters (paths-ignore, rules: changes:) |
| waiting for a busy runner | more runners, or autoscaling (Runners) |
| E2E on every PR push | E2E on merge to main, or only for affected areas |
Cache keys: a model
A cache is only safe if its key changes whenever the cached content should change. The usual key is the OS, the tool and a hash of the lockfile, with a restore key prefix as a fallback (a slightly stale cache is still much better than none). The model mimics how actions/cache and GitLab's cache:key:files resolve a key:
import hashlib
def key_for(os_name, lockfile):
digest = hashlib.sha256(lockfile.encode()).hexdigest()[:12]
return f"{os_name}-npm-{digest}"
def restore(store, key, restore_prefixes):
if key in store:
return "hit", key
for prefix in restore_prefixes: # newest entry with a matching prefix
matches = [k for k in store if k.startswith(prefix)]
if matches:
return "partial", matches[-1]
return "miss", None
store = {}
lock_v1 = '{"lodash": "4.17.21"}'
lock_v2 = '{"lodash": "4.17.21", "chalk": "5.6.2"}'
for run, lock in enumerate([lock_v1, lock_v1, lock_v2, lock_v2], start=1):
key = key_for("linux", lock)
status, used = restore(store, key, ["linux-npm-"])
note = f"restored {used}" if used else "nothing restored"
print(f"run {run}: key {key}: {status:<7} {note}")
if status != "hit":
store[key] = "node_modules cache" # saved at the end of the jobrun 1: key linux-npm-bb5d4b6bb85e: miss nothing restored
run 2: key linux-npm-bb5d4b6bb85e: hit restored linux-npm-bb5d4b6bb85e
run 3: key linux-npm-a00d88791a24: partial restored linux-npm-bb5d4b6bb85e
run 4: key linux-npm-a00d88791a24: hit restored linux-npm-a00d88791a24
Runs 1 and 3 pay for downloads; runs 2 and 4 reuse them. The partial restore in run 3 starts from the old cache and only downloads what changed. Two rules keep caches honest:
- Cache downloads, not results. Cache
~/.npmor~/.cache/pip, then still runnpm ci/pip installso the lockfile decides what is installed. A cachednode_modulesfrom another branch can hide a missing dependency. - Never trust a cache for security decisions. Caches can be written by pull-request runs on some platforms; don't cache things a later privileged job executes without checking.
Caches vs artifacts is a recurring confusion; Cache vs Artifacts runs both side by side.
Test yourself
What does a smoke test in the build job check?
- Code style
- That the produced artifact actually installs and starts (e.g. prints its version)
- Test coverage
- The commit message
Which cache key is safe for npm dependencies?
npm-cache- OS + tool + hash of package-lock.json
- The branch name
- The date
Why cache ~/.npm and still run npm ci, instead of caching node_modules?
- node_modules is too large to cache
- The lockfile still decides what is installed; the cache only saves download time
- npm ci deletes caches
- It's required by GitHub