GitHub Actions cache key builder
Pick your package manager and get an actions/cache step with the right paths, a lockfile-based key and a restore-keys fallback so installs stay fast and caches never go stale.
Workflow step
- uses: actions/cache@v4
with:
path: |
~/.npm
key: ${{ runner.os }}-npm-v1-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-v1-
Exact key: ${{ runner.os }}-npm-v1-${{ hashFiles('**/package-lock.json') }}. Fallback prefix: ${{ runner.os }}-npm-v1-
Everything runs in your browser; nothing you type is sent anywhere.
How to use it
- Choose your package manager. The cache paths and lockfile pattern fill in automatically.
- Decide whether the key includes the OS and CPU architecture, and add a version salt you can bump to bust the cache.
- Copy the YAML into your workflow before the install step.
Frequently asked questions
What makes a good cache key?
A prefix that identifies the OS, tool and a salt, followed by a hash of the lockfile. When the lockfile changes the key changes, so you get a fresh cache.
What are restore-keys for?
If there is no exact match, GitHub restores the most recent cache whose key starts with a restore key. Your install then only fetches what changed.
How do I invalidate a bad cache?
Change the version salt in the key, for example from v1 to v2. Old entries age out after 7 days without use.
How big can the cache be?
A repository gets 10 GB of cache in total by default, and least recently used entries are evicted when it fills.
Related guides
- Cache Cargo and Rust Builds in GitHub ActionsCache the Cargo registry, git dependencies and target directory in GitHub Actions with Swatinem/rust-cache or actions/cache, and avoid bloated caches.
- Cache Go Modules and Build Cache in GitHub ActionsCache Go modules (GOMODCACHE) and the build cache (GOCACHE) in GitHub Actions with setup-go or actions/cache. Keys, go.sum, and verifying hits.
- Cache Gradle Builds in GitHub ActionsCache Gradle dependencies and build cache in GitHub Actions with gradle/actions/setup-gradle or actions/cache. Keys, read-only PRs, cleanup and checks.
- Cache npm Dependencies in GitHub ActionsCache npm dependencies in GitHub Actions with setup-node or actions/cache. Working YAML, the right key, why not node_modules, and how to confirm a hit.
Embed this calculator on your site
Free to embed on blogs and docs. Paste this HTML where you want it to appear:
<iframe src="https://compiler.dev/embed/actions-cache-key-builder" title="GitHub Actions cache key builder" width="100%" height="560" style="border:1px solid #e5e7eb;border-radius:12px" loading="lazy"></iframe>
<p style="font-size:12px">Calculator by <a href="https://compiler.dev/tools/actions-cache-key-builder">compiler.dev</a></p>Related tools
- GitHub Actions workflow YAML checkerPaste a GitHub Actions workflow and catch missing runs-on, unpinned actions, pull_request_target risks, missing permissions and bad needs.
- CI minutes forecasterForecast monthly CI minutes and GitHub Actions cost from pull requests per day, pushes per PR, jobs per run and minutes per job. Plan and runner aware.
- Test sharding plannerPaste test durations and split them into balanced shards. See each shard's tests, the expected wall time and the speedup before you change your CI.
- Dependabot config generatorGenerate a dependabot.yml for npm, pip, Docker, GitHub Actions, Go, Cargo and more, with schedule, grouping, labels and reviewers. Validated, copy-ready.