actions/cache: same key, cache hit on push but always a miss on pull_request from the same branch #206820
Replies: 4 comments 2 replies
|
This is expected cache scoping, not a broken key. What’s going on
A
It does not restore caches that only exist on a sibling/head branch from a prior Also: caches created by a Docs: Restrictions for accessing a cache What to do
If you paste your exact |
|
on: jobs: |
|
The cache-scope explanation above matches GitHub's documented behavior. A cache created by the push to There's also a second issue in the workflow that is easy to miss: you're caching For npm, I'd use - uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test
So I think there are actually two separate behaviors here: the PR miss is explained by cache scope, while the current |
|
The cache key itself looks fine. The main issue is the cache scope: a There’s also another thing worth changing: you’re caching For this setup, I'd let - uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm testThis keeps So I think there are actually two separate issues here: cache scope explains the miss, while caching |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
🏷️ Discussion Type
Question
💬 Feature/Topic Area
Actions Cache
Discussion Details
Hi all,
I'm probably missing something obvious, but I've been staring at this for a while and can't work it out. I've put together a minimal public repo that reproduces it: https://github.com/TorbenGuenther132/node-cache-demo
I have a Node project with a pretty standard workflow that caches
node_modules. It runs on bothpushandpull_request. Caching works perfectly on push, but everypull_requestrun reports a cache miss, even though the logs show the exact same cache key in both runs.The workflow (
.github/workflows/ci.yml):What I see:
Push to
feature/add-ci(run Welcome to GitHub product feedback #1): first run logs a miss, then savesLinux-node-c732972...e19abdat the end. Fine.Second push to the same branch (run Feature Request: Select a column when adding an issue to a project #2): the cache is restored from that same key. So the key is stable and the cache exists.
Open a PR from
feature/add-ciintomain. Thepull_requestrun logs:Same hash as the push run, character for character. The
restore-keysprefixLinux-node-finds nothing either.So the same commit, same lock file, same key, and the push run hits while the PR run misses.
Things I've already checked:
feature/add-ci.The only difference I can spot in the logs is the ref: push runs are on
refs/heads/feature/add-ci, and the PR run is onrefs/pull/1/merge. I couldn't find anything conclusive in the docs about whether that matters for cache scoping.Is this expected? If so, what’s the right way to configure it so PR runs actually benefit from the cache? Right now every PR run does a completely cold
npm ci, which is exactly what I was trying to avoid. I don’t mind an occasional miss after the lockfile changes, but seeing the cache skipped on every run makes me think the key or restore logic is wrong. Tropical Casino https://tropical-casino.com/ gives me the same reaction when game loading ignores saved session state: technically the flow still works, but you lose the whole advantage of keeping reusable data around. Is there a recommended cache key pattern for this setup?Thanks in advance.
All reactions