Sub topic 10
Matrix jobs and needs
This chapter assumes you know the on, jobs, runs-on, and steps keys, and that you have read the earlier chapters on needs, fan-out, and fan-in. Here you will see how a matrix job behaves as a single node in the dependency graph.
A matrix is one job in the graph
A strategy.matrix expands one job definition into several job runs, called legs. Each leg gets its own fresh runner and runs in parallel with the others by default. In the dependency graph, the matrix is still one job with one name. The legs are copies of that job.
This matters because needs refers to the job name, not to a single leg. A job that lists a matrix job in needs waits for every leg to finish.
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
max-parallel: 2
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- run: echo "Testing on ${{ matrix.os }}"
release:
needs: test
runs-on: ubuntu-latest
steps:
- run: echo "All matrix legs passed"The matrix creates three test legs. max-parallel: 2 lets at most two run at once, so the third starts when one of the first two finishes. release starts only after all three legs have succeeded.
How the result of a matrix job is decided
The matrix job as a whole has a single result, which needs.test.result reports. It follows these rules:
| Legs | Result of the matrix job |
|---|---|
| All legs succeed | success |
| At least one leg fails | failure |
| Legs are cancelled, none failed | cancelled |
| All legs are skipped | skipped |
A single failed leg is enough to make the whole test job fail. The default behavior from the earlier chapter on failures then applies: release is skipped.
Fail-fast and the rest of the legs
By default, strategy.fail-fast is true. When one leg fails, GitHub cancels the legs that are still running or queued. The matrix job then ends as a failure, and downstream jobs are skipped.
If you want every leg to finish so you can see the full picture, set fail-fast: false.
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]With this setting, a failing Windows leg no longer stops the other two. The matrix job still reports failure, so release is still skipped.
Using a matrix on the downstream side
You can also put a matrix on the job that has needs. Each of its legs waits for the upstream job, then runs in parallel.
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: echo "build once"
smoke:
needs: build
runs-on: ubuntu-latest
strategy:
matrix:
target: [staging, canary]
steps:
- run: echo "Smoke test ${{ matrix.target }}"build runs once. Then both smoke legs start together.
Limits to know about
- A matrix can produce at most 256 jobs per workflow run.
needscannot select individual legs. You cannot say "wait only for the Linux leg."- If you need only part of the matrix to gate a job, split it into separate jobs, or make the gating job check the result with an
ifcondition. - Outputs from a matrix job are unreliable. Every leg writes to the same output name, and the last leg to finish usually wins. Use artifacts with distinct names instead, such as
dist-${{ matrix.os }}.
Common errors
- Release ran before all platforms passed. Check that
needsnames the matrix job. If it does not, the job runs in parallel with the matrix. - Downstream job skipped, but only one leg failed. This is the expected result. Use
if: ${{ always() }}on the downstream job only if it should run anyway. - Legs cancelled that did not fail.
fail-fastdefaults totrue. Set it tofalseto let them finish.
Exercise
A workflow has a test matrix with legs for Node 18, 20, and 22, and a publish job with needs: test. The Node 20 leg fails. By default, what happens to the Node 22 leg if it is still running, and what happens to publish?
Show answer
With fail-fast defaulting to true, GitHub cancels the Node 22 leg if it is still running. The test job ends as a failure, so publish is skipped. Setting fail-fast: false would let Node 22 finish, but publish would still be skipped because the test job as a whole failed.
Exercise
You want a notify job to run after the test matrix finishes, whether it passed or failed. What if condition and needs setting do you use?
Show answer
Set needs: test and if: ${{ always() }}. The always() function makes notify run even when test fails. To limit it to failures, use if: ${{ always() && needs.test.result == 'failure' }}.