How to Schedule a Cache Warm-Up on the Default Branch in GitHub Actions
Scheduled runs execute on the default branch, which is exactly where a shared cache must live to be reused by PRs.
Caches are scoped to a branch, and PR runs read caches from the base branch. A cron job on the default branch reinstalls dependencies so the cache stays populated and unexpired.
Steps
- Schedule a daily
cron(schedule runs on the default branch by design). - Restore and save the cache keyed on the lockfile hash.
- Run
npm ciso a save happens on a cache miss.
Workflow
.github/workflows/ci.yml
on:
schedule:
- cron: '0 5 * * *'
jobs:
warm-cache:
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
- Caches unused for seven days are evicted; a daily warm-up keeps them alive.
- PR runs can read base-branch caches but cannot write them, so the warm-up must run on the default branch.
Related guides
How to Run a Workflow on a Cron Schedule in GitHub ActionsTrigger a GitHub Actions workflow on a recurring timetable with on.schedule and a five-field POSIX cron expre…
How to Avoid the Scheduled-Runs-Run-on-Default-Branch-Only Gotcha in GitHub ActionsUnderstand why GitHub Actions schedule triggers only use the workflow file on the default branch, and how to…