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 - uses: actions/setup-node@v3
22 with:
23 node-version: 20
24 cache: 'npm'
25 # This line enables distribution
26 # The "--stop-agents-after" is optional, but allows idle agents to shut down once the "e2e-ci" targets have been requested
27 # - run: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="e2e-ci"
28 - run: npm ci
29
30 - uses: nrwl/nx-set-shas@v3
31
32 # This line is needed for nx affected to work when CI is running on a PR
33 - run: git branch --track main origin/main
34 if: ${{ github.event_name == 'pull_request' }}
35
36 - run: npx nx-cloud record -- nx format:check
37 - run: npx nx affected -t lint test build e2e-ci
38
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.