Sub topic 6
Using the built-in cache option of setup actions
This chapter assumes you can read a workflow file, know what a job and a step are, and have used actions/cache with a key built from hashFiles. If not, review the chapters on actions/cache and cache keys first.
What the built-in option does
Several setup actions can cache dependencies for you. actions/setup-node, actions/setup-python, and actions/setup-java all offer this. You add one input, and the action does three things:
- It finds the dependency manifest or lock file for your package manager.
- It builds a cache key from the runner's operating system, the package manager, and a hash of that file.
- It restores the cache before your install step and saves it when the job ends successfully.
You write no separate actions/cache step. Internally the setup action uses the same cache service, so scope, size limits, and eviction behave the same as in the earlier chapters.
A minimal example
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 testThe cache: npm input tells the action to cache the npm download cache. It reads package-lock.json by default. The accepted values for Node.js are npm, yarn, and pnpm.
Order matters. actions/checkout must run first, because the action needs the lock file on disk to compute the key. The setup step must also run before the install step, so the cache is restored in time.
Comparing the two approaches
| Aspect | `actions/cache` | Built-in `cache` option |
|---|---|---|
| Lines of YAML | A full step with `path`, `key`, `restore-keys` | One input |
| Key control | You write the key | The action builds it |
| Cached path | Any path you choose | The package manager's cache directory |
| Fallback keys | You define `restore-keys` | Handled by the action |
| Use outside setup actions | Yes | No |
The built-in option is the right default for a standard project. Use actions/cache when you need a custom key, such as one that includes the matrix Node.js version, or when you cache something other than a package manager's download cache.
What gets cached
The built-in option caches the package manager's global download cache, not node_modules. For npm this is ~/.npm. That is why you still run npm ci. The install runs, but it reads packages from disk instead of the network.
Caching node_modules directly is discouraged. npm ci deletes that folder before installing, so a restored copy would be wasted.
A common error: lock file not found
If the action cannot find a lock file, the step fails with a message that it could not find a dependency file. This happens in three typical situations:
- The project has no committed lock file.
- The lock file is in a subdirectory, as in a monorepo.
actions/checkoutis missing or runs after the setup step.
For the subdirectory case, point the action at the file with cache-dependency-path:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
cache-dependency-path: web/package-lock.json
- run: npm ci
working-directory: webThe action hashes whatever file you name, so the key changes when that file changes.
Reading the result
The setup step's log shows whether a cache was restored. Look for a line stating that the cache was restored from a key, or that no cache was found. A miss on the first run is normal. A miss on every run usually means the lock file changes on each commit, or the cache was evicted.
Exercise
Your workflow runs actions/setup-node@v4 with cache: npm and then actions/checkout@v4. The setup step fails with a dependency file error. What is the cause, and how do you fix it?
Show answer
The setup action runs before checkout, so package-lock.json is not on disk when the action computes the key. Move actions/checkout ahead of actions/setup-node.
Exercise
Your lock file is at web/package-lock.json. Which input do you add to actions/setup-node so the cache key is computed from it?
Show answer
Add cache-dependency-path: web/package-lock.json alongside cache: npm.