patternbashMajor
Monorepo CI with path-based job filtering
Viewed 0 times
monorepoturboreponxaffected packagesCI filterfetch-depth
Problem
In a monorepo, every CI run builds and tests all packages even when only one package changed. As the repo grows, CI time scales with the number of packages rather than the number of changes.
Solution
Use Turborepo's --filter with git diff to run only affected packages:
For nx monorepos:
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # needed for turbo to compute affected packages
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- name: Run affected tests
run: |
npx turbo run test build lint \
--filter='...[origin/main]' \
--cache-dir=.turbo
- uses: actions/cache@v4
with:
path: .turbo
key: turbo-${{ github.sha }}
restore-keys: turbo-For nx monorepos:
npx nx affected --target=test --base=origin/mainWhy
--filter='...[origin/main]' tells Turbo to run only packages that have changed relative to main, plus their dependents. This scales CI time to the size of the change, not the size of the repo.
Gotchas
- fetch-depth: 0 is required; without it git cannot compute the diff and Turbo falls back to running everything
- Turbo remote cache (Vercel or self-hosted) shares cache across CI runs—hit rates improve dramatically after the first run
- Packages that share a dependency get rebuilt when that dependency changes, even if their own code did not change
Revisions (0)
No revisions yet.