How do you build a matrix dynamically from a previous job?
AdvancedConceptJobs, Steps, Matrix & Concurrency
Answer
Emit JSON as a job output and parse it with fromJSON:
jobs:
plan:
runs-on: ubuntu-24.04
outputs:
services: ${{ steps.list.outputs.services }}
steps:
- uses: actions/checkout@v4
- id: list
run: echo "services=$(ls services | jq -R . | jq -cs .)" >> "$GITHUB_OUTPUT"
build:
needs: plan
if: needs.plan.outputs.services != '[]'
strategy:
matrix:
service: ${{ fromJSON(needs.plan.outputs.services) }}
runs-on: ubuntu-24.04
steps:
- run: echo "building ${{ matrix.service }}"
Guard against an empty list, since an empty matrix fails the workflow with a "does not contain any values" error.
More on Jobs, Steps, Matrix & Concurrency
How does `needs` work, and what happens to a job whose dependency failed or was skipped?BeginnerWhat is the implicit `success()` in a job `if`, and why is `always()` usually the wrong fix?IntermediateWhat values can `needs.<job>.result` take?BeginnerHow does a matrix expand, and how are job names generated?BeginnerHow do `include` and `exclude` work in a matrix?IntermediateWhat do `fail-fast` and `max-parallel` do?Beginner