Skip to Content
Mengty Learns
CI/CD

Caching That Actually Works in CI/CD

Speed up GitHub Actions with pnpm store caching and Docker layer caching (cache-from/to gha), design stable cache keys, and know when caching hurts.

how-tointermediateTested with pnpm 11.6.0, github_actions 2026-11 (docs)Updated Nov 27, 2026

TL;DR

Cache inputs that are slow to fetch and deterministic to key: the pnpm store (via actions/setup-node with cache: 'pnpm') and Docker BuildKit layers (type=gha,mode=max). Never cache node_modules directly, always hash your lockfile in the key, and skip caching if restore/save takes longer than recomputing.

Why it exists

Fresh GitHub Actions runners start with an empty disk on every job. Re-downloading 750+ npm packages or rebuilding a multi-stage Docker image from apt-get on every pull request wastes minutes of runner time and feedback loops.

Mental model

A CI cache is a key-value archive (tar.zst) stored next to your repo:

  • Exact key hit: restores the exact artifact for that lockfile/Dockerfile hash.
  • Prefix restore-keys: falls back to the newest partial match (useful for Docker/Apt, dangerous if your install step doesn't prune stale files).
  • Eviction: GitHub Actions caps total cache size per repository (10 GB) and evicts unused entries after 7 days.

Do it

1. pnpm store cache in GitHub Actions

Pin actions to full commit SHAs (supply-chain best practice) and let setup-node handle the pnpm store key automatically:

# .github/workflows/ci.yml (example; not run directly)
steps:
  - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
  - uses: pnpm/action-setup@a7487c7e89a18df2991f7f222e4898a00d66ddda # v4.1.0
  - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
    with:
      node-version: "22"
      cache: "pnpm"
  - run: pnpm install --frozen-lockfile

2. Docker BuildKit layer cache (type=gha)

Use docker/setup-buildx-action and docker/build-push-action with mode=max so intermediate multi-stage layers (deps, builder) are cached too—not just the final runner stage:

# Docker BuildKit GHA cache (not run directly)
- uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2e # v3.10.0
- uses: docker/build-push-action@14487ce63c7a62a4a324672d2e86b94230dd13d3 # v6.15.0
  with:
    context: .
    push: false
    tags: web:ci
    cache-from: type=gha
    cache-to: type=gha,mode=max
Tip:

Notice mode=max: the default mode=min only exports layers from the final image stage, which means a multi-stage builder stage misses the cache on every run.

Gotchas

  • Caching node_modules: breaks native addons across Node versions and leaves orphan packages when dependencies are removed. Cache the pnpm store instead.
  • Forgetting mode=max in Docker: multi-stage builds will rebuild deps and builder every time unless mode=max is set.
  • When caching hurts: if your cache archive is 1.5 GB and takes 45 seconds to download/upload while pnpm install from registry takes 20 seconds, caching makes CI slower and burns your 10 GB repo quota.
  • Cross-PR cache isolation: pull requests from forks cannot write to the base branch cache, and feature branches cannot read caches created by other feature branches (only main and their own branch).

Production checklist

  • Key dependency caches on hashFiles('**/pnpm-lock.yaml') (automatic with setup-node cache: 'pnpm')
  • Use --frozen-lockfile so CI never silently mutates the lockfile
  • Set cache-to: type=gha,mode=max for multi-stage Dockerfiles
  • Measure job duration with and without cache—remove caches that don't save at least 20–30 seconds

Check yourself

Check Yourself

1. Why should multi-stage Docker builds in GitHub Actions use cache-to: type=gha,mode=max instead of the default mode=min?

2. You cache ~/.pnpm-store with a static key 'pnpm-store' and no lockfile hash. What goes wrong?

Sources