Sub topic 9
Giving Each Matrix Job Its Own Cache Key
This chapter assumes you can write a basic workflow, that you know how actions/cache uses key and restore-keys, and that you have seen strategy.matrix. It shows how to combine the two.
Why matrix jobs share a key by accident
A matrix expands one job definition into many jobs that run at the same time. If the cache step uses a fixed key, every one of those jobs asks for the same cache entry.
That causes two problems:
- Caches are immutable. The first job to finish saves the entry. The other jobs find it already exists and cannot overwrite it.
- Jobs restore content that was saved by a different configuration. A cache built on Linux with Node.js 18 may not suit Windows with Node.js 22.
The fix is to put every matrix value that changes the cached content into the key.
Adding matrix values to the key
Use runner.os for the operating system. Use matrix.<name> for each other variable, such as the Node.js version.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node${{ matrix.node }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node${{ matrix.node }}-npm-
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testThis matrix creates six jobs, so it creates six cache entries. For example, the Linux, Node.js 20 job uses a key shaped like Linux-node20-npm-<hash>.
The key has the same parts in the same order every time:
- The platform (
runner.os). - The matrix variables.
- A label for the tool (
npm). - The hash of the lock file.
Putting the changing part last lets restore-keys work as a prefix.
Keep restore-keys as specific as the key
A restore-keys entry is matched as a prefix. If you write only npm-, a Node.js 22 job can fall back to a Node.js 18 cache. Restore keys should keep the matrix values and drop only the hash. Then a changed lock file still gets a nearby cache, but never one from a different configuration.
Common mistakes
- Leaving out a variable. If only
runner.osis in the key, all three Linux jobs share one entry. Whichever finishes first saves it. - Using
matrix.osinstead ofrunner.os. This works, but the value isubuntu-latest. That label can point to a newer image later, and the key will not change.runner.osis a stable value such asLinux. - Adding a variable but not using it. Variables added with
include, such asexperimental: true, only need to be in the key if they change what gets cached. - Over-splitting. Each extra variable multiplies the number of entries. Cache storage is limited and old entries are evicted, so splitting by values that do not change the content wastes space.
Deciding what belongs in the key
Ask what would make the cached files wrong or useless. A pure download cache, such as the npm cache, is mostly independent of the Node.js version, so sharing it can be fine. Compiled output and native modules depend on the platform and the runtime version. Include those values for those caches.
Exercise
A matrix has os: [ubuntu-latest, windows-latest] and node: [20, 22]. The cache key is ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}. How many distinct cache entries can the Linux jobs end up using, and what should you change if Node.js version matters?
Show answer
The two Linux jobs share one entry, because their keys are identical. Add node${{ matrix.node }} to both key and restore-keys, giving ${{ runner.os }}-node${{ matrix.node }}-npm-${{ hashFiles('**/package-lock.json') }}. Each Linux job then has its own entry.