Sub topic 14
Combining results from a build matrix
This chapter assumes you can write a GitHub Actions workflow with jobs and steps, and that you have already uploaded a report as an artifact and written a job summary. Here you combine the results of several matrix jobs into one report.
Why matrix results need combining
A matrix runs the same job several times, for example once per Python version. Each run is a separate job on its own runner. Each job has its own log, its own job summary, and its own file system. No job can read another job's files.
If every job writes a report to reports/junit.xml, the files do not meet anywhere. You need to move them to one place. The tool for that is artifacts: each matrix job uploads its results, and a later job downloads all of them.
Use a unique artifact name per job
In actions/upload-artifact@v4, an artifact name must be unique within a workflow run. If two matrix jobs upload with the same name, the second upload fails. Put the matrix value in the name.
Also keep file names distinct. If you later merge the downloaded files into one folder, two files with the same name overwrite each other. The example below names the report junit-3.11.xml and junit-3.12.xml.
Keep going when one job fails
By default, one failed matrix job cancels the others. Set fail-fast: false so every combination finishes and reports. Use if: ${{ !cancelled() }} on the upload step so the report is saved even when the tests fail.
Add a report job that depends on the matrix
A job with needs: test waits until every matrix job finishes. By default it is skipped if any of them failed. Adding if: ${{ !cancelled() }} makes it run anyway, which is what you want for a report.
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.11", "3.12"]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- run: pip install -r requirements.txt pytest
- run: pytest --junitxml=reports/junit-${{ matrix.python-version }}.xml
- uses: actions/upload-artifact@v4
if: ${{ !cancelled() }}
with:
name: test-results-${{ matrix.python-version }}
path: reports/
report:
needs: test
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
pattern: test-results-*
merge-multiple: true
path: reports
- run: ls reportsThe pattern input selects every artifact whose name starts with test-results-. With merge-multiple: true, all files land directly in reports. Without it, each artifact gets its own subfolder named after the artifact.
Build one summary
Once the files are in one folder, the report job can run the summary script from the previous chapter on each file. It can also add the totals. The summary appears on the report job, so readers see one table for the whole run instead of one per matrix job.
Common errors
- Duplicate artifact names. The upload fails with a conflict. Add
${{ matrix.* }}to the name. - Report job skipped.
needsalone skips the job when a dependency fails. Addif: ${{ !cancelled() }}. - Empty reports folder. The
patterndoes not match the uploaded names, or no test job reached the upload step. - Overwritten files. Merged artifacts that share a file name replace each other. Include the matrix value in the file name.
Exercise
A matrix has jobs for Python 3.11 and 3.12. Both upload an artifact named test-results and the second upload fails. What change fixes it, and what should the report job's download step use to collect the results?
Show answer
Change the upload name to test-results-${{ matrix.python-version }} so each job has a unique name. In the report job, use actions/download-artifact@v4 with pattern: test-results-* and merge-multiple: true to download all artifacts into one folder.
Exercise
The report job has needs: test, but it never runs when a test job fails. Why, and how do you fix it?
Show answer
A job with needs is skipped by default when a dependency fails. Add if: ${{ !cancelled() }} to the report job so it runs after failures and is skipped only when the run is cancelled.