How a partial match restores#
The restore-keys input lists "prefix-matched keys, that are used for restoring stale cache if no cache hit occurred for key". actions/cache GitHub's reference adds that the action tries the exact key, then prefixes, on the run's branch first and then on the default branch, that when several caches match a prefix, "the action returns the most recently created cache", and that a run can restore caches from its own branch, the default branch and, for a pull request, the base branch. Dependency caching reference
The cache-hit output is true only for an exact match. It is false when a restore key matched and empty when nothing was restored. actions/cache outputs The step log names the key it used, in a line that starts Cache restored from key: in the action's source. The separate actions/cache/restore action also returns it as the cache-matched-key output. actions/cache/restore A step guarded by if: steps.cache.outputs.cache-hit != 'true' therefore runs after a partial match, and it runs on top of the restored files.
Fixture: a deleted module that stayed in dist/#
This fixture simulates the restore with a tarball, because the restore is only a file extraction. TypeScript 5.9.3 on Node 24.22.3. Commit A has src/price.ts and src/legacy-discount.ts, and its dist/ is saved under build-linux-<hash A>. Commit B deletes the legacy module. Its exact key misses, the prefix build-linux- matches A, and the build runs:
# key build-linux-<hash B> misses; restore-keys build-linux- matches A: cache-hit=false
$ npx tsc && ls dist
legacy-discount.js
price.js
$ rm -rf dist && npx tsc && ls dist
price.js
The compiler wrote price.js and did not remove legacy-discount.js, so an artifact made from dist/ would contain code that no longer exists at commit B. The build exited 0 both times. Any packaging step that globs dist/**, and any server that loads every file in a directory, would pick it up.
Decide what goes in the cache#
| Path | Safe to restore from another commit? | Why |
|---|---|---|
Package manager download cache (~/.npm, ~/.cache/pip, Go module cache) | Yes, when installs are pinned by a lockfile | The installer installs what the lockfile names; without pins, a cached package can be chosen over a newer download |
Compiler caches keyed by their own inputs (GOCACHE, sccache) | Yes, with care | The tool looks each entry up by a hash of its inputs; see go test's cached results for what it cannot validate |
node_modules | Only with an exact key | A partial restore holds another lockfile's tree. npm ci removes it first; npm install builds on it |
dist/, build/, target/release binaries you ship | No | Nothing checks that the files came from this commit |
Cache the download directory, where a partial match only saves time, and build into a clean output directory. If an output cache is worth its time, give it an exact key built from every input and no restore-keys.
- uses: actions/cache@v6
with:
path: ~/.npm
key: npm-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
restore-keys: npm-${{ runner.os }}-
- run: rm -rf dist && npm ci && npm run build
shell: bash
What else leaves the same trace#
A file in the artifact with no source at the commit has other causes: a build step that copies a whole directory, a self-hosted runner whose checkout set clean: false, or a generator that is out of date. The restored key in the log, the checkout's inputs and a fresh run of the generator tell them apart. Compare the artifact's file list with a clean build of the same commit. A file only the artifact has came from somewhere else; a file both have can still differ in content, so compare checksums too.
To pass a build between jobs, use an artifact from the same run instead of a cache; see an artifact from another run. For build caching in Docker, see a cached layer that kept an old download.