Skip to content

Lesson 5 of 9 · 26m

CI Strategy, Test Selection, and Merge Queues

Design CI around merge confidence, queue behavior, flaky-test economics, and progressive validation instead of running everything everywhere.

The worst monorepo CI systems are honest in the worst way: they run everything because nobody trusts the graph. That feels safe until queue time becomes the dominant cost and developers start batching unrelated changes. CI design is queueing theory plus risk management. You are buying confidence per minute, not running jobs because they exist.

Separate validation tiers by risk and cost

A mature pipeline usually has tiers:

pre-merge pull request   fast, affected, high-signal
merge queue              broader, serialized against final main state
post-merge main          full build, artifact publication, deploy gates
nightly / scheduled      expensive integration, compatibility, mutation, load

The pull request tier should be quick enough that developers do not context-switch into oblivion. The merge queue protects main from semantic conflicts — two green PRs can still conflict behaviorally after one merges. Post-merge validates artifacts exactly as they will ship. Nightly catches slow-moving incompatibilities that do not belong in the critical path.

Test selection is about dependency truth

Running tests only for affected packages is useful when test inputs are visible. Be careful with cross-package integration tests. A test living in apps/web may depend on packages/auth, but if it uses a generated API client or a Docker compose service, those are inputs too.

Example task configuration:

{
  "tasks": {
    "test": {
      "dependsOn": ["^build"],
      "inputs": ["src/**", "test/**", "package.json", "../../packages/*/src/**"],
      "outputs": ["coverage/**"],
      "env": ["CI", "NODE_ENV"]
    }
  }
}

Do not copy this blindly. The lesson is to model what the test truly reads. Broad inputs reduce false skips but increase cache misses. That trade is often correct for integration tests.

Flaky tests distort the entire system

A two percent flaky test is annoying. Fifty of them turn the merge queue into a casino. Track flakes as operational defects:

test name
failure rate
first failing commit
owner
quarantine status
cost per week in retry minutes

Retries hide pain and multiply cost. Use them sparingly for known external instability, not as a blanket policy. A retry loop that eventually passes is not proof of correctness; it is statistical laundering.

Merge queues are a correctness tool

Without a merge queue, every PR validates against a moving target. With one, changes are tested sequentially against the predicted future state of main:

main + PR-182 + PR-183 -> validate batch
main + PR-182 -> validate fallback or bisect on failure

Batching improves throughput, but failure attribution becomes harder. Good systems bisect failed batches; bad systems merely rerun until the problem looks intermittent. If your monorepo has high merge volume, a queue stops being optional.

Make CI output legible

A failing job should answer three questions quickly: what ran, why it ran, and what command reproduces it locally. Print the affected target list and exact task invocation:

echo "base=$BASE head=$HEAD"
nx show projects --affected --base="$BASE" --head="$HEAD"
nx run-many -t test --projects="$AFFECTED" --parallel=3

If the local reproduction requires undocumented environment variables or a special CI service, engineers will push blind fixes. CI should be a reproducible environment, not a distant oracle.

Field notes

  • The correct number of checks is not "all checks." It is the smallest set that reliably catches catastrophic regression before merge.
  • Queue time is a developer-experience cost even when machine cost looks acceptable.
  • Never let test selection skip generated-code validation if generated artifacts are consumed downstream.
  • Measure end-to-end time from push to mergeable, not just job runtime. Parallel waste can still produce slow outcomes.

Was this lesson helpful?

Sign in to rate