Configuring CI Using GitHub Actions and Nx

Below is an example of a GitHub Actions setup, building, and testing only what is affected.

.github/workflows/ci.yml
1name: CI 2on: 3 push: 4 branches: 5 # Change this if your primary branch is not main 6 - main 7 pull_request: 8 9# Needed for nx-set-shas when run on the main branch 10permissions: 11 actions: read 12 contents: read 13 14jobs: 15 main: 16 runs-on: ubuntu-latest 17 steps: 18 - uses: actions/checkout@v4 19 with: 20 fetch-depth: 0 21 filter: tree:0 22 23 - uses: actions/setup-node@v3 24 with: 25 node-version: 20 26 cache: 'npm' 27 # This line enables distribution 28 # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested 29 # - run: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" 30 - run: npm ci 31 32 - uses: nrwl/nx-set-shas@v4 33 34 - run: npx nx-cloud record -- nx format:check 35 - run: npx nx affected -t lint test build e2e-ci 36

Get the Commit of the Last Successful Build

The GitHub can track the last successful run on the main branch and use this as a reference point for the BASE. The nrwl/nx-set-shas provides a convenient implementation of this functionality, which you can drop into your existing CI workflow.

To understand why knowing the last successful build is important for the affected command, check out the in-depth explanation in Actions's docs.