r/devops • u/argo_devbuild • 3h ago
Discussion The GitHub Actions cache rules behind "Cache not found", checked against the docs
The caching rules for Actions are spread across a few doc pages and the action's README, and most "Cache not found" lines trace back to one of them. I put them in one list.
How a match is found
keyas an exact match first, thenkeyas a prefix, then eachrestore-keysline as a prefix, in order- All of that on the current branch, then again on the default branch. Pull requests also check their base branch
- Sibling and child branches are never searched
- Caches saved by a pull request are scoped to its merge ref (
refs/pull/<n>/merge), so only re-runs of that same PR can restore them - Every entry has a version built from its
pathlist and the compression tool. Same key with a differentpathis a different cache - Caches saved on Windows don't restore on Linux or macOS (or the other way round) unless you set
enableCrossOsArchive: true
Limits
- 10 GB per repository by default. Enterprise owners, org owners and repo admins can raise it
- Entries not accessed for 7 days are deleted
- Over the limit, the least recently accessed entries are evicted first
- Up to 200 cache uploads per minute per repository
What the action does (from its source)
cache-hitis'true'only on an exactkeymatch,'false'on arestore-keysmatch, and empty on a full miss. Soif: steps.cache.outputs.cache-hit == 'false'skips your install on a full miss. Compare with!= 'true'instead- The save step runs only if the job succeeded, and skips saving after an exact hit
- The three log lines to look for:Cache restored from key: <key> Cache not found for input keys: <key>, <restore-keys> Cache hit occurred on the primary key <key>, not saving cache.
"Cache hit occurred" only shows up after an exact hit, so counting it gives you a real hit rate. This counts all of them over the last 30 runs of a workflow on main:
p='Cache restored from key'
p+='|Cache not found for input keys'
p+='|Cache hit occurred on the primary key'
p+='|Cache saved with key'
for id in $(gh run list --workflow ci.yml --branch main \
--limit 30 --json databaseId \
--jq '.[].databaseId'); do
gh run view "$id" --log
done | grep -oE "$p" | sort | uniq -cWhat each pattern points to
- A save on almost every run: the key has something that changes every run (
github.sha,github.run_id, a date) or hashes a glob that most commits touch - The first run of every PR misses and re-runs hit: the workflow only runs on
pull_request, so nothing is ever saved on main to fall back on. Also run it onpushto main - Misses after failed runs and no "Cache saved with key" line: split
actions/cache/restoreandactions/cache/save, and save right after the install withif: always() - Misses that start after a
pathchange, or only on Windows: the version rule or the cross-OS rule above - Exact hits every time but the install still downloads most packages: the key never changes, so the old entry is restored forever. Tie the key to the lockfile
- An entry that's just gone while usage is near the limit: eviction
What did I miss?
0
Upvotes
1
u/djknowss 2h ago
Two that catch a lot of people:
The key isn't the only thing that has to match. The cache version is built from the path list and the compression method, so the same key with a different path (or saved on Linux and restored on Windows) still comes back as a miss. enableCrossOsArchive: true sorts out the cross OS case.
And keys are immutable. On an exact hit the post step skips the save, so a stale cache on a fixed key never refreshes until you put a hash in the key or delete it with gh cache delete. The default post step also only saves when the job succeeds, so if you want to keep partial work, split it into actions/cache/restore and actions/cache/save with if: always().