Sub topic 1
Why monorepos need selective CI
Prerequisites
This chapter assumes you can read a basic GitHub Actions workflow (on, jobs, steps, runs-on). It also assumes you know Git branches, pull requests, and diffs, and that you can read YAML. Familiarity with a repository that holds several projects or packages helps, because every example here uses one.
The problem
A monorepo is a single repository that holds several projects, such as an API, a web app, and shared libraries. Teams choose it for atomic changes across projects, one place for tooling, and simpler dependency sharing.
The cost shows up in CI. A default workflow runs on every push and pull request, regardless of which files changed. In a monorepo, a one-line fix in the web app then triggers the API tests, the billing tests, and every other job.
Consider a repository with this layout:
services/api/
services/billing/
apps/web/
libs/shared/
.github/workflows/A typical unfiltered workflow looks like this:
name: ci
on:
pull_request:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: make -C services/api testThis runs on a README typo, a web app change, and a billing change. Only the API change needs it.
What it costs
Running everything on every change wastes resources in three ways.
| Cost | Effect |
|---|---|
| Runner minutes | Paid minutes are spent on jobs that cannot fail from the change. |
| Wait time | Developers wait for unrelated jobs, or for queued runners. |
| Noise | Unrelated flaky tests fail a pull request that never touched them. |
The costs grow with the repository. Each new project adds jobs to every pull request, so the waste scales with project count times change count.
The goal: run what the change affects
Selective CI runs only the work a change can affect. You apply it at two levels, and the rest of this tutorial builds on both.
- Workflow level. Path filters on the trigger decide whether a workflow starts at all.
- Job level. A change-detection job decides which jobs inside a workflow run, using
ifconditions on its outputs.
The trade-off
Selection introduces a risk: skipping too much. A change to libs/shared can break the API and the web app, even though no file under either was touched. A filter that ignores this lets broken code merge. Each technique in this tutorial has to answer two questions: what can I skip safely, and what must still run?
Selection also interacts with branch protection. A workflow that never starts, or a job that is skipped, reports differently from one that passed. A required status check can then block a pull request forever, or let one through that you wanted blocked. Later chapters cover both problems and show a gate job that solves them.
Roadmap
The following chapters proceed in this order:
- Triggers and glob patterns, then workflow-level
pathsandpaths-ignore. - Where workflow-level filters fall short.
- Job-level
if, job outputs, and a change-detection job. - Skipped jobs,
needs, and the gate job for required checks. - Shared code, dynamic matrices, and reusable workflows.
Exercise
A pull request changes only apps/web/src/App.tsx. Your unfiltered workflow runs API, billing, and web tests. Name one cost this creates, and one risk you must still guard against once you start skipping jobs.
Show answer
The cost can be wasted runner minutes, longer waits, or unrelated flaky failures, since the API and billing tests run on a change they cannot be affected by. The risk is skipping too much, for example not running dependent projects when a shared library or the workflow file changes. A skipped or never-started workflow can also interact badly with required status checks.