Sub topic 6
Caching dependencies and build output
This chapter assumes you can read a workflow file, know how to find step durations in the Actions UI, and have run npm ci or a similar install command. Caching is worth adding only after a step timing shows that installs or builds take a large share of the job.
Why caching helps
Each job starts on a fresh runner. Every npm ci, pip install or compile starts from nothing and downloads or rebuilds the same files as the last run. A cache stores those files in GitHub's cache storage and restores them at the start of the next job. The step still runs, but it has less to do.
The saving depends on the step. Check the step durations first. If the install takes 8 seconds of a 6-minute job, caching it will not matter.
The simplest option: setup actions
The setup actions for most languages have a built-in cache input. This is the example from the spine:
name: ci
on:
pull_request:
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- name: Run tests with timing
run: |
start=$(date +%s)
status=0
npm test || status=$?
echo "Tests took $(( $(date +%s) - start ))s" >> "$GITHUB_STEP_SUMMARY"
exit $statusWith cache: npm, setup-node caches npm's download directory, not node_modules. The key is built from the runner OS and a hash of package-lock.json. When the lockfile changes, the key changes and a new cache is saved.
npm ci still runs and still installs packages into node_modules. It copies them from the local download cache instead of fetching them over the network.
The concurrency block cancels older runs on the same ref. That is a separate saving, covered in a later chapter.
How a cache key works
The lookup follows these steps:
- The job computes a key, such as
Linux-npm-<hash of lockfile>. - If a cache with that exact key exists, it is restored. This is a cache hit.
- If not, the job may try a list of fallback prefixes (
restore-keys) and restore the closest match. - At the end of a successful job, the cache is saved under the exact key if no cache with that key existed.
A cache is never updated under an existing key. If you cache build output under a fixed key, it will stay stale. Put something that changes with the content into the key, usually a file hash.
Caching build output with actions/cache
Use actions/cache directly for things the setup actions do not cover, such as a compiler cache or a framework build directory.
- uses: actions/cache@v4
with:
path: .next/cache
key: next-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-${{ hashFiles('src/**') }}
restore-keys: |
next-${{ runner.os }}-${{ hashFiles('package-lock.json') }}-
next-${{ runner.os }}-The exact key changes whenever the source changes, so most runs miss it. The restore-keys prefixes still restore the previous build output, and the build tool reuses what it can. Then the job saves a fresh cache under the new key.
Common errors
- Caching
node_modulesdirectly. It can break when the Node version or operating system changes. Cache the package manager's download directory instead. - A key that never changes. The cache restores but is never refreshed, so it goes stale.
- A key that always changes. Every run misses and saves a new cache, which adds upload time and no benefit. Check the post-job step logs for "Cache saved" on every run.
- Expecting caches to cross branches freely. A run can restore caches from its own branch and from the default branch, but not from sibling branches.
- Assuming the cache is permanent. GitHub removes caches not accessed for 7 days and evicts the oldest when a repository exceeds its size limit, which is 10 GB by default.
Checking that it helped
Look at the install step in the Actions UI for a run with a cache hit and one with a miss. A hit shows "Cache restored from key" in the setup step. Compare the step durations. If the restore itself takes nearly as long as a fresh install, a large cache is costing you as much as it saves. Later chapters show how to compare across several runs.
Exercise
Your workflow caches build output with the key build-${{ github.sha }}. Each run restores nothing useful and saves a new cache. What is wrong, and what would you change?
Show answer
The commit SHA is unique per commit, so the exact key never matches and no run gets a hit. Use a key built from the files that determine the output, such as build-${{ runner.os }}-${{ hashFiles('package-lock.json') }}, and add restore-keys with a shorter prefix so a run can restore the most recent similar cache.
Exercise
A job takes 5 minutes. The install step takes 10 seconds and the test step takes 4 minutes. Should you add dependency caching first? Why?
Show answer
No. The install step is about 3% of the job, so caching can save only a few seconds at most. The test step is the slow part, so look at splitting tests across parallel jobs instead.