When our monorepo passed 60 packages, every pull request ran every test suite, and a one-line README change took 35 minutes of CI. The obvious fix, "only test what changed", is easy to get half right. Our first attempt used path filters, and within a month a change to a shared validation library merged green while breaking two services that imported it. Their tests had not run because their folders had not changed.
Running less CI safely needs three things: knowing which packages a change affects through the dependency graph, keeping required checks meaningful when jobs are skipped, and a full run on a schedule to catch what the graph does not know. This guide covers each with GitHub Actions and GitLab examples. Repository layout itself is covered in post 612.
Level 1: path filters
Both platforms can skip a whole workflow or job based on changed paths.
# GitHub Actions: run this workflow only when the API or shared libs change
on:
pull_request:
paths:
- "services/api/**"
- "packages/shared-*/**"
- "pnpm-lock.yaml"
# GitLab: run the job only when relevant files change against main
test-api:
rules:
- changes:
paths:
- services/api/**/*
- packages/shared-*/**/*
- pnpm-lock.yaml
compare_to: refs/heads/main
script: [pnpm --filter api test]
In GitLab, compare_to matters for branch pipelines; without it, a branch pipeline compares against the previous push, and a new branch can evaluate changes as true for everything. Merge request pipelines compare against the target branch already.
Path filters work for leaf services. They fail for shared code, because you have to remember to list every consumer's dependency paths by hand, and that list rots.
Level 2: dependency-aware "affected"
Workspace tools already know the dependency graph from package.json workspaces or project configuration. They can compute the set of projects affected by changed files, including everything that depends on them.
# Nx: run test for projects affected since main
npx nx affected -t lint test build --base=origin/main --head=HEAD
# Turborepo: run tasks for packages changed since main and their dependents
npx turbo run lint test build --affected
Turborepo's --affected compares against the default branch (configurable with TURBO_SCM_BASE); the older filter syntax --filter=...[origin/main] does the same explicitly. Both tools need enough git history to find the merge base, so fetch depth matters:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # or fetch main explicitly; shallow clones break affected detection
On GitLab, set GIT_DEPTH: 0 for the job, or fetch the target branch in before_script.
For Go workspaces or polyglot repos without Nx or Turborepo, Bazel, Pants and similar build systems compute affected targets from their own graphs. A small script that maps changed files to modules and walks go list -deps is also workable for a handful of modules.
Level 3: remote cache
"Affected" decides what to run. A remote cache decides whether running it costs anything. Nx and Turborepo hash each task's inputs (source files, dependencies, environment variables you declare) and store outputs and logs. When the same hash appears again, on another branch or another developer's machine, the result is replayed in seconds.
{
"$schema": "https://turborepo.com/schema.json",
"tasks": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"]
},
"test": {
"dependsOn": ["build"],
"inputs": ["src/**", "test/**", "package.json"],
"env": ["NODE_ENV"]
}
}
}
The cache is only correct if inputs are declared completely. An environment variable that changes behaviour but is not listed in env lets a stale result replay. We treat changes to turbo.json or nx.json inputs as production changes and review them as such.
Remote cache options include the vendors' hosted services and self-hosted implementations of their cache APIs. Restrict write access to CI on trusted branches; developers and pull requests from forks should read only, otherwise anyone could plant a poisoned artifact under a valid hash.
Keeping required checks honest
This is where most setups quietly break. Branch protection on GitHub requires named status checks. If a workflow is skipped because of paths: filters, its checks never report, and a required check stays "Expected — Waiting for status to be reported", blocking the merge. Teams then remove the check from required, and the gate is gone.
Two patterns that work:
One always-running gate job. Run a single workflow on every pull request. It computes affected projects, runs them, and ends with a job that is the only required check:
jobs:
affected:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: npx turbo run lint test build --affected
ci-gate:
if: always()
needs: [affected]
runs-on: ubuntu-24.04
steps:
- run: |
test "${{ needs.affected.result }}" = "success" || exit 1
if: always() makes the gate report even when upstream jobs fail or are skipped, and it fails unless they succeeded.
Skipped jobs inside a running workflow. A job skipped by an if: condition reports as "Success" (skipped) to branch protection, unlike a workflow skipped by path filters. Moving filters from on.paths into job-level conditions keeps the checks present.
On GitLab, the "Pipelines must succeed" merge check looks at the pipeline as a whole, so jobs excluded by rules:changes simply do not appear. The risk there is the opposite: a pipeline with nothing relevant in it passes trivially. Keep at least one always-running job that validates the affected set is not empty when code changed.
The nightly full run
The dependency graph does not know everything. Things it commonly misses:
a shared Docker base image or CI template changed outside the workspace;
a tool version bump in
.tool-versionsor the runner image;tests that depend on external services, data fixtures or time;
runtime coupling through HTTP or events, which no import graph captures.
We run the full suite on main every night and on every release branch cut:
on:
schedule:
- cron: "17 2 * * *"
workflow_dispatch:
A failure in the nightly run that did not show up in any pull request is treated as a bug in our affected configuration, and we add the missing input or dependency edge.
What it bought us
Metric | Before | After |
|---|---|---|
Median PR CI time | 35 min | 6 min |
PRs running full suite | 100% | about 8% (shared libs, lock file) |
Remote cache hit rate on PRs | n/a | about 70% of tasks |
Breakages found only by nightly | n/a | 3 in the first quarter, each fixed in config |
Common mistakes
Mistake | Why it hurts | Do this instead |
|---|---|---|
Hand-written path lists for shared code | Consumers of shared libs are not tested | Use the workspace graph (affected) |
Shallow clone in CI | Affected falls back to everything or nothing |
|
| Checks never report, merges blocked | One always-running gate job |
Undeclared task inputs | Remote cache replays stale results | Declare inputs and env explicitly |
No full run at all | Graph blind spots go unnoticed | Nightly full suite on main |
Key takeaways
Path filters suit leaf services; shared code needs dependency-aware affected detection.
Fetch enough git history for the tool to find the merge base.
A remote cache turns repeated work into replays, but only with complete input declarations.
Make one gate job the required check so skipped work never blocks or bypasses protection.
Keep a nightly full run and treat its surprises as configuration bugs.
References
GitHub Docs, Workflow syntax, on.pull_request.paths: https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax
GitHub Docs, Troubleshooting required status checks: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/troubleshooting-required-status-checks
GitLab Docs, rules:changes and compare_to: https://docs.gitlab.com/ci/yaml/#ruleschanges
Nx Docs, Run only tasks affected by a PR: https://nx.dev/docs/features/ci-features/affected
Turborepo Docs, Constructing CI: https://turborepo.com/docs/crafting-your-repository/constructing-ci
Turborepo Docs, Caching: https://turborepo.com/docs/crafting-your-repository/caching
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.