Sub topic 6
Using the built-in cache option of setup actions
This chapter assumes you can read a workflow file and know what actions/cache does with path, key, and restore-keys. You should also know how hashFiles builds a key from a lock file. The earlier chapters covered those. Here you will see how the same mechanism is available through a single input on the setup actions.
What the built-in option does
Setup actions install a language runtime on the runner. Examples are actions/setup-node, actions/setup-python, actions/setup-java, and actions/setup-go. Most of them can also manage a dependency cache. You turn it on with an input, usually cache.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm testWith cache: npm, the action does four things for you:
- It asks the package manager where its global cache directory is. For npm this is
~/.npm. - It finds the lock file and hashes it.
- It builds a cache key from the platform, the architecture, the package manager, and that hash.
- It restores the cache during setup. A post-job step saves it when the job ends.
You wrote none of the actions/cache step shown earlier. The setup action calls the same cache library internally, so the cache has the same behavior. It has the same scope rules, immutability, size limits, and eviction.
What gets cached
The built-in options cache the package manager's download cache. They do not cache the installed dependency folder.
| Setup action | `cache` values | Default lock file used for the hash | Cached directory |
|---|---|---|---|
| `actions/setup-node` | `npm`, `yarn`, `pnpm` | `package-lock.json`, `npm-shrinkwrap.json`, `yarn.lock`, or `pnpm-lock.yaml` | The package manager's global cache or store |
| `actions/setup-python` | `pip`, `pipenv`, `poetry` | `requirements.txt` (pip), `Pipfile.lock`, `poetry.lock` | The pip, pipenv, or poetry cache |
| `actions/setup-java` | `maven`, `gradle`, `sbt` | `pom.xml`, Gradle build files, or sbt build files | `~/.m2/repository`, `~/.gradle/caches`, or the sbt caches |
| `actions/setup-go` | on by default in v4 and later | `go.sum` | The Go module and build caches |
Check the README of the action version you pin, because supported values and default lock files change between releases.
Caching node_modules directly is a different choice. It skips the install work but can break when Node.js versions or operating systems differ. Caching the download cache is safer. npm ci still runs and still deletes and recreates node_modules, but it reads packages from disk instead of the network.
Choosing the lock file with cache-dependency-path
By default the action looks for a lock file in the repository root. In a monorepo or a project with the lock file in a subdirectory, that search finds nothing. Set cache-dependency-path to tell the action which files to hash.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: |
web/package-lock.json
api/package-lock.json
- run: npm ci
working-directory: webThe input accepts several paths or glob patterns, one per line. All matching files are hashed together. If any one changes, the key changes. This is the same effect as passing a multi-file pattern to hashFiles.
The same input exists on setup-python:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip
cache-dependency-path: requirements/*.txt
- run: pip install -r requirements/dev.txt
- run: python -m pytestOrder of steps matters
The setup action must be able to run the package manager to find its cache directory. For npm and Yarn Classic, that is already installed on the runner. For pnpm, it is not guaranteed. Install pnpm before setup-node:
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9
- uses: actions/setup-node@v4
with:
node-version: 20
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm testIf setup-node runs first with cache: pnpm, it fails because it cannot find the pnpm command. The checkout step also has to come before setup. Without it, the lock file does not exist yet when the action tries to hash it.
How the key is built
The key is generated for you, so you cannot add your own text to it with these inputs. For setup-node v4, it has this shape:
node-cache-Linux-x64-npm-<hash of lock file>It contains the operating system, the CPU architecture, the package manager, and the hash. It does not contain the Node.js version. A job on Node 18 and a job on Node 20 use the same key on the same runner type.
For npm's download cache this is usually fine. The cache stores package tarballs addressed by content, and they work with any Node version. It can matter if your install step compiles native addons or if you cache something other than downloads. In that case, a built-in key that ignores the version can hand one job an artifact built for another. setup-python does include the Python version in its key. Read the key in the "Post job" log to confirm what your pinned version of an action does.
Behavior to know before you depend on it
- Saves happen only when the job succeeds. If a test fails, the post step is skipped and the cache is not written. A first run with failing tests never populates the cache.
- Saves happen only after an exact-key miss. After a lock file change, the first job saves a new entry. Later jobs with the same lock file restore it and skip the save.
- The restore is a fallback chain internally. The actions restore with the exact key. If you need your own fallback behavior, use
actions/cachedirectly. - A missing lock file is an error, not a silent skip. With
cacheset and no matching file, the action fails with a message that the dependencies lock file was not found. Fix the path or setcache-dependency-path. - Setup actions share the repository's cache storage with
actions/cache. Their entries count toward the same size limit and are evicted the same way. - For the exact hit and miss messages, expand the setup step and the "Post" step in the job log. The log prints the key it tried and whether it found the cache.
Using it in a matrix
The built-in option works unchanged inside a matrix job. Each job restores and saves independently:
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm testSix jobs run, but they produce only two distinct keys, one for Linux and one for Windows, because the Node version is not in the key. Jobs on the same OS share a cache entry. The first to finish saves it, and the others that started earlier may all miss and then try to save. Only one save succeeds, because cache entries are immutable. The others log a message that the key already exists. That is harmless.
When to choose the built-in option or actions/cache
| Situation | Better choice |
|---|---|
| One language, standard lock file, default cache directory | Built-in `cache` input |
| Lock file in a nonstandard place | Built-in with `cache-dependency-path` |
| You need `restore-keys` or a version in the key | `actions/cache` |
| You cache a directory the setup action does not know about | `actions/cache` |
| You want to restore in one job and save in another | `actions/cache/restore` and `actions/cache/save` |
The built-in option is shorter and has fewer ways to get a key wrong. actions/cache gives you control. Start with the built-in option and move only when you hit one of the limits in the table.
Exercise
A repository keeps its lock file at frontend/package-lock.json. The workflow uses actions/setup-node@v4 with cache: npm, and the job fails during setup with a message that the dependencies lock file was not found. What change fixes it, and why does the original fail?
Show answer
Add cache-dependency-path: frontend/package-lock.json to the with block. By default the action only looks for a lock file in the repository root, so it finds nothing and fails. The input points the hash step at the real file. Also confirm that actions/checkout runs before setup-node, so the file exists on the runner.
Exercise
A matrix job runs Node 18, 20, and 22 on ubuntu-latest with cache: npm. How many distinct cache keys does setup-node v4 produce, and what could go wrong if the install step compiles native addons?
Show answer
One key, because the key holds the OS, architecture, package manager, and lock file hash, but not the Node version. If the cached directory held compiled output, one Node version could restore an artifact built for another. The npm download cache holds tarballs, so it is usually safe. For compiled output, switch to actions/cache with matrix.node in the key.