Skip to content
Latchkey

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-keys so 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 ci

Gotchas

  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →