How to Implement CI Dependency Caching
Cache dependencies in CI: key the cache on the lockfile, restore before install, and invalidate on a lockfile or a platform change.
On this page
Introduction
A CI job that re-downloads every dependency on every run spends most of its time on the network, when the dependencies rarely change. Caching them makes the run fast: the cache is keyed on the lockfile, and an unchanged lockfile restores the packages and skips the download. The catch is the key and the invalidation: a cache that hits when it should not installs stale packages, and one that misses when it should hits nothing.
This tutorial covers keying the cache on the lockfile, restoring before the install, including the platform and the version, and testing the hit and the invalidation. The examples use a CI cache, and the design applies to any CI with a dependency cache.
Key the cache on the lockfile
The cache key is the hash of the dependency lockfile, so a change to the dependencies changes the key and the cache misses, and an unchanged lockfile hits. The lockfile is the exact resolved set, so its hash is the right cache key: the same lockfile means the same packages, and a different lockfile means a different set that must be installed.
Hash the lockfile’s content, not its timestamp, so a touch without a change hits and a change without a timestamp (as in a fresh checkout) misses correctly. The content hash is the same key on every machine for the same lockfile, which is what lets the cache be shared across the runners.
# the cache key is the lockfile hash + the OS + the runtime version
- uses: actions/cache@v4
with:
path: ~/.pnpm-store
key: pnpm-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}
Restore before the install step
Restore the cache before the install, so the install finds the packages in the cache and skips the download. The order matters: a restore after the install is useless, because the install already downloaded. The restore step precedes the install, and the install uses the restored cache transparently.
The install should use the lockfile’s frozen mode, so it installs the exact resolved set and fails if the lockfile would change. A frozen install with the restored cache is the fast, correct path: the packages are the resolved ones, and they are restored rather than downloaded.
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with: { path: ~/.pnpm-store, key: "pnpm-${{ runner.os }}-${{ hashFiles('**/pnpm-lock.yaml') }}" }
- run: pnpm install --frozen-lockfile # uses the restored cache and the lockfile
Include the platform and the runtime version
The key includes the OS and the runtime version, so a cache built on one platform is not restored on another. The native dependencies and the binaries differ by OS and by the runtime version, so a cache from Linux is wrong on Windows and a cache from one Node version is wrong on another. The OS and the version belong in the key.
A cache restored across the platforms would install the wrong binaries, which fails or misbehaves at runtime, and the failure is subtle. The platform and the version in the key prevent it, at the cost of a separate cache per platform, which is the correct trade. The key is specific enough that a hit is safe.
Choose the cache scope and the pruning
Cache the package manager’s store, such as the pnpm store or the npm cache, which is the downloaded packages and is shared across the lockfile versions, so a change to one package still reuses the others. Caching the installed tree, such as node_modules, is faster when the lockfile is unchanged, but it is invalidated by any lockfile change and is larger. Choose by the trade.
Prune the cache so it does not grow without bound. A store accumulates every package ever downloaded, and an unbounded cache consumes the cache budget and evicts the useful entries. A cache action that evicts the old entries on a new key, or a periodic cleanup, keeps the store proportional to the current dependencies.
Test the hit and the invalidation
Test that a second run with the same lockfile hits the cache and installs fast, by running twice and asserting the second hits. Test that a lockfile change misses and reinstalls, by changing a dependency and asserting the cache misses. Test that a change of the OS or the runtime version misses, so the platform-specific cache is separate.
# first run misses and populates the cache; second run hits
ci run # cache miss, install, store
ci run # cache hit, install from the store (fast)
# change a dependency: miss again
pnpm add left-pad && ci run # cache miss, the lockfile changed
Common pitfalls
Keying the cache on a timestamp so a change without a touch misses or a touch without a change hits, restoring after the install so the cache is useless, omitting the OS or the runtime version so a wrong platform’s binaries are installed, an unbounded store that evicts the useful entries, and a non-frozen install that resolves differently from the lockfile are the recurring mistakes. A content-hash key with the platform and the version, a restore before the install, a frozen install, and a pruned store address them.
Conclusion
CI dependency caching makes the run fast by restoring the dependencies instead of downloading them. Key the cache on the lockfile’s content hash plus the platform and the runtime version, restore before the install, install in the frozen mode, and prune the store. Self-check by asserting the second run hits and installs fast, and that a lockfile change misses. Then the CI spends its time on the build, not the download.
Sources: GitHub Actions: Caching dependencies, pnpm: CI and caching, npm: Cache and CI.
FAQ
- What key should the cache use?
- The lockfile's content hash, plus the OS and the runtime version. The lockfile hash invalidates when the dependencies change, and the OS and the version ensure a cache built with different binaries is not restored, which would install the wrong packages.
- Restore the cache or the download cache?
- Both are useful. The package-manager's cache directory, such as the pnpm store or the npm cache, restores the downloaded packages, and the node_modules directory restores the installed tree. Caching the store is smaller and safer across the lockfile changes; caching the installed tree is faster when the lockfile is unchanged.
Report a problem with this articleOpens a pre-filled issue on GitHub.