Sub topic 5
Uploading reports as artifacts
This chapter assumes you can read a workflow file and run pytest from the command line. You should also know that a failed step normally stops the rest of its job. The report files from the previous chapters live on the runner's disk, and the runner is deleted when the job ends. Artifacts are how you keep those files.
What an artifact is
An artifact is a file or folder that a job saves to the workflow run. You can download it from the run page, or a later job in the same run can download it. Artifacts are the right place for raw reports such as junit.xml and coverage.xml, which are too large or too detailed for a job summary.
The actions/upload-artifact action does the upload. It needs two inputs:
name: the label for the artifact. It must be unique within the run.path: a file, a folder, or a glob pattern to include.
Upload the reports
This job runs pytest, writes both reports into a reports/ folder, and uploads that folder.
name: tests
on: [push, pull_request]
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install -r requirements.txt pytest pytest-cov
- run: >-
pytest
--junitxml=reports/junit.xml
--cov=src
--cov-report=xml:reports/coverage.xml
--cov-report=term-missing
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: test-reports
path: reports/The example assumes your project has a requirements.txt file and a src package.
Why the `if` line matters
When the pytest step fails, GitHub skips every later step by default. The upload would be skipped too, and a failing run is exactly when you want the report.
if: ${{ !cancelled() }} makes the step run after success or failure, but not when someone cancels the run. Cancelling usually means you no longer want the output, so skipping the upload is sensible.
if: always() also works, but it runs even after a cancel, which can delay the cancellation. Prefer !cancelled() for report uploads.
Handling a missing folder
If pytest crashes before it writes anything, reports/ will not exist. By default, upload-artifact then prints a warning and continues. You can change this with the if-no-files-found input:
| Value | Behavior when nothing matches |
|---|---|
| `warn` | Prints a warning and the step succeeds. This is the default. |
| `error` | Fails the step. |
| `ignore` | Succeeds without a message. |
Use error when a missing report means something is wrong with your setup and you want to know.
Retention and naming
Artifacts are kept for 90 days by default, depending on your repository and organization settings. Set retention-days to a lower number if the reports are only useful for a short time:
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: test-reports
path: reports/
retention-days: 14
if-no-files-found: errorWith actions/upload-artifact@v4, artifact names must be unique within a run. Two jobs that upload the name test-reports will conflict, and the second upload fails. This matters for build matrices, which a later chapter covers. For now, remember that each job needs its own name, for example by adding a matrix value to it.
Common errors
- No artifact on failed runs. The upload step has no
ifcondition, so it was skipped. - Empty or missing artifact. The
pathdoes not match where the test runner wrote the files. Check the output paths in your pytest options. - Duplicate name error. Two uploads in one run use the same
name. - Secrets in reports. Artifacts can be downloaded by anyone who can read the repository's runs. Check that your reports do not contain tokens or private data.
Exercise
Your upload step has no if line. Tests fail, and the run page shows no artifact. Why, and what is the smallest change that fixes it?
Show answer
A failed test step causes later steps without a status condition to be skipped, so the upload never ran. Add if: ${{ !cancelled() }} to the upload step so it runs after a failure but not after a cancellation.
Exercise
Two matrix jobs both upload an artifact with name: test-reports. What happens, and how do you avoid it?
Show answer
With upload-artifact v4, the second upload fails because the name is already used in the run. Give each job a unique name, such as test-reports-${{ matrix.python-version }}.