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 # Enable Nx Cloud recording to track build performance over time 35 # This requires connecting your workspace to Nx Cloud. Run "nx connect" and set the NX_CLOUD_ACCESS_TOKEN env variable in your CI 36 # - run: npx nx-cloud record -- nx format:check 37 38 # Without Nx Cloud, run format:check directly 39 - run: npx nx format:check 40 - run: npx nx affected -t lint test build e2e-ci 41

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.