Sub topic 13
Checking Cache Hits and Troubleshooting Misses
This chapter assumes you can already add actions/cache to a workflow, build a key with hashFiles, and use restore-keys. It also assumes you know the scope and eviction rules from the previous chapter. Here you learn to tell whether a cache was used, read the evidence in the logs, and find the cause when it wasn't.
Three possible outcomes
A cache step ends in one of three states.
| Outcome | What happened | Typical log line |
|---|---|---|
| Exact hit | An entry matched the `key` exactly. | `Cache restored from key: ...` |
| Partial hit | No exact match, but an entry matched a `restore-keys` prefix. | `Cache restored from key: ...` (a different key from the one you asked for) |
| Miss | Nothing matched. | `Cache not found for input keys: ...` |
The two restore outcomes print the same kind of line, so you must compare the restored key with your primary key. Don't assume that "restored" means "exact."
A partial hit is useful, since the package manager only fetches what changed. It also changes what happens at the end of the job. After a partial hit, the post step saves a new entry under the primary key. After an exact hit, the post step skips saving, because the key already exists and entries are immutable.
Reading the cache-hit output
actions/cache exposes the output cache-hit. It is 'true' only when the primary key matched exactly. A partial hit and a miss both fail that test. Outputs are strings, so compare against the text 'true':
name: Cache report
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
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
env:
EXACT: ${{ steps.npm-cache.outputs.cache-hit }}
run: |
if [ "$EXACT" = "true" ]; then
echo "Exact cache hit" >> "$GITHUB_STEP_SUMMARY"
else
echo "No exact hit. Partial hit or miss." >> "$GITHUB_STEP_SUMMARY"
fi
- run: npm ci
- run: npm testWriting to $GITHUB_STEP_SUMMARY puts the result on the run's summary page. You can see the outcome of every matrix job without opening each log.
cache-hit can't separate a partial hit from a miss. The split restore action actions/cache/restore adds two outputs for that:
cache-primary-keyis the key you requested.cache-matched-keyis the key that was actually restored. It is empty on a miss.
steps:
- uses: actions/checkout@v4
- id: restore
uses: actions/cache/restore@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Classify result
env:
WANTED: ${{ steps.restore.outputs.cache-primary-key }}
GOT: ${{ steps.restore.outputs.cache-matched-key }}
run: |
if [ -z "$GOT" ]; then
echo "miss"
elif [ "$GOT" = "$WANTED" ]; then
echo "exact hit"
else
echo "partial hit from $GOT"
fi
- run: npm ciWhat to look for in the logs
Open the cache step and its post step. The post step appears as "Post Run actions/cache@v4" at the end of the job. Four messages cover most cases:
Cache not found for input keys: ...lists the primary key and each restore key. Copy these strings and compare them with what you expect.Cache restored from key: ...shows the key that matched. Compare it with the primary key.Cache saved with key: ...means the post step uploaded a new entry.Failed to save: Unable to reserve cache with key ..., another job may be creating this cachemeans an entry with that key already exists or is being created. This is normal when two matrix jobs share a key.
The post step also prints the cache size. A size of a few hundred bytes usually means your path matched almost nothing.
Troubleshooting misses
Work through the causes in this order, from most to least common.
1. The key changes on every run
Anything that varies between runs in the key gives a fresh key each time, so nothing ever matches. The usual causes are github.sha, github.run_id, a timestamp, or a hash of files that the build itself modifies. Use only stable inputs: the OS, the tool version, and a hash of the lock file.
A related mistake is hashing a file that changes during the job. If hashFiles runs in the key input before the build, it sees the checked-out files. If you compute a hash after npm ci rewrites something, the saved key differs from the restore key.
2. hashFiles matches nothing
If the glob matches no files, hashFiles returns an empty string. Your key then ends in a dash, such as Linux-npm-. That key still works, but it never changes when dependencies change, so the cache gets stale. A wrong path in a monorepo is the usual reason. Print the hash to check it:
steps:
- uses: actions/checkout@v4
- name: Show key inputs
env:
LOCK_HASH: ${{ hashFiles('**/package-lock.json') }}
run: |
echo "runner.os=$RUNNER_OS"
echo "lock hash='${LOCK_HASH}'"
ls -la package-lock.jsonAn empty lock hash='' tells you the pattern is wrong. Remember that hashFiles only sees files inside the workspace, so it can't hash anything outside the checkout.
3. The cache was never saved
The post step saves only if the job succeeds. If a later step fails, no entry is written, and the next run misses again. This hides real progress: a long install that succeeded is thrown away because the tests failed. If you want to keep the cache after a failure, use the split actions. Put actions/cache/save in a step with if: always(), as shown in the chapter on splitting restore and save.
Also check whether the save was attempted at all. A missing "Cache saved" line, with no error, often means an exact hit happened.
4. The scope doesn't include the entry
A run can read caches from its own branch, its base branch, and the default branch. It can't read caches from sibling branches. A pull request restores from the base branch but writes to its own merge ref (refs/pull/N/merge). If you expect a feature branch to reuse the cache of another feature branch, it won't. The first run on a new branch usually misses unless the default branch has a matching entry. Pull requests from forks also don't get write access in the same way, so their caches stay isolated.
5. The entry was evicted
Entries unused for 7 days are removed. When a repository goes over its total size limit, the oldest entries are removed first. Large caches pushed by busy branches can force out the entries that the default branch needs. If you see misses on a quiet branch after a week or a burst of activity, check the list of caches (next section).
6. The path or environment differs
A cache entry has a hidden version made from the path list and the compression method. If you change path, old entries stop matching, even with the same key. The operating system also matters. Linux and Windows caches don't mix by default, which is why runner.os belongs in the key. Cache paths must exist on the runner, and ~ expands differently per OS. A path that doesn't exist at save time results in a warning and no entry.
7. Matrix jobs collide or diverge
In a matrix, the key must include everything that changes the cached content, such as OS and Node.js version. If two jobs share a key, the first to finish wins and the other gets the "Unable to reserve cache" message. Both then read an entry built for the wrong configuration. If the key includes matrix.node in the cache step but the lock file differs per configuration, check that each job's key is what you expect by printing it in a step.
Replace nothing above; the diagram below summarizes the order of checks.
Inspecting stored caches
The repository's Actions > Caches page lists every entry with its key, size, branch, and last-used time. Compare the listed key with the primary key in the log of the missing run. The GitHub CLI shows the same data:
gh cache list --limit 20
gh cache list --key Linux-npm- --sort last_accessed_at
gh cache delete Linux-npm-3f2a9cDeleting an entry is the way to replace one that has a bad value, since entries are immutable. You need the actions: write permission to delete, and actions: read to list. In a workflow, grant these under permissions and pass GH_TOKEN: ${{ github.token }} to the step.
A short checklist
- Read the restore line and compare the restored key with the primary key.
- Print
hashFilesoutput and confirm it isn't empty. - Confirm that an earlier run on this ref, its base, or the default branch saved an entry.
- Open Actions > Caches to confirm the entry still exists.
- Check that
path,runner.os, and matrix values match between the saving and restoring jobs.
Exercise
A matrix workflow tests Node.js 18, 20, and 22 on Ubuntu with the key ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}. The first run on a new branch shows "Cache restored from key: Linux-npm-abc123" in all three jobs, yet cache-hit is 'false'. The post steps print "Unable to reserve cache" for two jobs. What explains each observation, and what do you change?
Show answer
The primary key was probably Linux-npm-def456, and the restored key Linux-npm-abc123 came from a restore-keys prefix, so it was a partial hit. cache-hit is 'true' only on an exact match, so it reports 'false'. All three jobs share one key because it has no Node.js version. They all try to save under the same new key. The first job to finish reserves it, and the other two get "Unable to reserve cache". Add node${{ matrix.node }} to both the key and the restore key, for example ${{ runner.os }}-node${{ matrix.node }}-npm-${{ hashFiles('**/package-lock.json') }}. Then each configuration saves and restores its own entry.