Sub topic 5
Falling back with restore-keys
This chapter assumes you can read a workflow file and have already used actions/cache with a key built from hashFiles. If you haven't, read the chapters on actions/cache and cache keys first.
The problem with exact keys
A key such as ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} changes whenever the lock file changes. After you add one dependency, the new key matches nothing. The step reports a miss, and npm ci downloads every package again, even though 99% of them are unchanged.
restore-keys fixes this. It gives the cache action a list of fallback prefixes to try when the exact key misses.
How the lookup works
The action searches in this order:
- It looks for a cache whose name equals
keyexactly. - If there is none, it takes each line of
restore-keysin order. For each line, it looks for caches whose key starts with that prefix. - If several caches match one prefix, it picks the most recently created one.
- If nothing matches, the step is a miss and
pathstays empty.
The first exact match or the first prefix that finds a cache ends the search. Later prefixes are not tried.
A complete example
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- run: npm ci
- run: npm testSuppose the lock file changes. The exact key misses, so the action restores the newest cache whose key starts with Linux-npm-. npm ci then finds most packages in ~/.npm and downloads only the new ones. When the job succeeds, the action saves a new cache under the new exact key, so the next run gets an exact hit.
Partial hits and `cache-hit`
A restore through restore-keys is a partial hit. The output cache-hit is 'true' only for an exact match. For a partial hit it is 'false', even though files were restored. Because the post step saves a cache whenever the exact key missed, a partial hit still produces a fresh entry at the end of the job.
Choosing prefixes
Order the prefixes from most specific to least specific. Each prefix should only match caches that are safe to reuse.
restore-keys: |
${{ runner.os }}-node20-npm-
${{ runner.os }}-npm-Keep these points in mind:
- Include the operating system. A cache from Linux can break a Windows job because native modules and path formats differ.
- Don't use a prefix so short that it matches unrelated caches, such as
npm-. - Fallbacks suit caches that tolerate extra or stale content, like a package download cache. They are risky for build output, because stale files can leak into the result.
- The cache can only grow stale in one direction. A restored fallback is saved again under the new key with whatever the job leaves in
path, so old entries can pile up unless the tool prunes them.
Common mistakes
- Writing
restore-keysas the only key.keyis required.restore-keysis only a fallback. - Using a prefix that never matches. If the prefix doesn't match the start of your
keyformat, the fallback never fires. Compare them character by character. - Expecting a fallback to be saved. The save step uses
key, never a restore key.
Exercise
A workflow uses key ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} and restore-keys ${{ runner.os }}-pip-. You edit requirements.txt, and the run restores an older cache. What is the value of the cache-hit output, and which key is the new cache saved under?
Show answer
cache-hit is 'false', because only an exact key match sets it to 'true'. At the end of a successful job, the new cache is saved under the exact key built from the new requirements.txt hash, not under the restore key.