Sub topic 2
Why cache dependencies in CI
This chapter assumes you can write a basic GitHub Actions workflow in YAML, and that you have used a package manager such as npm, pip, or Cargo. You will learn what caching does in a workflow, when it helps, and which mistakes make it less useful than expected.
The cost of a clean runner
Each job in GitHub Actions starts on a fresh virtual machine called a runner. The runner has no files from your previous runs. A job that installs dependencies therefore downloads every package again, every time.
Here is a typical workflow without caching:
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm testThe npm ci step fetches every package listed in package-lock.json from the registry. If the lock file did not change since the last run, that work repeats for no reason. It also adds network traffic, and a registry outage or rate limit can fail your build.
What a cache does
A cache is a stored copy of files that a later run can restore. The workflow saves a chosen directory under a name called a key. A later run asks for that key. If a matching entry exists, the files are copied onto the runner before your install step runs.
Note what is cached. For npm you cache the download cache (~/.npm), not node_modules. The package manager still runs, but it reads packages from disk instead of the network. This keeps the result correct for the current lock file.
When caching helps
Caching pays off when three things are true:
- The files are expensive to produce, such as downloaded packages or compiled output.
- The files change rarely compared with how often the workflow runs.
- Restoring the files is faster than recreating them.
Caching helps little in other cases:
| Situation | Effect of caching |
|---|---|
| Large dependency tree, small lock file changes | Large savings |
| Few or tiny dependencies | Little or no gain |
| Dependencies change on every run | Cache is rarely reused |
| Cached directory is huge and fast to download | Restore time may exceed install time |
Common errors
These mistakes appear often in first attempts:
- Treating the cache as guaranteed. A cache is an optimization. Entries can be missing, evicted, or unavailable. Your workflow must still succeed on a miss, only more slowly.
- Caching the wrong directory. If
pathdoes not match where your tool stores files, the save works but the restore gives you nothing useful. - Using a fixed key. A key that never changes serves stale files forever. Keys should change when the inputs change, usually by hashing the lock file. The next chapters cover this.
- Caching build output you need to share. A cache is for speeding up later runs. It is not a way to pass results between jobs or to publish them.
Measuring the benefit
Do not assume caching helped. Open a run and compare step durations with and without a hit. The log of a cache step also states whether it found an entry. If restoring takes about as long as installing, the cache adds complexity without a gain, and you can remove it.
Exercise
A workflow caches node_modules under the fixed key npm-cache. A teammate adds a new dependency, but CI still fails with "module not found" for it. Give the likely cause and one fix.
Show answer
The fixed key never changes, so every run restores the same old copy of the files and ignores the lockfile change. Make the key depend on the lock file, for example by including a hash of package-lock.json. Also prefer caching the package manager's download cache over node_modules.