GitHub Actions cache not working consistently #200314
-
🏷️ Discussion TypeBug 💬 Feature/Topic AreaCode quality Discussion DetailsI'm using actions/cache@v4 to cache node_modules in my workflow. It works fine for the first run, but subsequent runs are taking just as long. I've verified the cache key matches and restore-keys are set properly. Has anyone else experienced this? Is there some limitation I'm missing? The cache size is around 800MB and my runner is ubuntu-latest. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
💬 Your Product Feedback Has Been Submitted 🎉 Thank you for taking the time to share your insights with us! Your feedback is invaluable as we build a better GitHub experience for all our users. Here's what you can expect moving forward ⏩
Where to look to see what's shipping 👀
What you can do in the meantime 💻
As a member of the GitHub community, your participation is essential. While we can't promise that every suggestion will be implemented, we want to emphasize that your feedback is instrumental in guiding our decisions and priorities. Thank you once again for your contribution to making GitHub even better! We're grateful for your ongoing support and collaboration in shaping the future of our platform. ⭐ |
Beta Was this translation helpful? Give feedback.
-
|
I faced this exact issue a few weeks ago. Here are the most common reasons and fixes:
First, look at your workflow logs. Do you see Cache restored from key or Cache not found? If it says Cache not found, your key isn't matching. Fix: Make sure package-lock.json exists and is being committed. If it's missing, hashFiles('**/package-lock.json') returns empty and cache misses every time.
The official docs actually recommend against caching node_modules. It can cause issues with Node version mismatches and npm ci. Better approach: Cache ~/.npm instead: - name: Cache npm dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-This caches the downloaded tarballs, not the installed modules. npm install becomes much faster.
If any step after npm install fails, the cache save step (post step) gets skipped automatically. So even if cache restored properly, it won't save for next run. Fix: Add if: always() to your cache step or ensure subsequent steps don't fail.
GitHub Actions has a 10 GB cache limit per repository. If you're hitting this, older caches get evicted. Check your total cache usage in: Repository Settings > Actions > Caches.
Make sure you're using: · actions/cache@v4 Older versions had some cache restore bugs. |
Beta Was this translation helpful? Give feedback.
I faced this exact issue a few weeks ago. Here are the most common reasons and fixes:
First, look at your workflow logs. Do you see Cache restored from key or Cache not found? If it says Cache not found, your key isn't matching.
Fix: Make sure package-lock.json exists and is being committed. If it's missing, hashFiles('**/package-lock.json') returns empty and cache misses every time.
The official docs actually recommend against caching node_modules. It can cause issues with Node version mismatches and npm ci.
Better approach: Cache ~/.npm instead: