Sub topic 2
Artifacts versus caching: choosing the right tool
This chapter assumes you can read a GitHub Actions workflow and know that each job runs on its own runner with a fresh file system. You will learn how artifacts and caches differ and how to pick the right one for a given file.
Two ways to keep files
GitHub Actions gives you two storage features that look alike. Both save files from one run so something else can use them later. They solve different problems.
- An artifact is a set of files that a job produces and that you want to keep or hand on. Examples are a compiled
distdirectory, a test report, or a log. - A cache is a set of files that make a job faster, such as downloaded dependencies. The workflow can rebuild them if they are missing.
The key question is this: if the files vanished, would the workflow be wrong, or only slower? If it would be wrong, you need an artifact. If it would only be slower, use a cache.
Comparing the two
| Aspect | Artifact | Cache |
|---|---|---|
| Purpose | Keep or pass on results | Speed up repeated work |
| Typical content | Binaries, bundles, reports, logs | `node_modules`, package downloads |
| Lookup | By artifact name, within a run (or by run ID) | By key, with fallback keys |
| Guaranteed to exist | Yes, until retention expires | No, entries can be evicted or miss |
| Visible to people | Yes, in the run summary and through the GitHub CLI | No, it is an internal store |
| Official actions | `actions/upload-artifact`, `actions/download-artifact` | `actions/cache`, or the cache option of `actions/setup-node` |
A cache lookup can miss, so your steps must still work without it. An artifact is a deliberate output, so a later job can depend on it.
How the choice plays out
Consider a typical Node.js project. Installing dependencies takes a minute on every run. The packages come from a registry and are the same until package-lock.json changes. That is a cache. Running npm run build produces the dist directory that a deploy job must ship. That is an artifact.
The two features often appear in one workflow. The cache speeds up the install step, and the artifact carries the build output to the next job.
name: build
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 && npm run build
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/Here cache: npm stores the npm download cache, keyed on your lockfile. The upload-artifact step stores the finished dist directory. You will look at the upload step in detail in the next chapter.
Common mistakes
- Using a cache to pass build output between jobs. A cache miss leaves the next job without its input. Cache entries are also shared across runs by key, so a job could pick up output from an older run and ship stale files.
- Uploading
node_modulesas an artifact. It is large, it counts against your storage, and a cache does the same job better. - Treating a cache as a place to store results. Nobody can open a cache entry from the run page, and the platform may evict it.
- Expecting artifacts to speed up reruns. Artifacts are scoped to a run by default. A new run does not see them unless you download from a specific run ID, which a later chapter covers.
Rule of thumb
Use a cache for inputs you can regenerate. Use an artifact for outputs you need to keep. When you are unsure, ask who consumes the files. A later job, a later workflow, or a person reading the run means an artifact. The same job on a future run, only for speed, means a cache.
Exercise
A workflow has a test job that produces coverage-report.html, and a lint job that downloads Python packages with pip on every run. Which of these two sets of files should go in an artifact, and which in a cache? Give one reason for each.
Show answer
Put coverage-report.html in an artifact, because it is a result that people (or later jobs) need and the workflow cannot recreate it without rerunning the tests. Put the pip downloads in a cache, because they can be fetched again if the entry is missing, so the cache only saves time.