Configuring CI Using CircleCI and Nx

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 Nx orb is used, head over to the official docs.

.circleci/config.yml
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,test,build --parallel=3 --configuration=ci 15workflows: 16 build: 17 jobs: 18 - main 19

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.

Using CircleCI in a 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.

Caution

It should be a user token, not the project token.

Distributed Task Execution with Nx Cloud

Read more about Distributed Task Execution (DTE).

.circleci/config.yml
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,test,build --parallel=3 --configuration=ci 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.