Sub topic 7
Passing Files Between Jobs with Artifacts
This chapter assumes you know basic YAML, the on, jobs, runs-on, and steps keys, and how needs orders jobs. The previous chapter showed how to pass small values with job outputs. Files need a different mechanism.
Why jobs cannot share files directly
Each job runs on its own fresh runner. When the build job writes a dist/ folder, that folder exists only on the build job's runner. The runner is discarded when the job ends, so a later deploy job starts with an empty workspace.
An artifact is a file or folder that a job uploads to GitHub for storage. Other jobs in the same workflow run can then download it. Two actions handle this:
actions/upload-artifact@v4saves files from the runner under a name you choose.actions/download-artifact@v4fetches the files saved under that name.
Outputs suit short strings such as a version number. Artifacts suit build results, test reports, and any other files.
A build and deploy example
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: mkdir -p dist && echo "hello" > dist/app.txt
- uses: actions/upload-artifact@v4
with:
name: dist
path: dist/
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
name: dist
path: dist/
- run: ls distThe build job creates dist/app.txt and uploads the folder as an artifact named dist. The deploy job declares needs: build, so it starts only after the upload has finished. It downloads the artifact into dist/ and lists the contents.
The needs line matters here. Without it, both jobs start at the same time, and deploy would try to download an artifact that does not exist yet.
Options worth knowing
The with block of each action takes a few inputs you will use often.
| Input | Action | Purpose |
|---|---|---|
| `name` | upload and download | Identifies the artifact. Names must match between the two actions. |
| `path` | upload | Files or folders to save. Accepts globs and multi-line lists. |
| `path` | download | Directory to extract into. Defaults to the workspace. |
| `retention-days` | upload | How long GitHub keeps the artifact before deleting it. |
| `if-no-files-found` | upload | `warn` (default), `error`, or `ignore`. |
Setting if-no-files-found: error is a good habit for build output. Otherwise a wrong path only produces a warning, and the failure appears later in the downstream job.
Common errors
- Artifact not found. The download
namedoes not match the uploadname, or the downloading job is missingneeds, so it ran before the upload. - Name collisions in v4. With
upload-artifact@v4, each artifact name must be unique within a workflow run. Two jobs uploading to the namedistcauses the second upload to fail. Matrix jobs should include the matrix value in the name, for examplename: dist-${{ matrix.os }}. - Files in the wrong place. The downloaded files are extracted into the
pathyou give. If you omit it, they go into the workspace root, which can differ from where your later steps look. - Artifacts from another run. These actions only reach artifacts from the current workflow run. Sharing across runs needs a different mechanism, such as the
run-idinput or the caching feature. - Skipped upload. If the build step fails, the upload step is skipped by default. Add
if: always()to the upload step when you want logs or reports saved from failed runs.
Exercise
Exercise
A workflow has a test job that uploads an artifact named report and a publish job that downloads report. The publish job fails with "Artifact not found". Name two likely causes and one fix for each.
Show answer
First, publish may lack needs: test, so it runs in parallel and downloads before the upload exists. Fix: add needs: test. Second, the artifact names may differ, for example report versus reports, or the upload step was skipped because an earlier step failed. Fix: make the name values identical and check that the upload step actually ran.