Sub topic 1
What Workflow Artifacts Are and Why Use Them
Prerequisites
This chapter assumes you know the following:
- Basic YAML syntax.
- How GitHub Actions workflows, jobs, and steps fit together.
- That each job runs on its own runner with a fresh file system.
You do not need to have used artifacts before.
What an artifact is
A workflow artifact is a file, or a set of files, that a job saves during a workflow run. GitHub stores the files outside the runner. They stay available after the job ends and the runner is destroyed.
Typical artifacts include:
- Compiled binaries and packaged builds, such as a
dist/directory. - Test reports and coverage output.
- Log files that help you debug a failed run.
Artifacts are attached to the workflow run that created them. You can find them on the run's summary page, download them with the GitHub CLI, or fetch them in a later job.
Why you need them
Each job in a workflow runs on a separate runner. The runner starts with a clean file system, and GitHub discards it when the job finishes. If a build job creates dist/, a deploy job cannot see that directory. Its runner has never run the build.
Artifacts solve this in three ways:
- Handing files between jobs. One job uploads its output and a later job downloads it.
- Keeping results after the run. Reports and logs stay available for people to inspect after the runner is gone.
- Handing files between workflow runs. A later run or a different workflow can fetch files an earlier run produced.
The diagram shows the first case.
Artifacts versus caching
GitHub Actions also has a cache, and the two are easy to confuse. They serve different goals.
| Question | Artifact | Cache |
|---|---|---|
| Main purpose | Keep and share the output of a run | Speed up runs by reusing files |
| Typical content | Binaries, reports, logs | Dependencies such as `node_modules` |
| Lookup | By artifact name within a run | By cache key |
| Can be missing? | No, if the upload succeeded and has not expired | Yes, a cache miss is normal |
| Visible to people | Yes, downloadable from the run page | Not as a deliverable |
Use an artifact when the files are a result you need to keep or pass on. Use a cache when the files can be rebuilt and you only want to avoid doing the work again. If your workflow still works when the files are absent, just slower, you want a cache.
A first look at the actions
Two official actions handle artifacts. actions/upload-artifact saves files from a job. actions/download-artifact retrieves them. This chapter uses only the upload side to show the shape of the idea. Later chapters cover each option in detail.
This job builds a project and uploads the dist/ directory as an artifact named dist:
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: errorEach option does one thing:
nameis the label you use to find the artifact later.pathsays which files to upload.retention-dayssets how long GitHub keeps the artifact before deleting it. Here, 7 days.if-no-files-found: errorfails the step when nothing matchespath. Without it, the step only warns and your workflow can go on without the files you expected.
After the run, the artifact appears in the Artifacts section of the run summary. You can download it as a zip file from there.
Common mistakes
- Expecting files to carry over between jobs. Without an upload and a download, the second job cannot see the first job's files.
- Using an artifact as a dependency cache. It works, but it is slower to manage and clutters the run page. Use the cache for that.
- Forgetting that artifacts expire. Once the retention period ends, GitHub deletes the artifact. Anything you need long term belongs in a release or a package registry.
- Uploading sensitive files. Anyone who can read the run can usually download its artifacts. Later chapters cover this.
Exercise
A build job compiles a binary, and a separate package job needs to bundle it. The package job fails because the binary is missing. Why does this happen, and which mechanism fixes it?
Show answer
Each job runs on its own runner with a fresh file system, so the binary built in build does not exist in package. The fix is to upload the binary as an artifact in build with actions/upload-artifact. The package job then downloads it with actions/download-artifact.
Exercise
Your workflow installs npm dependencies on every run, and you want it to run faster. Should you use an artifact or a cache, and why?
Show answer
Use a cache. The dependencies can be rebuilt, so a missing copy only makes the run slower. An artifact is meant for output you need to keep, share, or download.