r/devops • u/argo_devbuild • 2d 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
3
u/Jan_Stepan 1d ago
I think the exact-hit count is off. You're counting a message at the end of the job. If the cache hits and a test fails later, the hit still happened, but that message never gets printed.
With
id: cacheon the cache step, I'd add this right after it, before the tests run:Then you can count the
cache_hit=trueoutput lines even from jobs that failed.2
u/Feisty-Piglet7758 1d ago
Two rules missing from the list. On a prefix match it is the most recent matching cache that gets restored, not the first one, which is why a restore-keys setup quietly hands you last week's entry. And the docs are explicit that a pull request can read caches on the base branch, forks included, so anything sitting in a cached path is readable by anyone who can open a PR, credentials in particular.
1
u/BetterSWE_Official 1d ago
Useful list. The practical fix that falls out of the branch rules is to make sure main actually saves the cache, with a workflow on push to main or a nightly schedule. Then every PR and feature branch can fall back to it. A lot of repos only run CI on pull_request, so main never saves anything and every new branch starts cold.
1
u/NoDevelopment206 1d ago
one more thing that bites people is caching node_modules directly. if you use npm ci it wipes that folder first so the cache does nothing. caching the package manager folder like ~/.npm and keying on the lockfile hash works a lot better. did you see big time savings after fixing your keys?
1
u/hubhinge52 20h ago
same hole here, failed runs read as misses, we push cache-hit as a metric now