All guides

Monorepo pipelines: build only what changed

GitHub Actions can limit a monorepo pipeline to what changed at three levels: the workflow, the job and the matrix. The first one, a path filter, leaves required status checks pending when it skips the workflow.

A laptop showing a stylesheet in a code editor

Path filters on the workflow

paths on a push or pull_request trigger names file path patterns. The workflow runs if at least one changed path matches.

YAML

on:
  pull_request:
    paths:
      - 'services/api/**'
      - 'libs/shared/**'
      - '!services/api/docs/**'

The directory names are for you to replace. paths-ignore works the other way: the workflow does not run when all changed paths match its patterns. One event cannot use both keys. To include and exclude at once, use paths with patterns that start with !. Order matters: a matching ! pattern after a positive match excludes the path, and a positive match after it includes the path again.

GitHub builds the list of changed files from a three-dot diff for pull requests and a two-dot diff for pushes. Three limits change the outcome. A push of more than 1,000 commits always runs the workflow. So does a diff that times out. If the diff has more than 3,000 files and the matching files are not among the first 3,000, the workflow does not run.

Required checks and skipped workflows

Path filters conflict with required status checks. GitHub's documentation states it directly: a workflow skipped because of path filtering, branch filtering or a commit message leaves its checks in the "Pending" state. A pull request that requires those checks is blocked with "Waiting for status to be reported".

GitHub's own fix is to avoid requiring workflows that can be skipped. The same page lists a second behavior: a job that is skipped by a conditional reports "Success". To keep a required check and still skip work, start the workflow on every pull request and decide inside it which jobs run.

Changes detected per job

A first job detects what changed and publishes the result as job outputs. The other jobs read the outputs in if.

YAML

on:
  pull_request:

jobs:
  changes:
    runs-on: ubuntu-latest
    permissions:
      pull-requests: read
    outputs:
      api: ${{ steps.filter.outputs.api }}
      packages: ${{ steps.filter.outputs.changes }}
    steps:
      - uses: dorny/paths-filter@v4
        id: filter
        with:
          filters: |
            api:
              - 'services/api/**'
              - 'libs/shared/**'
            web:
              - 'services/web/**'
              - 'libs/shared/**'

  api-checks:
    needs: changes
    if: needs.changes.outputs.api == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - run: npm ci
        working-directory: services/api
      - run: npm test
        working-directory: services/api

dorny/paths-filter is a third-party action. Its README states that on pull request events it fetches the list of modified files from the REST API, requires the pull-requests: read permission and needs no checkout. On other events it uses git commands, and the repository must already be checked out. Review its source and pin it to a full commit SHA.

Git alone gives the same answer. actions/checkout fetches a single commit by default, and fetch-depth: 0 fetches all history for all branches and tags:

YAML

steps:
  - uses: actions/checkout@v7
    with:
      fetch-depth: 0
  - id: diff
    env:
      BASE_REF: ${{ github.base_ref }}
    run: |
      if ! git diff --quiet "origin/$BASE_REF...HEAD" -- services/api libs/shared; then
        echo "api=true" >> "$GITHUB_OUTPUT"
      fi

github.base_ref is the target branch of the pull request. git diff --quiet disables all output and exits with 1 if there are differences.

A matrix over the changed packages

The action's changes output is a JSON array with the names of all filters that matched a changed file. In its README, a matrix reads that array, and one job definition runs for each changed package:

YAML

  build:
    needs: changes
    if: needs.changes.outputs.packages != '[]'
    runs-on: ubuntu-latest
    strategy:
      matrix:
        package: ${{ fromJSON(needs.changes.outputs.packages) }}
    steps:
      - uses: actions/checkout@v7
      - run: npm ci
        working-directory: services/${{ matrix.package }}
      - run: npm test
        working-directory: services/${{ matrix.package }}

The if line is an addition to the README's example. GitHub evaluates a job's if before it applies the matrix, and the action writes [] when no filter matched. Each filter here is named after its directory, so the matrix value is also the path.

One check to require

GitHub's page on required checks lists a third behavior: a job that depends on a failed job is skipped and may not block merging. For a required check that depends on other jobs, it tells you to combine always() with needs. Add one such job and make it the required check:

YAML

  ci-ok:
    needs: [changes, api-checks, build]
    if: always()
    runs-on: ubuntu-latest
    steps:
      - if: contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')
        run: exit 1

needs.<job_id>.result is success, failure, cancelled or skipped. The step fails the job when any job it needs failed or was canceled.

Mistakes to avoid

  1. A shared library missing from a filter. A filter matches file paths and nothing else, so list every shared path under each package that uses it. Build tools have their own commands for this: in Nx, nx affected -t test selects the tasks whose inputs include a file your branch changed, and in Turborepo, turbo run test --affected filters to the packages affected by changes on the current branch.
  2. Workflow files left out. Add .github/workflows/** to each filter if a change to the pipeline has to run every package.
  3. A required workflow that a path filter can skip. Its checks stay pending and the pull request cannot merge.
  4. File lists pasted into a script. The README of dorny/paths-filter warns that file names in its outputs can be attacker-influenced on pull requests. Pass them through env, never through ${{ }} inside run.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides