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.
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-lockfile2. 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=maxNotice 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=maxin Docker: multi-stage builds will rebuilddepsandbuilderevery time unlessmode=maxis set. - When caching hurts: if your cache archive is 1.5 GB and takes 45 seconds to download/upload while
pnpm installfrom 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
mainand their own branch).
Production checklist
- Key dependency caches on
hashFiles('**/pnpm-lock.yaml')(automatic withsetup-nodecache: 'pnpm') - Use
--frozen-lockfileso CI never silently mutates the lockfile - Set
cache-to: type=gha,mode=maxfor 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
Related Notes
Pipeline Anatomy: Triggers, Jobs, Cache, Artifacts
How a CI/CD pipeline turns a git push into parallel jobs, cached dependencies, build artifacts, and a deployment gate.
OIDC in CI Instead of Long-Lived Keys
Use GitHub Actions OpenID Connect (OIDC) to mint short-lived cloud credentials on AWS, GCP, and Azure instead of storing static IAM keys in secrets.