Sub topic 12
Choosing between caches and artifacts
This chapter assumes you can write a workflow with several jobs and steps. It also assumes you have used actions/cache or the cache option of actions/setup-node, and that you know how matrix jobs expand. Both caches and artifacts store files outside the runner, so they look interchangeable at first. They solve different problems, and picking the wrong one causes slow builds, missing files, or storage bills.
The core difference
A cache is a speed optimization. It holds files that you can regenerate, such as downloaded dependencies, and later runs reuse them to skip work. A cache entry may be missing, so a workflow must still succeed without it.
An artifact is a result. It holds files that a run produced and that someone or something needs, such as a build output, a test report, or a coverage file. A workflow that uploads an artifact expects to get exactly those files back, and a missing artifact is an error.
| Question | Cache | Artifact |
|---|---|---|
| Purpose | Reuse inputs across runs | Keep or pass outputs of a run |
| Guaranteed to exist | No, a miss is normal | Yes, until retention expires |
| Shared between jobs of one run | Possible, but not its purpose | Yes, the standard way |
| Shared between runs | Yes, keyed by name and scope | Not directly, only through the API or UI |
| Downloadable by people | No | Yes, from the run summary page |
| Mutable | No, a key is immutable once saved | No in v4, each name is uploaded once per run |
| Lifetime | Evicted after 7 days without access, or by the size limit | Retention period, 90 days by default |
| Typical content | `~/.npm`, `~/.cache/pip`, `~/.cargo/registry` | `dist/`, `coverage/`, test reports, logs |
Which one do you need?
Ask two questions. First, can the files be rebuilt if they are missing? If yes and you only want speed, use a cache. Second, does another job or a person need these exact files from this run? If yes, use an artifact.
A build often needs both. The npm download cache is a cache, because a miss only makes the install slower. The compiled dist/ directory is an artifact, because the deploy job must receive that exact build.
Passing build output between jobs
Jobs run on separate runners, so files created in one job are gone when the next job starts. An artifact carries them across. The example below caches dependencies through setup-node, builds, and uploads the output. A second job downloads it.
name: Build and deploy
on: [push]
jobs:
build:
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 run build
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
retention-days: 7
if-no-files-found: error
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: dist
path: dist
- run: ls -R distTwo settings matter here. if-no-files-found: error makes the build job fail when dist/ is empty. The default is only a warning, and the failure would then show up later in the deploy job. retention-days: 7 shortens retention, so storage is released earlier than the 90-day default.
The cache does the opposite job. If the setup-node cache is missing, npm ci is slower but the build still works. If the artifact is missing, the deploy job has nothing to deploy, and download-artifact fails the step.
Why a cache is the wrong tool for passing files
You can technically save a cache in one job and restore it in another. Do not do this for results, for four reasons:
- A cache restore can miss. A job that depends on a restore has to handle a miss, which defeats the purpose.
- A cache key is immutable. Once a key exists, later saves to the same key are skipped. If you build
dist/on every run, you need a unique key per run, and these keys then fill your cache quota. - Caches are not shown on the run page, so nobody can download and inspect the files.
- Caches are shared by scope rules, not by run. A concurrent run on another branch may see different entries than you expect.
Why an artifact is the wrong tool for dependencies
The reverse also fails. Uploading node_modules as an artifact at the end of every run costs upload time and storage, and it does not help the next run. A new run cannot restore the previous run's artifact with a simple step. The download-artifact action reads artifacts from the current run unless you pass a run ID and a token. Dependencies belong in a cache, where the key and restore-keys decide when a stored entry is reused.
Artifacts also cost storage on every run, and each upload counts against your quota until retention expires. Caches are limited to a fixed size per repository, 10 GB by default, and old entries are evicted when it fills up. The two limits behave differently, so a large file that is fine as a cache entry may be expensive as a stream of artifacts.
Artifacts in matrix jobs
In upload-artifact@v4, an artifact name must be unique within a workflow run. Matrix jobs that all upload to the name test-report conflict, and later uploads fail. Put the matrix values in the name, then collect the pieces in a later job.
name: Test matrix with reports
on: [push]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
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: |
mkdir -p reports
npm test > reports/node-${{ matrix.node }}.txt 2>&1
- uses: actions/upload-artifact@v4
if: always()
with:
name: test-report-node${{ matrix.node }}
path: reports/
summarize:
needs: test
if: always()
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
pattern: test-report-*
merge-multiple: true
path: reports
- run: ls reportsThe if: always() on the upload step keeps the report even when tests fail, which is when you most want it. In the summarize job, pattern selects all matching artifacts, and merge-multiple: true puts their files into one directory instead of one subdirectory per artifact.
This example also shows the two storage types side by side. The cache in setup-node has one entry per operating system and lock file, which the matrix jobs can share when the key is the same. The artifacts have one entry per matrix job, because each job produces different output.
Common errors
- Using a cache to hand a build to the next job. It works until the first cache miss or key collision. Use an artifact.
- Uploading the same artifact name from every matrix job. In v4, the second upload fails. Include the matrix values in the name.
- Leaving retention at the default for large, short-lived files. Set
retention-daysso storage is released sooner. - Expecting an artifact to persist for the next run. Artifacts belong to the run that created them.
- Not setting
if-no-files-found: errorfor required outputs. An empty upload succeeds with a warning, and the failure appears in a later job.
Summary
Use a cache for reusable, rebuildable inputs where a miss only costs time. Use an artifact for outputs that a later job or a person needs from a specific run. Combine them when a job both needs fast dependencies and produces a result. In matrix jobs, make artifact names unique and merge them in a final job.
Exercise
A workflow has a build job that compiles a Go binary and a release job that publishes it. The author saves the binary with actions/cache under the key binary-${{ github.sha }} and restores it in release. Name one risk with this design and say what to use instead.
Show answer
Cache restores can miss, so release may find no binary and either fail or publish nothing, and the cache is not downloadable from the run page. Also, the per-commit keys fill the repository's cache quota and are evicted unpredictably. Use actions/upload-artifact@v4 in build and actions/download-artifact@v4 in release, with if-no-files-found: error. Keep a cache only for the Go module download directory.
Exercise
A matrix of three Node.js versions each runs actions/upload-artifact@v4 with name: coverage. The second and third uploads fail. How do you fix it, and how do you read all three results in a later job?
Show answer
Give each job a unique name, such as coverage-node${{ matrix.node }}. In the later job, use actions/download-artifact@v4 with pattern: coverage-* and merge-multiple: true to download all three into one directory.