Sub topic 13
Checking cache hits and troubleshooting misses
This chapter assumes you can already add actions/cache to a workflow and build a key with hashFiles. It shows how to tell whether a run restored a cache and how to find out why one didn't.
Reading the cache result
The actions/cache step reports its result in two places: the log and the cache-hit output.
In the log, the step prints one of these lines:
Cache restored from key: ...means a cache was restored. This can be an exact match or arestore-keysfallback.Cache not found for input keys: ...means nothing matched.
The post-job step at the end of the run then prints either Cache saved with key: ... or a message that the key already exists.
The cache-hit output is narrower than the log message. It is 'true' only when the exact key matched. A fallback through restore-keys restores files but does not set the output to 'true'.
steps:
- uses: actions/checkout@v4
- id: npm-cache
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Report cache result
run: echo "Exact hit: ${{ steps.npm-cache.outputs.cache-hit }}"
- run: npm ciCompare against 'true' rather than testing for an empty value. The output is a string, and it can be empty or 'false' when there was no exact hit.
Following the decision
Common causes of misses
| Symptom | Likely cause | Fix |
|---|---|---|
| Miss on every run | The key contains something that changes each run, such as `github.sha` or a timestamp | Build the key only from inputs that define the dependencies |
| Key ends in `npm-` with no hash | `hashFiles` matched no files, so it returned an empty string | Check the glob. A lock file in a subfolder needs a pattern like `**/package-lock.json` |
| `Path Validation Error` warning | The `path` does not exist on the runner | Use the directory the package manager really writes to, and check it after the install step |
| Hit on one branch, miss on another | Caches are scoped to a branch. A branch can read its own caches and those of its base branch or the default branch, but not those of sibling branches | Run the workflow on the default branch so other branches can fall back to its cache |
| Cache vanished | Entries not accessed for 7 days are evicted. Eviction also starts when the repository exceeds its total size limit, 10 GB by default | Reduce the cached paths and keep the workflow running regularly |
| New dependencies, but the old cache keeps being restored | Cache entries are immutable. An exact key hit never re-saves | Change the key when the inputs change, which is what `hashFiles` is for |
| Works on Linux, misses on Windows | `runner.os` is part of the key, so each OS has its own entry | Expected behavior. Each OS builds its own cache the first time |
A troubleshooting routine
- Open the cache step's log and note the exact key it printed.
- Compare that key with the one from the previous run. If they differ, find which expression changed.
- Look at the repository's Actions tab, then Caches, to see which keys exist, their sizes, and which branch owns them.
- Confirm that the
pathhas contents afternpm ci, so the save has something to store. - If you expect a fallback, check that the
restore-keysprefix matches the start of an existing key.
Exercise
A workflow uses the key ${{ runner.os }}-npm-${{ github.sha }} and a restore key of ${{ runner.os }}-npm-. The log always shows "Cache restored" but cache-hit is never 'true', and a new cache is saved every run. Why, and how do you fix it?
Show answer
Every commit has a new SHA, so the exact key never matches. The restore key finds the newest older cache, which restores files but leaves cache-hit as 'false'. The post step then saves a new entry each time. Change the key to use hashFiles('**/package-lock.json') so it changes only when the dependencies change.