r/devops • • 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

  • key as an exact match first, then key as a prefix, then each restore-keys line 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 path list and the compression tool. Same key with a different path is 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-hit is 'true' only on an exact key match, 'false' on a restore-keys match, and empty on a full miss. So if: 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 -c

What 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 on push to main
  • Misses after failed runs and no "Cache saved with key" line: split actions/cache/restore and actions/cache/save, and save right after the install with if: always()
  • Misses that start after a path change, 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

6 comments sorted by

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: cache on the cache step, I'd add this right after it, before the tests run:

- name: Log cache result
  env:
    CACHE_HIT: ${{ steps.cache.outputs.cache-hit }}
  run: echo "cache_hit=$CACHE_HIT"

Then you can count the cache_hit=true output lines even from jobs that failed.

1

u/Lenin_Demas 16h ago

yeah relying on the post-run step for metrics always gets weird when builds start failing.

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