Configuring CI Using CircleCI and Nx

Nx is a smart, fast and extensible build system, and it works really well with monorepos. Monorepos provide a lot of advantages:

  • Everything at that current commit works together. Changes can be verified across all affected parts of the organization.
  • Easy to split code into composable modules
  • Easier dependency management
  • One toolchain setup
  • Code editors and IDEs are "workspace" aware
  • Consistent developer experience
  • And more ...

But they come with their own technical challenges. The more code you add into your repository, the slower the CI gets. Adding Nx to your CI pipeline makes this more efficient.

Setting up CircleCI

The CircleCI can track the last successful run on main branch and use this as a reference point for the BASE. The Nx Orb provides conventient implementation of this functionality which you can drop into you existing CI config. To understand why knowing the last successful build is important for the affected command, check out the in-depth explanation at Orb's docs.

Below is an example of a Circle CI setup for an Nx workspace only building and testing 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.4.0
4jobs:
5  main:
6    steps:
7      - checkout
8      - run: npm install
9      - nx/set-shas
10      - run: npx nx affected --base=$NX_BASE --target=build --parallel=3
11      - run: npx nx affected --base=$NX_BASE --target=test --parallel=2
12  pr:
13    steps:
14      - checkout
15      - run: npm install
16      - nx/set-shas
17      - run: npx nx affected --base=$NX_BASE --target=build --parallel=3
18      - run: npx nx affected --base=$NX_BASE --target=test --parallel=2
19workflows:
20  build:
21    jobs:
22      - main:
23          filters:
24            branches:
25              only: main
26      - pr:
27          filters:
28            branches:
29              ignore: main

The pr and main jobs implement the CI workflow.

Using CircleCI on 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.

Note: It should be a user token, not project token.

Distributed CI with Nx Cloud

A computation cache is created on your local machine to make the developer experience faster. This allows you to not waste time re-building, re-testing, re-linting, or any number of other actions you might take on code that hasn't changed. Because the cache is stored locally, you are the only member of your team that can take advantage of these instant commands. You can manage and share this cache manually.

Nx Cloud allows this cache to be shared across your entire organization, meaning that any cacheable operation completed on your workspace only needs to be run once. Nx Cloud also allows you to distribute your CI across multiple machines to make sure the CI is fast even for very large repos.

Learn more about configuring your CI environment using Nx Cloud with Distributed Caching and Distributed Task Execution in the Nx Cloud docs.