Sub topic 14
Building a matrix from changed projects
This chapter assumes you can write a change-detection job with dorny/paths-filter, read job outputs through the needs context, and use if conditions on jobs. Here you turn the list of changed projects into a job matrix, so one job definition fans out into one run per changed project.
Why a dynamic matrix
The earlier pattern used one test job per area. That works for two or three areas. With ten services you would copy the same job ten times, and each copy would need its own output and its own if.
A matrix replaces the copies. The matrix needs a list of project names, and the change-detection job can build that list for you.
The changes output
When you give dorny/paths-filter several named filters, it sets an output called changes. The value is a JSON array of the filter names that matched. If only api matched, the output is ["api"]. If nothing matched, it is [].
Expose that output from the job, then feed it to the matrix with fromJSON.
changes:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
packages: ${{ steps.filter.outputs.changes }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
api: 'services/api/**'
billing: 'services/billing/**'
test:
needs: changes
if: needs.changes.outputs.packages != '[]'
runs-on: ubuntu-latest
strategy:
matrix:
package: ${{ fromJSON(needs.changes.outputs.packages) }}
steps:
- uses: actions/checkout@v4
- run: make -C services/${{ matrix.package }} testHow it flows
Job outputs are always strings. fromJSON parses the string into a real array, and GitHub creates one matrix entry per element. Each entry runs the same steps with its own matrix.package value.
The empty matrix guard
The if line is required. If no filter matched, packages is [], and a matrix built from an empty array is an error. The job fails at startup instead of being skipped. The condition needs.changes.outputs.packages != '[]' skips the job before GitHub tries to expand the matrix.
The comparison is against a string, because outputs are strings. The exact text depends on the action, so print the output once in a debug step to confirm it is [] and not [ ] or an empty string.
Common errors
- Filter names must match directory names. The command
make -C services/${{ matrix.package }}only works if the filter nameapimatches the folderservices/api. Choose names with that in mind. - Forgetting
needs: changes. Without it, theneeds.changescontext is not available, and the matrix expression fails. - Shared code. A change in
libs/sharedmatches no project filter here. Either add the shared path to each filter, as in the earlier chapter, or the matrix will miss affected projects. - Required checks. Matrix jobs have names like
test (api). These names change from run to run, so do not mark them as required. Require the gate job from the earlier chapter instead, and listtestin itsneeds.
Exercise
A pull request changes only README.md. With the workflow above, what happens to the test job, and why does it not fail?
Show answer
The test job is skipped. No filter matches README.md, so the changes output is []. The if condition needs.changes.outputs.packages != '[]' is false, so GitHub skips the job before it expands the matrix. Without that condition, the empty matrix would cause an error.
Exercise
You add a third filter, search: 'services/search/**'. What else must you change for the new project to be tested?
Show answer
Nothing else in the workflow. The changes output now includes search when its files change, and the matrix creates a run with matrix.package set to search. The folder services/search must exist and support make test.