Interview Prep/GitHub Actions/Jobs, Steps, Matrix & Concurrency

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.

All GitHub Actions questions Roadmap: GitHub Actions

More on Jobs, Steps, Matrix & Concurrency