Sub topic 7
Downloading artifacts in a workflow
This chapter assumes you can read a workflow file, know that each job runs on its own fresh runner, and have uploaded an artifact with actions/upload-artifact@v4. Here you will fetch that artifact in a later job.
What download-artifact does
actions/download-artifact@v4 copies an artifact from GitHub's artifact storage onto the current runner. A job starts with an empty file system, so files built in an earlier job are not there until you download them.
The action reads from storage, not from the other runner. The upload must therefore be finished before the download starts. Inside one workflow run, you guarantee this with needs.
A basic download
This workflow builds in one job and reads the result in another.
name: build-and-deploy
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci && npm run build
- 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 -R dist/The deploy job does not run actions/checkout. It needs only the built files, so it downloads the artifact and nothing else.
The inputs you will use most
| Input | Purpose |
|---|---|
| `name` | The artifact to download. Must match the upload name exactly. |
| `path` | The destination directory on the runner. Defaults to the workspace. |
| `pattern` | A glob that selects several artifacts by name. |
| `merge-multiple` | Puts files from all matched artifacts into one directory. |
| `run-id`, `github-token`, `repository` | Download from a different workflow run. |
The action also sets an output, download-path, with the absolute path where the files landed. You can use it in later steps through ${{ steps.<id>.outputs.download-path }}.
Where the files end up
The directory layout depends on how you ask for the artifact.
- With
name, the contents go directly intopath. If the artifact holdsindex.html, you getdist/index.html. The artifact name does not become a folder. - Without
name, the action downloads every artifact in the run. Each one goes into a subdirectory named after the artifact, for exampledist/andreports/insidepath. - With
patternand nomerge-multiple, you get one subdirectory per matched artifact. - With
patternandmerge-multiple: true, all files go intopathtogether. If two artifacts contain a file with the same path, the later one overwrites the earlier one. Matrix builds are covered in a later chapter.
The layout inside an artifact is fixed at upload time. The upload stores paths relative to the common root of the files it matched, so check the upload path if the layout surprises you.
Check the result in the log
When a download works, the log shows the artifact name and the destination. Add a listing step while you are developing.
steps:
- uses: actions/download-artifact@v4
id: fetch
with:
name: dist
path: dist/
- run: |
echo "Downloaded to ${{ steps.fetch.outputs.download-path }}"
ls -R dist/Common errors
- "Unable to find any artifacts for the associated workflow." The
namedoes not match any upload. Names are case-sensitive. Also confirm the upload step actually ran and was not skipped by anif:condition. - The download runs before the upload finishes. Without
needs, jobs start in parallel and the artifact may not exist yet. Addneeds: buildto the downloading job. - Files appear in an unexpected folder. You omitted
name, so each artifact got its own subdirectory. Setname, or setmerge-multiple: truewith apattern. - Mixing action versions. Artifacts uploaded with
upload-artifact@v4cannot be read bydownload-artifact@v3, and the reverse is also true. Use v4 for both. - Executable permissions lost. Artifacts are zipped, and file permissions are not preserved. If you upload a script and run it later, call it with
sh script.shor runchmod +xafter the download.
Exercise
Exercise
A workflow has a build job that uploads an artifact named Dist. The deploy job has needs: build and runs download-artifact@v4 with name: dist and path: out/. The step fails with "Unable to find any artifacts". What is the cause, and how do you fix it?
Show answer
Artifact names are case-sensitive, so dist does not match Dist. Make the names identical in the upload and download steps, for example both dist. The needs setting is already correct, so ordering is not the problem.