Sub topic 11
Keeping test reports as artifacts
This chapter assumes you have a working test workflow, can read a run's logs, and know basic YAML. Logs disappear into long scrollback, and the files your tests produce vanish when the runner is discarded. Artifacts fix that by saving those files with the run.
What an artifact is
An artifact is a file or folder that a job uploads and GitHub stores with the workflow run. You can download it from the run's summary page, or from a later job in the same run. Common examples are test reports (JUnit XML, HTML coverage pages), screenshots from browser tests, and log files.
Each runner starts as a clean virtual machine and is thrown away when the job ends. Anything you don't upload is lost.
Uploading a report
The actions/upload-artifact action does the upload. Give it a name and a path.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm test
- name: Upload test report
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: test-report
path: reports/This assumes your test command writes its report to reports/. Configure your test runner for that, for example with a JUnit reporter option.
Why the `if` condition matters
By default a step runs only if every earlier step succeeded. When npm test fails, the upload step is skipped, and that is exactly when you want the report.
The condition if: ${{ !cancelled() }} makes the step run after a failure but not when someone cancels the run. The older always() also runs after cancellation, which wastes time on a run nobody wants.
Paths and missing files
path accepts a single file, a directory, or a glob pattern. You can list several, one per line.
- uses: actions/upload-artifact@v4
with:
name: test-report
path: |
reports/
coverage/lcov.info
if-no-files-found: warn
retention-days: 14if-no-files-foundcontrols what happens when nothing matches.warnis the default and only prints a warning.errorfails the step.ignorestays silent.retention-dayssets how long GitHub keeps the artifact. If you omit it, the repository's default retention applies. A shorter value saves storage.
Paths are relative to the workspace, the folder where your repository was checked out.
Artifacts in a matrix
In version 4 of the action, artifact names must be unique within a run. If a matrix job uploads test-report several times, all but the first upload fail. Put the matrix values in the name.
- name: Upload test report
if: ${{ !cancelled() }}
uses: actions/upload-artifact@v4
with:
name: test-report-${{ matrix.os }}-node${{ matrix.node-version }}
path: reports/Using artifacts in later jobs
A later job can fetch an artifact with actions/download-artifact. Declare the dependency with needs, because separate jobs run on separate runners. This is how you could merge reports from every matrix job into one summary.
Exercise
Your npm test step fails, but the artifact never appears on the run page. The upload step uses uses: actions/upload-artifact@v4 with no if. What is the cause, and what is the fix?
Show answer
A step without an if runs only when all previous steps succeeded, so the failed test step caused the upload to be skipped. Add if: ${{ !cancelled() }} to the upload step so it runs after failures.
Exercise
A matrix of three Node versions uploads with name: test-report. Only one artifact is saved and the other uploads error out. How do you fix it?
Show answer
Make the name unique per matrix combination, for example name: test-report-node${{ matrix.node-version }}, because v4 of upload-artifact does not allow duplicate names in one run.