Sub topic 4
Building cache keys with hashFiles
This chapter assumes you can read a workflow file, know what actions/cache does, and have used a package manager such as npm. It covers how to build a cache key that changes when your dependencies change.
What a key does
A cache key is a string that names one cache entry. When the job starts, actions/cache looks for an entry whose key matches exactly. If it finds one, it restores the files. If it does not, the job runs without the cache. After a successful job, the action saves a new entry under that key.
Cache entries are immutable. Once a key has been saved, it cannot be overwritten. So the key must change whenever the cached content should change. A fixed key such as npm-cache would restore the same stale files forever.
Using hashFiles
The hashFiles function computes a SHA-256 hash of the files that match a glob pattern. If the files change, the hash changes, and so does the key. A lock file is the usual input because it records the exact dependency versions.
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') }}
- run: npm ci
- run: npm testThe key has three parts:
| Part | Example value | Purpose |
|---|---|---|
| `runner.os` | `Linux` | Keeps caches for different operating systems apart |
| `npm` | `npm` | Names the tool, so other caches do not collide |
| `hashFiles(...)` | `a1b2c3...` | Changes when the lock file changes |
The resulting key looks like Linux-npm- followed by a 64-character hash.
How the key decides a hit or a miss
The diagram shows what happens on each run.
When you change one dependency, package-lock.json changes. The hash changes, so the key misses and the job installs from the network. At the end, it saves a new entry for the new key. Later runs with the same lock file hit that entry.
Choosing what to hash
Hash the files that determine what gets cached. For other ecosystems, the pattern changes:
- npm:
**/package-lock.json - pip:
**/requirements.txt - Cargo:
**/Cargo.lock
You can pass several patterns, separated by commas, to hash them together: hashFiles('**/package-lock.json', '**/.npmrc').
Common errors
- The pattern matches nothing.
hashFilesthen returns an empty string, and the key ends with a dash. Every run uses the same key, so the cache never updates. Check the path and thatactions/checkoutran first. - The lock file is not committed. Without it there is nothing stable to hash.
- Hashing too much. A pattern like
**/*includes files that change on every run, so you never get a hit. - Omitting
runner.os. Paths and binaries differ between operating systems, so a cache from one can break another.
Exercise
Your workflow caches ~/.npm with the key ${{ runner.os }}-npm. You update a dependency, but the job keeps restoring the old files. Why, and how do you fix it?
Show answer
The key never changes, and cache entries are immutable, so the old entry always matches and no new entry is saved. Add a hash of the lock file: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}. The key then changes whenever the lock file does.
Exercise
Your key is ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}, but the repository stores the file at backend/requirements.txt. What does hashFiles return, and what is the effect on caching?
Show answer
It returns an empty string because the pattern matches no file at the repository root. The key becomes Linux-pip- on every run, so the cache is never refreshed after the first save. Use **/requirements.txt or backend/requirements.txt.