Sub topic 9
Calling a reusable workflow with a matrix
This chapter assumes you can write a workflow_call workflow with typed inputs and call it from a job with uses:. It also assumes you know how strategy.matrix works for ordinary jobs. Here you combine the two.
Why use a matrix on a call
A calling job can carry a strategy.matrix. GitHub then starts one run of the called workflow for each matrix combination. This keeps the test logic in one shared file. The caller decides which versions or platforms to cover.
name: CI
on:
pull_request:
jobs:
test:
strategy:
matrix:
node: ['18', '20', '22']
uses: ./.github/workflows/node-test.yml
with:
node-version: ${{ matrix.node }}The node-test.yml file is the reusable workflow from earlier chapters. It declares a node-version input of type string. Each matrix value is passed through with:, so the called workflow runs three times.
Where the matrix context is available
The matrix context works in the calling job's with: and secrets: blocks. It does not work inside the uses: value. The uses: string must be a fixed path, so you cannot pick a different workflow file per matrix entry with an expression.
If you need to vary the workflow, pass an input and branch on it inside the called workflow. The called workflow does not see the matrix context directly. It sees only the values you pass as inputs.
Passing several values with include
You can pass more than one value per combination. Use include to add extra keys, and exclude to drop combinations you don't want.
jobs:
test:
strategy:
fail-fast: false
max-parallel: 2
matrix:
node: ['20', '22']
os: [ubuntu-latest, windows-latest]
exclude:
- node: '20'
os: windows-latest
uses: ./.github/workflows/node-test.yml
with:
node-version: ${{ matrix.node }}
runner: ${{ matrix.os }}This example assumes the called workflow also declares a runner input and uses it in runs-on: ${{ inputs.runner }}. Without that input declaration, the call fails with an unexpected input error.
The fail-fast and max-parallel options apply to the calling job. With fail-fast: false, one failing run does not cancel the others.
Matrices inside the called workflow
A job inside the reusable workflow can have its own matrix. The two levels multiply. A caller with three values that calls a workflow with a four-entry matrix produces twelve job runs. Check this before you add a matrix on both sides, because it counts against the platform's limit of 256 jobs for a single matrix. It also uses your concurrent job quota.
Outputs from a matrix call
Be careful when you read outputs. If you declare workflow_call outputs and call the workflow in a matrix, a downstream job reading needs.test.outputs.* gets a value from only one of the runs. You can't rely on which one. If you need results from every combination, upload an artifact with a unique name per combination, such as result-${{ inputs.node-version }}. Then download them in a later job.
Common mistakes
| Symptom | Likely cause |
|---|---|
| Workflow file is invalid | Expression used inside `uses:` |
| Unexpected input error | Matrix key passed in `with:` but not declared under `workflow_call.inputs` |
| Matrix value rejected | Input type is `number` or `boolean` but the matrix value is a string |
| Artifact upload conflict | Same artifact name used by every matrix run |
Exercise
A caller uses a three-value matrix on a job that has uses: ./.github/workflows/build.yml. A later job reads needs.build.outputs.artifact-name. Why is that value unreliable, and what is a safer way to collect results from all three runs?
Show answer
With a matrix call, the output comes from only one of the runs, and you can't tell which. A safer way is to have each run upload an artifact with a unique name that includes the matrix value, then download all the artifacts in a later job.