Run only affected Nx projects in CI using `nx affected` with a base/head comparison

domain: nx.dev/ci/features/affected · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Ensure Nx is configured with a `nx.json` that defines `targetDefaults` and that each project's `project.json` declares its `build`, `test`, and `lint` targets
  2. In CI, set the comparison base to the last successful commit on the main branch (for PRs use `--base=origin/main`) and head to the current commit (`--head=HEAD`)
  3. Run `npx nx affected -t build test lint --base=origin/main --head=HEAD` to execute those targets only for projects whose source files changed
  4. Use Nx Cloud (or a self-hosted remote cache) so that unaffected-but-previously-built artifacts are served from cache, making subsequent runs nearly instant
  5. For CI on the main branch after merge, use the previous successful commit SHA as `--base` to catch all changes since the last green build
  6. Visualize the affected graph with `npx nx affected:graph --base=origin/main` to understand which projects are included and why

Known gotchas

Related routes

Build an affected-only CI pipeline for a Turbo or Nx monorepo with change detection
nx.dev · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans