Sub topic 14
Splitting Restore and Save Steps
This chapter assumes you can read a workflow file, know what actions/cache does, and understand cache keys built with hashFiles. You should also know that a cache entry is immutable once saved.
Why split the action
The actions/cache action is a combined action. It restores a cache at the point where you place it, then saves a cache in a post step at the end of the job. The save happens automatically, and only if the job succeeds and the exact key was not already found.
That default works for most jobs. It gets in the way in three cases:
- You want to save the cache even when a later step fails, or skip saving in some situations.
- You want to save right after installing dependencies, before a long test run.
- Several jobs only need to read the cache, and one job should be the only one that writes it.
The split actions actions/cache/restore and actions/cache/save let you place each half yourself.
Restore, install, then save conditionally
The restore action exposes a cache-hit output. It is 'true' only when the restored cache matched the primary key exactly. A partial match through restore-keys leaves it 'false'. The workflow below saves 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') }}Both steps use the same key expression. If they differ, the save step writes an entry that the next restore never looks for.
What changes compared with the combined action
Because the save step is now an ordinary step, it follows the normal rules for steps:
- It runs in order, so the cache is written before
npm teststarts. A failing test no longer prevents the save, since the save has already happened. - To save after a failure, add
if: always()orif: failure()and place the step last. Only do this if the cached files are still valid after the failure. - If an earlier step fails, later steps are skipped unless they have such a condition.
A common pattern: one writer, many readers
In a workflow with several jobs, you can let a single job build the cache and have the others use only actions/cache/restore. This avoids many jobs racing to save the same key. Readers never write, so they cannot overwrite or pollute the entry.
Common errors
| Symptom | Likely cause |
|---|---|
| Cache is never restored | `key` in the save step differs from the restore step |
| Save step runs on every build | Missing `if` on `cache-hit`, or the condition compares to the wrong value |
| Save skipped after a partial match | `cache-hit` is `'false'` for partial matches, so this is correct only if you want a fresh entry saved |
| Cache missing after a failed run | The combined action does not save on failure, so use the split actions with `if: always()` |
Note that cache-hit is a string. Compare it with 'true' in quotes, as in the example.
Exercise
A job uses actions/cache/restore with restore-keys, and a partial match is found. The next step runs npm ci, and then actions/cache/save has the condition steps.restore.outputs.cache-hit != 'true'. Does the save step run, and why is that useful?
Show answer
Yes, it runs. A partial match through restore-keys sets cache-hit to 'false', so the condition is satisfied. This is useful because the lock file changed, so the exact key does not exist yet. The save step stores an updated cache under the new exact key, and later runs with the same lock file get an exact hit.
Exercise
Why might you move cache saving into a separate step instead of using the combined actions/cache action when your test suite is slow and sometimes fails?
Show answer
The combined action saves only in a post step after the whole job succeeds. A failing test would prevent the save, and the dependencies would be downloaded again on the next run. A separate save step placed right after the install step stores the cache before the tests run.