Sub topic 7
Speeding up runs with dependency caching
This chapter assumes you have a working lint workflow like the one from the previous chapters. It uses actions/checkout, actions/setup-node, npm ci, and a package-lock.json file. You should also know that each GitHub-hosted job starts on a fresh virtual machine.
Why caching helps
A fresh runner has nothing installed, so every run downloads all your dependencies again. For a lint job, installing packages often takes longer than running the linter. A cache stores files from one run and restores them in the next. That cuts download time and reduces failures caused by registry outages.
What gets cached matters. The cache holds the package manager's download store, not your node_modules folder. For npm, that is the ~/.npm directory. npm ci still runs every time. It deletes node_modules and rebuilds it, but it pulls packages from the local cache instead of the network.
Turning on the built-in cache
The setup-node action can manage the cache for you. Add one line:
name: Lint
on:
pull_request:
push:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm run lint
- run: npm run format:checkThe cache: npm setting does three things:
- Before your steps run, it looks for a cache entry that matches your lockfile.
- If it finds one, it restores
~/.npm. - When the job succeeds, it saves a new entry if none matched exactly.
The action also supports yarn and pnpm. Whichever you choose, the matching lockfile must exist in the repository.
How the cache key works
The action builds a key from the operating system and a hash of the lockfile. When you change a dependency, the lockfile changes and so does the hash. The old entry no longer matches exactly, so the job installs what it needs and saves a new entry.
The diagram below shows the decision.
Things to know
- Commit the lockfile. Without
package-lock.json,cache: npmfails with an error, andnpm cifails too. - Caches are scoped by branch. A pull request can read caches from its base branch, such as
main, but not from sibling branches. Run the workflow on pushes tomainso that the default branch keeps a warm cache for pull requests. - Caches are not a source of truth. Treat a cache as an optimization. If the cache is empty, the job must still pass.
- Storage is limited. GitHub evicts entries that have not been used for seven days, and it removes the oldest entries when a repository exceeds its size limit.
Caching other tools
Some tools keep their own caches. ESLint can store results with --cache, and Ruff keeps a .ruff_cache folder. You can persist these with actions/cache, but the gain for lint-only jobs is usually small. Start with the dependency cache and add more only if you measure a slow step.
Exercise
Your lint job shows a cache miss on every run, even when you have not changed any dependencies. Name two likely causes.
Show answer
The lockfile may be changing between runs, for example if a step regenerates it or the lockfile is not committed, so the hash differs each time. Or the cache is never saved because the job fails before it finishes, or because the cache was created on a branch that the pull request cannot read from, such as a sibling branch rather than main.