Skip to content
Latchkey

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 ci so 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 ci

Gotchas

  • 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

Run this faster and cheaper on Latchkey managed runners. Start free →