Dependency Caching and Reproducibility
Take one task-board application from local tests to a real delivery pipeline: continuous integration fundamentals, push and pull-request quality gates, lint, type-check, unit, component and end-to-end gates, dependency caching, matrix builds, artifacts and checksums, secrets and supply-chain security, release promotion, staging and production, rollback and observability.
What you will be able to do
#- Key a cache so it changes when the lockfile changes.
- Use a frozen lockfile as a reproducibility gate.
- Explain why caching untrusted build output is unsafe.
Overview
#Caching is a contract about inputs
A dependency cache stores downloaded packages between runs so the next run installs faster. The cache is only correct when its key changes whenever the inputs change. The inputs are the lockfile and the package manager version. If the key does not change when the lockfile changes, the run reuses stale packages and the pipeline verifies the wrong dependency set.
jobs:
verify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- run: corepack enable
- run: pnpm install --frozen-lockfile
Key on the lockfile hash
A cache key such as pnpm-${{ hashFiles('pnpm-lock.yaml') }} changes when the lockfile changes. A static key never changes, so it serves stale packages forever. When the key changes, the cache misses and the runner downloads fresh packages, which is the correct behavior.
Frozen lockfile
pnpm install --frozen-lockfile fails when the lockfile does not match the manifest. That is a gate: it prevents a run from silently resolving different versions than the ones committed. Without it, CI can install a dependency set that no developer has.
Restore keys
A restore key lets a run start from a partial match and then fill the gaps. This speeds cold caches but must never be used to skip the install step. The install still runs; the cache only shortens it.
Cache poisoning
A cache written from untrusted input, such as a pull request from a fork, can be read by a later trusted run. Never cache build outputs or secrets from untrusted runs. Cache only the package store, and only for trusted branches.
Sandbox boundary
The browser sandbox cannot download packages or persist a cache between runs. It can compute a cache key from a lockfile string, which is the pure part of the logic.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
The cache key is hashFiles('pnpm-lock.yaml'). A developer bumps a dependency version and commits the new lockfile. What happens on the next run?
Exercise 2Implement
Enable pnpm dependency caching in the Node setup step and keep the install reproducible.
starter.yaml- uses: actions/setup-node@v4 with: node-version: 20 - run: pnpm installReveal the reference answer
reference.yaml- uses: actions/setup-node@v4 with: node-version: 20 cache: pnpm - run: corepack enable && pnpm install --frozen-lockfileAuthored expected output: The setup-node step caches pnpm and the install uses the frozen lockfile.
The cache speeds the install; the frozen lockfile keeps the dependency set identical to the committed one.
Exercise 3Debug
This cache key never changes, so stale packages are reused after a dependency update. Fix the key.
starter.yamlkey: pnpm-cacheReveal the reference answer
reference.yamlkey: pnpm-${{ hashFiles('pnpm-lock.yaml') }}Authored expected output: The key includes the lockfile hash so it changes when dependencies change.
A static key is a correctness bug, not just a performance issue. It serves packages that no longer match the lockfile.
Exercise 4Test
Write a function cacheKey(lockfileHash) that returns a string starting with 'pnpm-' followed by the hash, and returns 'pnpm-missing' when the hash is empty.
starter.javascriptfunction cacheKey(lockfileHash) { // return the cache key }Reveal the reference answer
reference.javascriptfunction cacheKey(lockfileHash) { if (typeof lockfileHash !== 'string' || lockfileHash.length === 0) { return 'pnpm-missing'; } return 'pnpm-' + lockfileHash; }Authored expected output: cacheKey('abc123') returns 'pnpm-abc123'; cacheKey('') returns 'pnpm-missing'.
The prefix namespaces the cache by package manager, and the missing case prevents an empty key from colliding across runs.
Common misconceptions
#- A cache speeds a run without affecting correctness.
- A static cache key is fine if the lockfile rarely changes.
- Caching build output from forks is safe.
Follow-up practice
#- cicd-09-matrix-builds
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.