How to Reduce CI Minutes With Caching
Every minute you spend re-downloading and rebuilding unchanged inputs is a minute you pay for; caching removes it.
Cache the dependency store and any deterministic build output keyed on the lockfile. A cache hit turns a multi-minute install into seconds.
Steps
- Cache the package store keyed on a hash of the lockfile.
- Add
restore-keysso a near-miss still saves most of the work. - Confirm the hit rate in logs; a low rate means the key is too specific.
Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
restore-keys: |
npm-
- run: npm ciGotchas
- Cache restore and save themselves take time; only cache stores big enough to pay off.
- A key that includes the commit SHA never hits; key on the lockfile instead.
- Cache storage is metered and evicted at 10 GB per repo, so do not cache the world.
Related guides
How to Cut CI Cost With Path FiltersStop GitHub Actions from billing minutes on irrelevant changes by adding paths and paths-ignore filters, so a…
How to Reduce CI Storage Cost for Artifacts and CachesLower GitHub Actions storage cost by shortening artifact retention and keeping caches under the per-repo limi…