Sub topic 3
Uploading Your First Artifact
This chapter assumes you can read a GitHub Actions workflow file, know that a workflow contains jobs and jobs contain steps, and remember that each job runs on a fresh runner with its own file system. It covers the first half of the artifact life cycle: getting files out of a job and into storage.
Why a job must upload
When a job ends, GitHub discards its runner. Anything the job wrote to disk disappears with it. An artifact is a named bundle of files that the job sends to GitHub's storage before the runner goes away. After that, later jobs, later workflows, and people can retrieve the bundle.
The official action for this is actions/upload-artifact. This chapter uses version 4 (@v4).
A minimal upload
The workflow below builds a project and uploads the dist directory.
name: build
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/
retention-days: 7
if-no-files-found: errorThe first two steps check out the code and build it. The third step is the upload. It takes four inputs here:
| Input | Meaning |
|---|---|
| `name` | The name of the artifact. Later steps and jobs use this name to find it. |
| `path` | The file, directory, or pattern to upload. |
| `retention-days` | How many days GitHub keeps the artifact before deleting it. |
| `if-no-files-found` | What to do when `path` matches nothing. |
Only path is required. If you leave out name, the artifact is called artifact. A descriptive name is better, because you will need to refer to it later.
What happens during the upload
The action runs on the runner as a normal step. It resolves path against the job's working directory, collects the matching files, compresses them into one archive, and sends the archive to GitHub. The step log lists the files it found and the artifact's size.
placeholderUse the diagram below for the sequence.
Retention
retention-days sets how long the artifact lives. Here it is 7, so GitHub deletes the artifact a week after the upload. If you omit the input, the repository's default retention applies. The default is 90 days on public repositories unless an administrator changed it. You can set a value lower than the repository limit but not higher. Short retention for build output you only need during a run keeps storage use down.
Missing files
if-no-files-found accepts three values:
warnis the default. The step prints a warning and the job continues.errorfails the step.ignorestays silent and continues.
The example uses error. A build that produces no dist directory is almost always a bug, and with the default warn the job would pass and the problem would surface later, when a download step finds nothing. Choose error whenever later work depends on the files.
Common mistakes
- Wrong path.
pathis relative to the job's working directory, which is the checkout root by default. If the build writes tobuild/and you listdist/, nothing matches. - Skipping the check. Leaving
if-no-files-foundatwarnhides an empty upload. - Reusing a name. In v4, an artifact name must be unique within a workflow run. Uploading twice under the same name fails. The next chapters cover this and how to handle matrix jobs.
- Expecting the files to be visible to other jobs automatically. Uploading does not copy anything to other jobs. They must download the artifact explicitly.
Checking the result
After the run finishes, open it on GitHub. The run summary page has an Artifacts section near the bottom that lists each artifact with its size. If dist is missing there, read the upload step's log to see which files it matched.
Exercise
Your build writes its output to out/, and a failed build must not look like a success. Which with: inputs should the upload step have, and which value of if-no-files-found do you choose?
Show answer
Use name: out (or another descriptive name) and path: out/. Set if-no-files-found: error so the step fails when the directory is empty or missing. The default warn would let the job pass. Optionally add retention-days to limit storage.
Exercise
A test job fails, and you want its results.txt file saved so you can read it. The upload step is the last step of the job, but no artifact appears. What is the likely cause and the fix?
Show answer
The upload step is skipped because an earlier step failed, and steps run only after success by default. Add if: always() (or if: failure()) to the upload step.