Sub topic 16
Sharing files with artifacts and speeding up runs with caching
This chapter assumes you can write a workflow with several jobs, use needs, and read a matrix definition. Each job runs on a fresh runner, so files created in one job do not exist in the next. Artifacts move files between jobs. Caches make repeated work faster.
Artifacts: passing files between jobs
An artifact is a file or directory that a job uploads to the workflow run. Other jobs in the same run can download it, and you can also download it from the run page after it finishes.
Use actions/upload-artifact in the producing job and actions/download-artifact in the consuming job. The needs key makes sure the upload finishes before the download starts.
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node: [18, 20]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: npm ci && npm run build
- uses: actions/upload-artifact@v4
with:
name: dist-node-${{ matrix.node }}
path: dist/
deploy:
needs: build
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: dist-node-20
- run: ls -RNote the artifact name includes the matrix value. In upload-artifact@v4, names must be unique within a run, so two matrix legs that upload the same name will conflict.
Caching: reusing downloaded dependencies
A cache stores files, such as downloaded packages, so that later runs can restore them instead of fetching them again. Unlike an artifact, a cache belongs to the repository, not to one run. It is a speed optimization only. Your workflow must still work when the cache is empty.
The simplest route is the built-in caching in setup actions:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm testFor other cases, use actions/cache with a key. The key decides which cache is restored. Hashing the lock file means the cache changes whenever the dependencies change.
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ runner.os }}-${{ hashFiles('requirements.txt') }}
restore-keys: |
pip-${{ runner.os }}-On an exact key match, the cache is restored and not saved again. On a miss, restore-keys supplies the newest cache with a matching prefix, and the step saves a new cache under the exact key when the job succeeds.
Artifacts versus caches
| Question | Artifact | Cache |
|---|---|---|
| Purpose | Pass or keep results | Speed up repeated work |
| Scope | One workflow run | Shared across runs |
| Guaranteed to exist | Yes, until retention expires | No, may be evicted |
| Typical content | Build output, test reports | Package downloads |
Common errors
- Uploading with a path that matches no files. The step warns by default. Set
if-no-files-found: errorto fail instead. - Reusing one artifact name across matrix legs, which fails in v4.
- Using a cache key that never changes, so stale dependencies are restored forever.
- Caching build output that you should pass as an artifact.
Exercise
A workflow caches npm packages with the key npm-${{ hashFiles('package-lock.json') }}. You add a dependency, which changes the lock file. What happens on the next run, and what does restore-keys add?
Show answer
The hash changes, so the exact key misses. Without restore-keys the run starts with an empty cache and saves a new one when the job succeeds. With a restore-keys prefix such as npm-, the most recent matching cache is restored first, so most packages are already present, and the new exact-key cache is still saved afterward.
Exercise
The deploy job needs the output of build, but ls shows an empty directory. Name two likely causes.
Show answer
The build job did not upload an artifact, or the artifact name in download-artifact does not match the uploaded name. Also check that deploy lists build in needs, and that the upload path matched real files.