Configuring CI Using Circle CI and Nx

Below is an example of an Circle CI setup, building and testing only what is affected.

.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: npx nx-cloud start-ci-run --distribute-on="5 linux-medium-js" --stop-agents-after="build" # this line enables distribution 11 - run: npm ci 12 - nx/set-shas 13 14 - run: npx nx-cloud record -- nx format:check 15 - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD -t lint test build --parallel=3 16workflows: 17 build: 18 jobs: 19 - main 20

Get the Commit of the Last Successful Build

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. Specifically, nx/set-shas populates the $NX_BASE environment variable with the commit SHA of the last successful run.

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.