Configuring CI Using CircleCI and Nx
The CircleCI
can track the last successful run on the main
branch and use this as a reference point for the BASE
. The Nx Orb
provides a convenient implementation of this functionality which you can drop into your existing CI config. To understand why knowing the last successful build is important for the affected command, check out the in-depth explanation in Orb's docs.
Below is an example of a Circle CI setup for an Nx workspace - building and testing only what is affected. For more details on how the orb is used, head over to the official docs.
1version: 2.1
2orbs:
3 nx: nrwl/nx@1.5.1
4jobs:
5 main:
6 docker:
7 - image: cimg/node:lts-browsers
8 steps:
9 - checkout
10 - run: npm ci
11 - nx/set-shas
12
13 - run: npx nx format:check
14 - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint --parallel=3
15 - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t test --parallel=3 --configuration=ci
16 - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t build --parallel=3
17workflows:
18 build:
19 jobs:
20 - main
21
The pr
and main
jobs implement the CI workflow.
Using CircleCI on the private repository
To use the Nx Orb with a private repository on your main branch, you need to grant the orb access to your CircleCI API. You can do this by creating an environment variable called CIRCLE_API_TOKEN
in the context or the project.
It should be a user token, not the project token.
Distributed CI with Nx Cloud
Read more about Distributed Task Execution (DTE).
1version: 2.1
2orbs:
3 nx: nrwl/nx@1.5.1
4jobs:
5 agent:
6 docker:
7 - image: cimg/node:lts-browsers
8 parameters:
9 ordinal:
10 type: integer
11 steps:
12 - checkout
13 - run: npm ci
14 - run:
15 command: npx nx-cloud start-agent
16 no_output_timeout: 60m
17 main:
18 docker:
19 - image: cimg/node:lts-browsers
20 steps:
21 - checkout
22 - run: npm ci
23 - nx/set-shas
24 - run: npx nx-cloud start-ci-run --stop-agents-after="build"
25
26 - run: npx nx-cloud record -- npx nx format:check
27 - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint --parallel=3 & npx nx affected --base=$NX_BASE --head=$NX_HEAD -t test --parallel=3 --configuration=ci & npx nx affected --base=$NX_BASE --head=$NX_HEAD -t build --parallel=3
28workflows:
29 build:
30 jobs:
31 - agent:
32 matrix:
33 parameters:
34 ordinal: [1, 2, 3]
35 - main
36
You can also use our ci-workflow generator to generate the configuration file.