Sub topic 9
Matrix builds: unique names and merging downloads
This chapter assumes you can read a workflow file and know that each job runs on its own runner with a fresh file system. It also assumes you have used actions/upload-artifact and actions/download-artifact in a simple build-then-deploy pipeline. Here you will see what changes when many matrix jobs each produce files.
Why matrix jobs need unique names
A matrix expands one job definition into several parallel jobs. Each of those jobs is a separate runner, so each one uploads its own files.
In v4 of the artifact actions, an artifact is immutable. Once an artifact with a given name exists in a workflow run, you cannot add files to it or overwrite it. If two matrix jobs both upload an artifact named results, the second upload fails with a conflict error.
The fix is to put a matrix value in the name, so every job produces a different artifact.
Uploading from each matrix job
The workflow below runs a test job on two operating systems. Each job writes a small results file and uploads it as results-<os>.
name: test-matrix
on: push
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v4
- run: echo "results for ${{ matrix.os }}" > results.txt
shell: bash
- uses: actions/upload-artifact@v4
if: always()
with:
name: results-${{ matrix.os }}
path: results.txt
collect:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
pattern: results-*
merge-multiple: true
path: all-results/
- run: ls all-results/Two details in the upload step matter:
name: results-${{ matrix.os }}producesresults-ubuntu-latestandresults-windows-latest.if: always()runs the upload even when an earlier step failed. This is useful for test results, because a failing test run is the one you most want to inspect.
The shell: bash line makes the echo redirect behave the same on the Windows runner as on Linux.
Downloading everything with a pattern
The collect job uses needs: test. It starts only after every matrix job has finished, so all the artifacts exist by then.
Without a name, download-artifact downloads all artifacts of the run. The pattern input narrows that to artifact names matching a glob. Here, results-* matches both artifacts and ignores any others, such as a dist artifact.
What merge-multiple does
By default, when you download more than one artifact, each one goes into its own subdirectory named after the artifact. The layout looks like this:
all-results/
results-ubuntu-latest/
results.txt
results-windows-latest/
results.txtSetting merge-multiple: true places the contents of all matched artifacts into the single path directory instead. That works well when the files have different names, for example report-linux.xml and report-windows.xml.
In the example above, both jobs upload a file called results.txt. With merge-multiple: true, the files land in the same directory and one overwrites the other. To keep both, either name the files uniquely or leave merge-multiple off and read from the per-artifact subdirectories.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Upload fails with an "artifact already exists" conflict | Several matrix jobs use the same `name` | Add a matrix value to the name |
| `collect` downloads nothing or too much | `pattern` does not match the names you uploaded | Check that the pattern matches your naming scheme |
| Some result files are missing after merging | Files with the same relative path overwrote each other | Give files unique names per job |
| No results for a failed matrix leg | The upload step was skipped after a failure | Add `if: always()` to the upload step |
Exercise
A matrix has node: [18, 20, 22]. Each job uploads coverage/lcov.info. Write the name value for the upload step, and the download-artifact inputs a later job needs to put all three files into one directory. What problem will you hit when merging, and how do you avoid it?
Show answer
Use name: coverage-${{ matrix.node }} in the upload step. In the later job, set pattern: coverage-*, merge-multiple: true, and a path such as all-coverage/. All three files share the relative path lcov.info, so merging makes them overwrite each other. Avoid this by renaming the file per job, for example coverage/lcov-${{ matrix.node }}.info, or by leaving merge-multiple off so each artifact keeps its own subdirectory.