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
2
3on:
4 push:
5 branches:
6 - main
7 pull_request:
8
9permissions:
10 actions: read
11 contents: read
12
13jobs:
14 main:
15 runs-on: ubuntu-latest
16 steps:
17 - uses: actions/checkout@v4
18 with:
19 filter: tree:0
20 fetch-depth: 0
21
22 # This enables task distribution via Nx Cloud
23 # Run this command as early as possible, before dependencies are installed
24 # Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun
25 # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution
26 # - run: npx nx start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build"
27
28 # Cache node_modules
29 - uses: actions/setup-node@v4
30 with:
31 node-version: 20
32 cache: 'npm'
33
34 - run: npm ci --legacy-peer-deps
35 - uses: nrwl/nx-set-shas@v4
36
37 # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud
38 # - run: npx nx-cloud record -- echo Hello World
39 - run: npx nx affected -t lint test build
40 # Nx Cloud recommends fixes for failures to help you get CI green faster. Learn more: https://nx.dev/ci/features/self-healing-ci
41 - run: npx nx fix-ci
42 if: always()
43
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.