CI/CD & GitHub Actions (50 Qs)•Part 2: Advanced Workflows, Security & Containerization (Mid-Level)
How do you generate dynamic job matrices programmatically?
mid LevelCode ExampleSecurity & AuthDocker & Containers
In Simple Words: When testing a monorepo, you only want to run jobs for microservices that actually changed. You can execute a discovery job that outputs a JSON array, and pass it dynamically to the matrix of a downstream job:
yaml
jobs:
detect-changes:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.packages }}
steps:
- uses: actions/checkout@v4
- id: set-matrix
run: echo "packages=[\"service-auth\", \"service-billing\"]" >> $GITHUB_OUTPUT
build:
needs: detect-changes
runs-on: ubuntu-latest
strategy:
matrix:
package: ${{ fromJson(needs.detect-changes.outputs.matrix) }}
steps:
- run: echo "Building package ${{ matrix.package }}"