Sub topic 15
Generating a matrix at runtime with fromJSON
A static matrix lists every value in the workflow file. Sometimes the values are not known until the run starts. Examples are the packages changed in a pull request, or the Node.js versions listed in a config file. A dynamic matrix solves this. One job computes the matrix, and a later job expands it.
This chapter assumes you know strategy.matrix, include and exclude, and the needs keyword for ordering jobs.
How the pieces connect
A job can publish string values as outputs. Another job that lists it under needs can read them through the needs context. The fromJSON function turns a JSON string into a real object, which the matrix can then use.
A minimal example
jobs:
plan:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set.outputs.matrix }}
steps:
- id: set
run: echo 'matrix={"node":[18,20,22]}' >> "$GITHUB_OUTPUT"
test:
needs: plan
runs-on: ubuntu-latest
strategy:
matrix: ${{ fromJSON(needs.plan.outputs.matrix) }}
steps:
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- run: node --versionThis works in three stages:
- The step with
id: setappendsmatrix=<json>to the file named by$GITHUB_OUTPUT. That makes it a step output. - The job-level
outputsblock copies the step output so other jobs can see it. Without this mapping,testcannot read the value. testwaits forplanbecause ofneeds.fromJSONparses the string, and the runner creates three jobs, one for each Node.js version.
Computing the JSON
Hard-coding the JSON in echo shows the mechanism, but it gains nothing over a static matrix. In practice, build the value from data. This step reads versions from a file in the repository:
steps:
- uses: actions/checkout@v4
- id: set
run: |
versions=$(jq -c '.versions' node-versions.json)
echo "matrix={\"node\":$versions}" >> "$GITHUB_OUTPUT"The -c flag makes jq print compact JSON on a single line. The name=value format of $GITHUB_OUTPUT expects one line per output.
The object can use the same keys as a static matrix, including include and exclude. For example, {"include":[{"pkg":"api"},{"pkg":"web"}]} creates one job per package.
Common errors
- Invalid JSON. A missing quote or a trailing comma fails the
testjob before any matrix job starts. Print the string withechoin theplanjob when debugging. - Forgetting
needs. Without it,needs.plan.outputs.matrixis empty and the matrix cannot be built. - Skipping the
outputsmapping. Step outputs are not visible to other jobs unless the job re-exports them. - An empty list. If the computed list has no values, the job fails instead of being skipped. Check for emptiness in
planand guardtestwith anif:condition, such asif: needs.plan.outputs.matrix != '{"node":[]}'. - Size limits. A workflow run can create at most 256 matrix jobs per matrix. Job outputs are also capped in size, so keep the JSON small.
Exercise
A plan job computes a matrix, but the test job fails with an empty matrix value. The step writes to $GITHUB_OUTPUT correctly and test has needs: plan. What else should you check?
Show answer
Check that the plan job has an outputs: block that maps matrix: ${{ steps.set.outputs.matrix }}. Step outputs are not visible to other jobs unless the job exports them. Also confirm that the step id matches the one used in the mapping.