Sub topic 3
Caching with actions/cache
This chapter assumes you can write a basic workflow file under .github/workflows, read YAML, and run a package manager such as npm. You should also know that a workflow is made of jobs, jobs are made of steps, and each job runs on its own fresh runner.
What the action does
The actions/cache action stores files from a runner in GitHub's cache storage and restores them in later runs. A fresh runner starts with an empty disk. Without a cache, every run downloads every dependency again. With a cache, the download folder comes back before the install step, so the install has far less to fetch.
The action does two things in one step:
- Restore. When the step runs, it looks for a cache entry that matches
key. If it finds one, it unpacks the files intopath. - Save. When the job finishes successfully, a post step saves
pathunderkey. It skips the save if the exact key already existed.
A first working example
This workflow caches npm's download cache in ~/.npm.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- run: npm ci
- run: npm testThe cache step must come after checkout, because hashFiles reads files from the workspace. It must come before npm ci, so the files are in place when the install runs.
The inputs
| Input | Purpose |
|---|---|
| `path` | Files or folders to save and restore. It can be a list. |
| `key` | The exact name of the cache entry. |
| `restore-keys` | Optional prefixes to try when `key` has no exact match. |
The example caches ~/.npm, not node_modules. The npm cache holds downloaded packages, and npm ci deletes node_modules before it installs. Cache the folder your package manager uses for downloads, and let it build the final install.
Reading the key
The key is ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}. It has three parts:
runner.osisLinux,Windows, ormacOS. Caches built on one system can be wrong on another, so the key separates them.npmis a fixed label that tells entries apart by purpose.hashFiles(...)produces a hash of every file matching the pattern. When the lock file changes, the hash changes, so the key changes.
A changed key means a miss. That miss is useful: new dependencies are installed, and the post step saves a fresh entry under the new key. If the lock file is unchanged, the key matches and the cache is restored.
Common errors
- Caching before checkout.
hashFilesfinds no files and returns an empty string. Every run then uses the same key, and the cache never updates. - A pattern that matches nothing. Check that the path in
hashFilesmatches where your lock file lives. - Expecting an update under the same key. An entry cannot be overwritten. If the key is unchanged, the old contents stay. Put the thing that changes into the key.
- Failed jobs. The post step saves only when the job succeeds, so a failing job saves nothing.
Exercise
A workflow caches ~/.npm with the key ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}. You add one dependency, which changes package-lock.json. What happens to the cache on the next run, and why?
Show answer
The hash changes, so the key changes and the exact key is not found, which is a miss. With restore-keys set, the step may restore an older entry that starts with Linux-npm-. Either way, npm ci installs the new dependency, and because the exact key missed, the post step saves a new entry under the new key if the job succeeds.