Interview Prep/GitHub Actions/Debugging, Cost, Monorepos & Releases

How do you build only the services that changed in a monorepo?

AdvancedConceptDebugging, Cost, Monorepos & Releases
Answer

Compute changed packages in a first job and feed a dynamic matrix:

jobs:
  changes:
    runs-on: ubuntu-24.04
    outputs:
      services: ${{ steps.filter.outputs.changes }}
    steps:
      - uses: actions/checkout@v4
      - id: filter
        uses: dorny/paths-filter@v3
        with:
          filters: |
            api: ['services/api/**', 'libs/common/**']
            web: ['apps/web/**', 'libs/common/**']
  build:
    needs: changes
    if: needs.changes.outputs.services != '[]'
    strategy:
      matrix:
        service: ${{ fromJSON(needs.changes.outputs.services) }}

Include shared libraries in each filter, and finish with a fan-in ci-ok job so required checks work even when nothing changed. Build tools like Nx or Turborepo (affected) do the same from the dependency graph.

All GitHub Actions questions Roadmap: GitHub Actions

More on Debugging, Cost, Monorepos & Releases