Sub topic 12
Choosing between caches and artifacts
This chapter assumes you can read a workflow file, know what actions/cache does, and have built cache keys with hashFiles. Caches and artifacts both store files from a run. They solve different problems, and mixing them up is a common source of slow or broken pipelines.
What each one is for
A cache is an optimization. It holds files that you can rebuild, such as downloaded dependencies, so later runs skip the work of fetching them. A workflow must still succeed if the cache is missing.
An artifact is an output. It holds files that the run produced and that someone or something needs afterward, such as a test report, a compiled binary, or a coverage file. A workflow can depend on an artifact being there.
| Question | Cache | Artifact |
|---|---|---|
| Purpose | Speed up future runs | Keep or pass on results |
| Lookup | By key, with optional `restore-keys` fallback | By name within a run |
| Shared across runs | Yes, within scope rules | Only by explicit download from that run |
| Can be missing safely | Yes | No, the consumer expects it |
| Downloadable in the UI | No | Yes, from the run summary |
| Typical content | `~/.npm`, `~/.cache/pip`, `~/.cargo` | Reports, bundles, installers |
Deciding which to use
Ask what happens if the files vanish.
- If the next run can recreate them with only a time cost, use a cache.
- If the files are the result of the work, or a later job needs this run's exact output, use an artifact.
Do not use a cache to hand build output from one job to another. A cache key can match an older entry through restore-keys, so a downstream job may receive files from a different commit. Caches are also immutable once saved under a key, so a rebuilt file will not replace the old one. Artifacts are tied to the run that made them, so the consumer gets exactly what that run produced.
Do not use artifacts to store dependencies between runs either. Downloading an artifact from a previous run needs extra API calls and run lookups, and artifacts expire on a retention schedule meant for results, not for reuse.
A workflow that uses both
This workflow caches the npm download cache to speed up installs. It then uploads the test report as an artifact so it can be read after the run.
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') }}
restore-keys: |
${{ runner.os }}-npm-
- run: npm ci
- run: npm test
- if: always()
uses: actions/upload-artifact@v4
with:
name: test-report
path: reports/
retention-days: 7The if: always() condition uploads the report even when tests fail, which is when you most want it. The cache step above it can be lost without breaking anything. The artifact is the deliverable.
Common mistakes
- Caching build output to pass it between jobs. Use
upload-artifactanddownload-artifactinstead. - Uploading
node_modulesas an artifact on every run. It is slow, counts against storage, and a cache does the job better. - Assuming a cache hit. Write steps so a miss only costs time.
- Treating an artifact as long-term storage. It expires after its retention period.
Exercise
A build job compiles a binary, and a separate deploy job in the same run must ship exactly that binary. Should the binary pass between the jobs as a cache or an artifact, and why?
Show answer
As an artifact. The deploy job needs the exact output of this run. A cache lookup can fall back to an older entry through restore-keys, and a saved cache entry cannot be overwritten, so the deploy job might ship the wrong binary. An artifact is bound to the run that produced it.
Exercise
Your workflow downloads 400 MB of Python packages on every run. Which storage option fits, and what should the workflow do if it is unavailable?
Show answer
Use a cache, keyed on the hash of the requirements file. If the cache is missing, the workflow should just install the packages normally, which costs time but does not fail the run.