Sub topic 10
Building a change-detection job
This chapter assumes you know basic workflow syntax, job outputs, and if conditions from earlier chapters. You will build one small job that decides which areas of the repository changed. Later jobs read its answer.
The idea
A workflow-level paths filter is all-or-nothing. It starts the whole workflow or none of it. A change-detection job lets the workflow always start, then lets each downstream job opt in or out.
The workflow
name: ci
on:
pull_request:
push:
branches: [main]
jobs:
changes:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
outputs:
backend: ${{ steps.filter.outputs.backend }}
frontend: ${{ steps.filter.outputs.frontend }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
backend:
- 'services/api/**'
- 'libs/shared/**'
- '.github/workflows/ci.yml'
frontend:
- 'apps/web/**'
- 'libs/shared/**'
- '.github/workflows/ci.yml'
backend-test:
needs: changes
if: needs.changes.outputs.backend == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make -C services/api test
frontend-test:
needs: changes
if: needs.changes.outputs.frontend == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm --prefix apps/web testHow it works
- The
changesjob checks out the code and runsdorny/paths-filter, a third-party action. It compares the changed files with each named filter. - For every filter name, the action sets a step output of
'true'or'false'. The step hasid: filter, so you read these values assteps.filter.outputs.backend. - The job's
outputsblock copies those step outputs so other jobs can see them. Step outputs are not visible outside their own job. - Each test job lists
changesinneeds. That makes the outputs available through theneedscontext and makes the test job wait. - The
ifcondition compares the output to the string'true'. A job whose condition is false is skipped.
Notice that libs/shared/** and the workflow file appear in both filters. A change to shared code, or to the pipeline itself, must run every area that depends on it.
Common errors
- Missing
needs. Without it,needs.changes.outputs.backendis empty and the condition is always false. The job is skipped without warning. - Comparing to a boolean. Outputs are strings. Write
== 'true', not== true. - Forgetting the
outputsblock. A step output alone is invisible to other jobs. - Omitting
pull-requests: read. Once you listpermissions, unlisted scopes becomenone, and the action can fail on pull requests. - Leaving out shared paths. If
libs/shared/**is missing, a library change skips the tests that exercise it.
Exercise
A pull request changes only libs/shared/util.py. Using the workflow above, which test jobs run, and why? What would change if you removed libs/shared/** from the frontend filter?
Show answer
Both backend-test and frontend-test run, because libs/shared/** appears in both filters, so both outputs are 'true'. Without that line in the frontend filter, the frontend output would be 'false' for this change, and frontend-test would be skipped even if the frontend depends on the shared library.