How to Warm a Cache With a Scheduled Job in GitHub Actions
A scheduled job on main installs dependencies and saves the cache so it never ages out and every branch inherits it.
Caches unused for 7 days are evicted, and default-branch caches seed every branch. A nightly job that runs npm ci and saves the cache keeps it fresh and available, so the first PR of the day gets a warm hit instead of a cold install.
Steps
- Add
on.schedulewith a nightly cron. - Restore, install, then save the cache under the shared key.
- Run it on the default branch so all branches inherit the warm cache.
Workflow
.github/workflows/ci.yml
on:
schedule:
- cron: '0 4 * * *'
jobs:
warm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.npm
key: npm-${{ hashFiles('package-lock.json') }}
- run: npm ciGotchas
- Scheduled runs use the default branch, which is exactly where a shared cache should live.
- Latchkey managed runners keep these warm-up jobs cheap and auto-retry transient failures.
Related guides
How to Save a Cache Only on the Default Branch in GitHub ActionsWrite caches only from default-branch runs in GitHub Actions using the save sub-action with an if on github.r…
How to Work Within the 10GB Cache Limit and Eviction in GitHub ActionsUnderstand the 10GB per-repository GitHub Actions cache limit and least-recently-used eviction, and trim cach…