Sub topic 10
Speeding up runs with dependency caching
This chapter assumes you can write a basic workflow that checks out code, sets up Node.js, and runs npm test. It also assumes you know what a lockfile is.
Why caching helps
Every job starts on a fresh runner. Without a cache, npm ci downloads every dependency from the registry on every run. On a project with many packages, this can take a large share of the total run time.
A cache stores files from one run so that later runs can restore them. The cache lives on GitHub's side, outside the runner, and is scoped to your repository.
The simplest approach: setup-node
For common package managers, actions/setup-node has caching built in. Add cache: npm to the setup step:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm testThe action caches npm's download cache, not your node_modules folder. On a cache hit, npm ci still runs and still installs packages, but it reads the tarballs from local disk instead of the network. The cache key is built from the runner's operating system and a hash of your lockfile, so changing package-lock.json produces a new cache.
The cache input also accepts yarn and pnpm. Setup actions for other languages offer a similar input, such as cache: pip in actions/setup-python.
How the cache works
Two details matter here:
- A cache is saved only if the job succeeds. A failed run does not store a cache.
- A cache entry is never overwritten. If the key already exists, GitHub keeps the old entry. This is why the key must change when the dependencies change.
Caching other paths with actions/cache
When no setup action covers your tooling, use actions/cache directly. You choose the paths, the key, and optional fallback keys:
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.cache/my-tool
key: ${{ runner.os }}-my-tool-${{ hashFiles('**/my-tool.lock') }}
restore-keys: |
${{ runner.os }}-my-tool-If no exact match exists for key, the action tries each restore-keys prefix and restores the most recent partial match. The job then updates the cache under the new exact key. A partial restore is usually still faster than starting from nothing.
Common errors
- The key never changes. A fixed key such as
depsmeans the first saved cache is reused forever, even after dependencies change. Include a lockfile hash. - No lockfile.
setup-nodecaching fails if it cannot find a lockfile. Commitpackage-lock.json, or setcache-dependency-pathif it lives in a subfolder. - Caching
node_modulesdirectly. This can break across Node.js versions or operating systems, and it skips the integrity checksnpm ciperforms. Cache the npm download cache instead. - Expecting a cache across operating systems. Keys include
runner.os, so a matrix over Ubuntu and Windows keeps separate caches. - Treating the cache as guaranteed. GitHub evicts entries that have not been accessed for seven days, and it removes the oldest entries when a repository exceeds its storage limit. Your workflow must still work on a cache miss.
Checking whether the cache worked
Open the run log and expand the setup step. A line such as Cache restored from key: ... means a hit. Cache not found for input keys: ... means a miss. At the end of the job, a post step reports whether a new cache was saved.
Exercise
Your workflow uses cache: npm, but each run still shows "Cache not found" and the post step saves a new cache every time. Name one likely cause and a fix.
Show answer
The lockfile changes on every run, for example because a step modifies it, or the key is built from a file that differs between runs. Make sure npm ci is used (it does not rewrite the lockfile), and confirm the lockfile hash is stable between commits that do not change dependencies. A cache saved from a feature branch can also be invisible to another branch, so check that the base branch has a saved cache.