Sub topic 10
Running fewer checks with path filters
Path filters let a workflow skip when a change touches only files that the workflow does not care about. Used well, they cut runtime and cost. Used carelessly, they leave pull requests stuck waiting for a check that never runs. This chapter assumes you know how GitHub Actions triggers work, what a required status check is, and how pull request workflows are structured. The examples use GitHub Actions, and the same ideas apply to GitLab CI rules:changes and Jenkins changeset conditions.
Why filter by path
A monorepo or a repository with several components often has checks that only matter for part of the code. A documentation edit does not need a 20-minute integration suite. A change to services/billing/ does not need to rebuild the mobile app.
Path filters answer the question "which checks does this change need?" by looking at the files changed. This is change-based selection. It works at the workflow or job level, which is coarser than test-impact analysis tools that pick individual tests.
The benefit is real, but so is the risk. A filter encodes a belief about what a change can affect. If the belief is wrong, a broken change merges without the check that would have caught it.
Path filters in GitHub Actions
The paths and paths-ignore keys sit under the event trigger.
name: backend-pr
on:
pull_request:
branches: [main]
paths:
- 'services/api/**'
- 'packages/shared/**'
- 'package-lock.json'
- '.github/workflows/backend-pr.yml'
jobs:
backend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test --workspace services/apiThe workflow runs if at least one changed file matches any pattern. Three details matter here:
- The workflow file itself is listed. Without that, a change to the workflow would not run the workflow, so you could not test your own edits.
- Shared code and lockfiles are listed. A change to
packages/shared/or a dependency bump can break the API without touchingservices/api/. - Patterns use glob syntax.
**matches across directories, and*does not cross a/.
You can use paths-ignore instead to list files that should not trigger the workflow:
on:
pull_request:
paths-ignore:
- 'docs/**'
- '**/*.md'You cannot use paths and paths-ignore together for the same event. If you need to include some files and exclude others, use paths with negated patterns that start with !. Order matters: a later pattern overrides an earlier one.
on:
pull_request:
paths:
- 'services/**'
- '!services/**/README.md'Choosing between include and ignore lists
| Approach | Default when a new directory appears | Failure mode |
|---|---|---|
| `paths` (allow list) | Workflow does not run | A new dependency path is forgotten, so checks are skipped |
| `paths-ignore` (deny list) | Workflow runs | Checks run more often than needed, which costs time but stays safe |
For checks that guard correctness, prefer paths-ignore with a short list of clearly safe exclusions such as docs and images. The failure mode is wasted minutes. For expensive checks scoped to one component, an allow list is reasonable, provided you include shared code, lockfiles, and the workflow file.
How path filters interact with required checks
This is the most common source of blocked merges. A required status check must report a result before a pull request can merge. If a workflow is skipped because of a path filter, no status is reported, and GitHub leaves the check in a "Expected, waiting for status" state. The pull request cannot merge.
Compare this with a job skipped by an if: condition inside a running workflow. A skipped job reports as "skipped", and GitHub treats a skipped required check as passing. The two skip mechanisms behave differently:
| Skip mechanism | Check state on the pull request | Blocks a required check? |
|---|---|---|
| Workflow-level `paths` or `paths-ignore` | No status reported, stays pending | Yes |
| Job-level `if:` evaluating to false | Reported as skipped | No |
| Workflow-level branch filter that excludes the branch | No status reported | Yes |
There are two common ways to avoid the stuck state.
Option 1: Always run a gate workflow, filter inside it
Run a lightweight workflow on every pull request. It detects which paths changed and uses job-level conditions to decide what to run. Then make one final "gate" job the only required check.
name: pr
on:
pull_request:
branches: [main]
jobs:
changes:
runs-on: ubuntu-latest
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/**'
- 'packages/shared/**'
- 'package-lock.json'
frontend:
- 'apps/web/**'
- 'packages/shared/**'
- 'package-lock.json'
backend:
needs: changes
if: needs.changes.outputs.backend == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test --workspace services/api
frontend:
needs: changes
if: needs.changes.outputs.frontend == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm test --workspace apps/web
gate:
needs: [backend, frontend]
if: always()
runs-on: ubuntu-latest
steps:
- name: Fail if any needed job failed or was cancelled
run: |
if [ "${{ contains(needs.*.result, 'failure') }}" = "true" ] || [ "${{ contains(needs.*.result, 'cancelled') }}" = "true" ]; then
echo "A required job did not pass."
exit 1
fiThe gate job runs even when upstream jobs are skipped, because of if: always(). It fails only if a job failed or was cancelled. Skipped jobs do not count as failures. In branch protection, require only gate. You can then add or rename component jobs without editing the branch protection rules.
Without if: always(), the gate job would itself be skipped whenever a dependency was skipped, which hides failures behind a "skipped" status.
Option 2: Pair a filtered workflow with a no-op twin
Some teams keep the filtered workflow and add a second workflow with the same job name and the opposite filter. The second one succeeds immediately. This works, but the two files must stay in sync, so it is easy to break when someone renames a job. Option 1 is easier to maintain.
Shared code, lockfiles, and configuration
Most escaped bugs from path filters come from missing dependencies in the filter. Review these categories for each filtered check:
- Shared libraries imported by the component.
- Lockfiles and package manifests, since a dependency bump changes behavior without touching source.
- Build and tooling configuration, such as
tsconfig.json, linter configs, and Dockerfiles. - Workflow files and any reusable workflows or composite actions they call.
- Test fixtures and database migrations used by the tests.
- Environment or infrastructure definitions the tests depend on.
A useful habit is to derive the filter from the build graph where possible. Tools such as Nx, Turborepo, and Bazel compute affected projects from the dependency graph, which avoids hand-maintained lists. If your repository uses one of them, run its "affected" command in the changes job instead of listing paths by hand.
Where filters are safe, and where they are not
Path filters fit best on pull requests, where speed matters and a later stage provides a backstop. They are a poor fit for the stages that exist to catch what the earlier stages missed.
- Pull requests: filter freely, within the rules above. A wrong filter costs a broken main branch, which later stages can catch.
- Merge queue and main: filter with care. These runs test the combined result of several changes, and a change in one area can break another. If the suite is the safety net for your filters, do not filter it.
- Nightly: do not filter. Nightly runs have the time budget to cover everything, and they are where you find what the filters missed.
The dotted loop in the diagram is the point of keeping a nightly run. When a nightly failure traces back to a change that a filter skipped on its pull request, the right fix is to widen that filter.
Edge cases
Pull requests with many files
GitHub compares the pull request's changed files against its base. For pull_request events, the filter uses a three-dot diff, which looks at changes since the branch diverged from the base. For push events, it compares against the previous commit. A diff with a very large number of files can be truncated by the API, so very large pull requests may not trigger a path-filtered workflow as expected. If you hit this, use the unfiltered gate pattern.
Push events and new branches
When a branch is first pushed, there is no previous commit to compare against, and push path filters can behave unexpectedly. Another gap: a tag push ignores paths filters. Do not rely on path filters for push workflows that must always run on main.
Merge queue events
The merge_group event does not support paths or paths-ignore. The workflow runs for every queue entry. If you want to skip work in the queue, put the change detection inside the job, as in Option 1.
Reruns and stale results
A filter evaluates at the time the workflow is triggered. If a follow-up commit adds files in a new path, the new push triggers a fresh evaluation, and the earlier skipped state does not carry over. Check that the status shown on the latest commit reflects the latest filter result.
Measuring whether filters are working
Track two numbers per filtered check:
- Skip rate: the share of pull requests where the check did not run. A rate near zero means the filter saves nothing. A rate near 100 percent suggests the check may belong in a later stage.
- Escapes: failures in later stages that the skipped check would have caught. Any escape is evidence the filter is too narrow.
Use these in the placement review described in later chapters. A filter that saves little and adds complexity is a candidate for removal.
Exercise
A repository requires a workflow named backend to pass before merging. The workflow uses on: pull_request: paths: ['services/api/**']. A developer opens a pull request that only edits docs/setup.md. The pull request cannot merge. Why, and what is the most maintainable fix?
Show answer
The path filter prevents the workflow from starting, so the backend check never reports a status and stays in "Expected, waiting for status". A required check must report a result. The fix is to run a workflow on every pull request, detect changed paths inside it (for example with dorny/paths-filter or an affected-project command), skip component jobs with job-level if: conditions, and require only a final gate job that uses if: always() and fails when any needed job failed or was cancelled. Skipped jobs report as skipped, which counts as passing.
Exercise
You maintain a filter that runs the API tests only when services/api/** changes. Last week a nightly run failed because a dependency bump in package-lock.json broke the API. What should you change in the pull request workflow, and what does this tell you about the nightly stage?
Show answer
Add package-lock.json to the filter, along with other shared inputs such as packages/shared/** and the workflow file. The nightly run did its job: because it is unfiltered, it caught a gap in the pull request filter. Each such failure is a signal to widen a filter, and it is a reason to keep the nightly stage unfiltered.