How to Run Only Affected Jobs on Changed Paths in GitHub Actions
A change-detection job exposes per-area booleans that downstream jobs gate on, so untouched services never build.
Run dorny/paths-filter once to compute which areas changed, expose the result as job outputs, and put an if: on each build job that reads needs.changes.outputs.<area>.
Steps
- Add a
changesjob that runsdorny/paths-filterwith named filters. - Expose each filter result as a job
outputs:entry. - Gate each build job with
if: needs.changes.outputs.<area> == 'true'.
Workflow
.github/workflows/ci.yml
jobs:
changes:
runs-on: ubuntu-latest
outputs:
api: ${{ steps.f.outputs.api }}
web: ${{ steps.f.outputs.web }}
steps:
- uses: actions/checkout@v4
- id: f
uses: dorny/paths-filter@v3
with:
filters: |
api: 'services/api/**'
web: 'services/web/**'
build-api:
needs: changes
if: needs.changes.outputs.api == 'true'
runs-on: ubuntu-latest
steps:
- run: echo build apiGotchas
- A required check that is skipped can block merges; add a passing gate job that always succeeds.
- On the first push of a new branch the filter compares against the base, so a wide diff runs everything.
Related guides
How to Skip CI on Docs-Only Changes in GitHub ActionsAvoid running the full pipeline for documentation edits by adding a paths-ignore filter so commits touching o…
How to Cache the Monorepo Dependency Store in GitHub ActionsCache a workspace package store once for a whole monorepo in GitHub Actions by keying on every lockfile, so a…