Prompt
I'm building CI/CD for a monorepo — how do I avoid slow builds?
Latest observation
To keep CI/CD fast in a monorepo, the goal is to build only what changed, reuse as much as possible, and run the slowest checks only when necessary.
Here are the biggest wins:
1) Use change detection to scope work
Only run tests/builds for affected packages/services.
- Detect changed files via git diff against the base branch
- Map paths to packages
- Trigger only the jobs needed for those packages
Examples:
apps/web/**changed → run web build/testpackages/ui/**changed → run UI package checks and anything depending on it- docs-only changes → skip heavy pipeline stages
If your tool supports it, use affected graph features:
- Nx:
nx affected - Turborepo:
turbo run ... --filter=...[base] - Bazel: target graph + incremental builds
2) Cache everything you can
Caching usually gives the largest speedup.
Cache dependency installs
npm,pnpm,yarncache- lockfile-based installs
- Prefer
pnpmfor monorepos if it fits your stack, because it deduplicates well
Cache build artifacts
Cache outputs like:
dist/- compiled packages
- test transpilation caches
- framework caches like
.next/cache,vitecache,jestcache
Cache by lockfile + source inputs
Make sure cache keys include:
- lockfile hash
- relevant package manifests
- build config
- source content hash where appropriate
3) Split the pipeline into stages
Don’t make every PR do everything.
Typical structure:
- Lint / typecheck / unit tests on affected packages
- Build only affected apps/packages
- Integration/e2e only when needed
- Deploy only from main or tagged releases
Use conditional logic:
- PRs: quick validation
- main branch: fuller validation
- release tags: full release pipeline
4) Run jobs in parallel
Monorepos often get slow because everything is serialized.
- Run independent package jobs concurrently
- Parallelize test shards
- Split large test suites by duration
- Use multiple CI executors if cost-effective
5) Avoid rebuilding shared deps unnecessarily
If a library changes, only downstream consumers should rebuild.
This is where a dependency graph matters:
- if
packages/corechanges, rebuild apps that depend on it - if
packages/authchanges, don’t rebuild unrelated services
Tools like Nx/Bazel/Turborepo handle this well if set up properly.
6) Keep expensive checks off the critical path
Examples:
- run full e2e nightly instead of every PR
- run security scanning asynchronously
- generate docs previews only when docs change
- run codegen only when schema files change
7) Make artifacts reusable between stages
Build once, deploy many.
Example:
- build an app in CI
- store artifact
- deploy the exact same artifact to staging/prod
This avoids rebuilding on deployment and reduces drift.
8) Use shallow, targeted test strategies
- Unit tests for changed packages
- contract tests for shared interfaces
- e2e only for key user flows
- snapshot tests only where they add value
A small number of high-signal tests is often better than a giant slow suite.
9) Reduce workspace overhead
Some monorepos are slow because of tooling overhead.
- Don’t install unnecessary package scripts in every job
- Avoid repeated bootstrap steps
- Use workspace filters
- Use smaller containers/images with preinstalled toolchains
- Keep CI images warm if your provider supports it
10) Measure and enforce performance
Track:
- total pipeline duration
- cache hit rate
- average job runtime
- “affected” job count per PR
- queue time vs execution time
Without measurement, it’s hard to know what helped.
A practical CI pattern
A good default pattern:
-
On PR
- install deps with cache
- compute affected packages
- lint/typecheck/test only affected
- build only affected
- skip e2e unless files touching critical paths changed
-
On main
- run full validation or broader affected set
- build release artifacts
- publish if green
-
Nightly
- full repo e2e
- security scans
- dependency checks
If you want the biggest first step
If you only do three things, do these:
- Affected-only builds/tests
- Dependency + build cache
- Parallelize jobs
Those usually cut monorepo CI time dramatically.
If you want, I can give you:
- a GitHub Actions example
- an Nx/Turborepo setup
- or a CI design for your specific monorepo structure