Sub topic 14
Splitting restore and save steps
This chapter assumes you can already write a workflow that uses actions/cache with a key built from hashFiles. It also assumes you know how restore-keys fall back to a partial match, and you understand that a cache entry can't be changed after it is created. Here you will learn when to replace the single actions/cache step with two separate actions, and how to control each one.
What the combined action does
actions/cache is one step in your workflow file, but it does two jobs at different times:
- When the step runs, it restores a cache. It looks for an entry that matches
key, then tries each line ofrestore-keys. - When the job ends, a hidden post step saves the cache. It saves only if the job succeeded and only if the exact
keywas not restored.
You don't control the second part. You can't save earlier, you can't save after a failure, and you can't skip the save for some branches. The split actions give you that control:
| Action | What it does | Where it runs |
|---|---|---|
| `actions/cache/restore@v4` | Restores a cache and does nothing else | At the step's position in the job |
| `actions/cache/save@v4` | Saves a cache and does nothing else | At the step's position in the job |
Both take the same path and key inputs as actions/cache. The restore action also accepts restore-keys.
The basic split pattern
The restore step needs an id so that later steps can read its outputs. The most useful output is cache-hit. The save step then uses an if condition to run only after an exact-key miss.
steps:
- uses: actions/checkout@v4
- id: restore
uses: actions/cache/restore@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
- run: npm ci
- if: steps.restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}Notice that key appears twice. The two steps are independent, so nothing passes the key from one to the other. If the two values drift apart, you will restore from one key and save to another, and the cache will never hit. The restore action exposes a cache-primary-key output, which you can use to avoid the duplication:
- if: steps.restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.npm
key: ${{ steps.restore.outputs.cache-primary-key }}What `cache-hit` really means
The output is a string, not a boolean. That is why the condition compares with 'true'.
| Situation | Value of `cache-hit` |
|---|---|
| Exact match on `key` | `'true'` |
| Match only through `restore-keys` | `'false'` |
| No match at all | empty string |
The comparison != 'true' covers both the partial match and the complete miss. That is what you want: after a partial match, npm ci may have downloaded new packages, so saving an updated entry under the exact key is worthwhile.
Saving even when the job fails
The post step of actions/cache never runs after a failed job. This hurts when the expensive work succeeds and a later step fails. A common case is a long dependency install followed by a flaky test run. Every rerun would repeat the install, because nothing was saved.
With the split actions, you can save right after the expensive step, before the tests start:
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: restore
uses: actions/cache/restore@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- run: npm ci
- if: steps.restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.npm
key: ${{ steps.restore.outputs.cache-primary-key }}
- run: npm testIf npm test fails, the cache is already saved. The next run starts warm.
You can also save at the very end regardless of the outcome by adding always() to the condition. Write the status function first, because a plain expression without a status function gets an implicit success() check:
- if: always() && steps.restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: build-cache
key: ${{ steps.restore.outputs.cache-primary-key }}Use this for build output, such as compiler caches, where a partial result from a failed build is still valuable. Don't use it when a failed job could leave a corrupt or half-written directory. Because entries are immutable, a bad cache saved under an exact key stays there until it is evicted or deleted.
Restoring everywhere, saving once
Matrix jobs and parallel jobs often need the same cache. If every job uses the combined action and all of them miss at the same moment, they all try to save the same key. Only one reservation succeeds. The others print a warning such as "Unable to reserve cache" and continue, so the job doesn't fail, but the work is wasted.
A cleaner design uses one job to warm the cache and other jobs to read it:
name: CI
on: [push, pull_request]
jobs:
warm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: restore
uses: actions/cache/restore@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
- if: steps.restore.outputs.cache-hit != 'true'
run: npm ci
- if: steps.restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.npm
key: ${{ steps.restore.outputs.cache-primary-key }}
test:
needs: warm
runs-on: ubuntu-latest
strategy:
matrix:
node: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/cache/restore@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
fail-on-cache-miss: true
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci
- run: npm testTwo inputs of the restore action matter here:
fail-on-cache-miss: truefails the step if no exact match exists. Use it when a missing cache means something is wrong with the workflow, not just a slow run.lookup-only: truechecks that an entry exists without downloading it. This is useful in the warm job when you only need to know whether to rebuild.
The test jobs only restore, so they never try to save. The cache holds npm's download cache, which doesn't depend on the Node.js version, so one key serves all three matrix entries. If your cached files did depend on the matrix value, you would add it to the key, as in the earlier matrix chapter.
Saving only from trusted branches
Caches written by one branch are visible to other branches under certain scope rules. You can reduce the risk of polluting the cache from short-lived branches by saving only from your default branch, while every branch still restores:
- if: github.ref == 'refs/heads/main' && steps.restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.npm
key: ${{ steps.restore.outputs.cache-primary-key }}This also keeps pull request runs from using up your repository's cache storage limit with entries that are rarely reused.
Common errors
- The key differs between restore and save. The save succeeds, but nothing ever matches. Use
cache-primary-keyor define the key once as a job-levelenvvalue. - The condition compares to a boolean. Writing
cache-hit == truecan behave unexpectedly because the output is a string. Compare with'true'. - The save step is placed after a step that can fail. The save is skipped, because steps after a failure run only when they have a status function such as
always(). Put the save immediately after the step that produces the files. - The
pathdiffers between steps. The cache is keyed bykeyand also bypathand compression settings. A save with a differentpaththan the restore can fail to match on a later restore. - Expecting an update under the same key. Saving to an existing exact key does not overwrite it. To refresh a cache that changes often, include
${{ github.run_id }}in the saved key and restore through arestore-keysprefix.
Exercise
Exercise
A workflow restores ~/.cargo with actions/cache/restore@v4 (id restore) under an exact key, then runs a 15-minute cargo build, then runs cargo test, which sometimes fails. Today the cache is never saved on runs where tests fail. Write the if line and the action for a save step that stores the cache right after the build, only on an exact-key miss, without repeating the key.
Show answer
Place this step between the build and the tests:
- if: steps.restore.outputs.cache-hit != 'true'
uses: actions/cache/save@v4
with:
path: ~/.cargo
key: ${{ steps.restore.outputs.cache-primary-key }}
Because the step runs before cargo test, a test failure no longer prevents the save. The condition compares against the string 'true', so both a partial match and a complete miss lead to a save. The cache-primary-key output supplies the same key that the restore step used.