Sub topic 10
Running fewer checks with path filters
This chapter assumes you can read a GitHub Actions workflow and know what a required status check is. You will learn to skip checks that cannot be affected by a change, without leaving pull requests stuck.
Why filter by path
A pull request that edits only docs/ does not need a 12-minute backend test run. Path filters tie a check to the files it covers. This shortens feedback for most pull requests and cuts runner cost. The check still runs whenever the files it covers change.
Filters work best on checks that map cleanly to a directory, such as backend tests, frontend tests, or documentation linting. They work poorly on checks that depend on shared code, like a root package.json or a build script.
The simple form and its trap
GitHub Actions supports paths and paths-ignore on the pull_request trigger:
on:
pull_request:
branches: [main]
paths:
- 'backend/**'
- 'package-lock.json'If a pull request touches none of these paths, the whole workflow is skipped. A skipped workflow creates no status check, so any required check from that workflow stays in the "Expected, waiting for status" state. The pull request cannot merge.
A safer pattern: detect changes inside the workflow
Let the workflow always start. Add a job that detects which areas changed, make the expensive jobs conditional on its output, and add one final job that you mark as required.
name: pr
on:
pull_request:
branches: [main]
permissions:
contents: read
pull-requests: read
jobs:
changes:
runs-on: ubuntu-latest
outputs:
backend: ${{ steps.filter.outputs.backend }}
steps:
- uses: actions/checkout@v4
- uses: dorny/paths-filter@v3
id: filter
with:
filters: |
backend:
- 'backend/**'
- 'package-lock.json'
backend-tests:
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 --prefix backend
gate:
if: always()
needs: [changes, backend-tests]
runs-on: ubuntu-latest
steps:
- name: Fail if any needed job failed or was cancelled
run: |
if [ "${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}" = "true" ]; then
exit 1
fiA job skipped by if reports as skipped, which branch protection treats as passing. The gate job runs on every pull request and fails only when a real job failed. Require gate and nothing else. When you add a new filtered job, add it to needs on gate, and branch protection needs no change.
Where filters should not apply
- Merge queue and main. Path filters are not available on
merge_group, and the merge commit tests the combined result of several changes. Run the integration suite there without filters. - Nightly runs. A scheduled run has no diff. It is your safety net for anything a filter wrongly skipped.
- Shared inputs. Include lockfiles, CI config, and shared libraries in every filter that depends on them. A missed dependency is the usual way a filter lets a broken change through.
Check your filters against escapes
When a bug reaches main although a check "should" have caught it, look at whether a filter skipped that check. Fix the filter, and keep the nightly run as the backstop. A later chapter covers reviewing placement using this kind of data.
Exercise
A workflow has on.pull_request.paths: ['src/**'], and its test job is a required status check. A contributor opens a pull request that only edits README.md. What happens, and how do you fix it?
Show answer
The workflow is skipped, so the test check is never reported and stays "Expected, waiting for status". The pull request cannot merge. Remove the workflow-level paths filter, detect changes in a changes job, put if conditions on the expensive jobs, and require a single always-running gate job instead of test.