Lesson 3 of 9 · 26m
Caching Dependencies and Build Artifacts Correctly
Design cache keys around immutable inputs, accept partial hits safely, and know when caching is slower or dangerous.
Caching is the first optimization teams reach for and the first correctness bug they create. The rule I give new hires is brutal but useful: a cache hit must never change what the correct build would have produced. If that sentence feels restrictive, good. Caches are not a magic speed layer; they are content-addressed trust decisions.
Key from inputs, not hope
A dependency cache should derive from lockfiles and toolchain inputs:
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
restore-keys: |
npm-${{ runner.os }}-hashFiles('package-lock.json') changes when dependency resolution inputs change. restore-keys permit a useful partial hit, but they also mean stale entries can be reused. That is acceptable for a download cache because the package manager still verifies exact versions. It is not acceptable for final build output.
Know the cache type
There are at least three different things people call cache:
download cache package tarballs, compiler artifacts, container layers
incremental compiler or test-runner intermediate state
output cache final dist/, image, or compiled binaryThe first is usually safe when checksums are enforced. The second requires correctness validation. The third is rarely safe unless keyed by every input that affects the output.
Lockfiles are part of the product
If a repository lacks a lockfile, caching is not your biggest problem; reproducibility is. Commit package-lock.json, poetry.lock, Cargo.lock, go.sum, or the equivalent for your ecosystem. Use exact toolchain versions where practical:
node --version
npm ci --ignore-scriptsnpm ci deletes node_modules and installs from the lockfile. It is slower than a loose install but gives CI the reproducibility you actually need.
Do not cache poisoned or mutable state
Avoid caching directories containing credentials, mutable git objects, privileged runner state, or outputs generated from untrusted pull-request code. Beware cache poisoning in forks: if an attacker can write malicious cached content reused by a trusted branch, your optimization became supply-chain malware. Most hosted CI systems isolate fork caches, but self-hosted runners and custom cache servers require explicit separation.
Measure hit rate and cost
Cache restoration over a slow network can cost more than a clean install. Track hit ratio, restore time, save time, cache size, and build correctness incidents. Evict aggressively. A 10 GB cache restored for every job is a tax disguised as an optimization.
Artifact versus cache
Artifacts pass evidence between jobs. Caches accelerate repeated work. Artifacts must be exact and retained deliberately; caches may be wrong, stale, evicted, or absent without changing correctness. If a missing cache causes failure, you built hidden state into the pipeline.
Field notes
- Include OS, architecture, toolchain version, and lockfile hash in keys when any of them affect output.
- Save caches only on successful preparation paths unless you deliberately cache compiler intermediates.
- Never cache secrets or directories such as
~/.sshand cloud credential chains. - For monorepos, key by affected workspace inputs; one giant key invalidates too often, one global key poisons unrelated builds.
Was this lesson helpful?