# 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 convenient 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](https://github.com/nrwl/nx-orb#background). 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](https://circleci.com/developer/orbs/orb/nrwl/nx). ```yaml version: 2.1 orbs: nx: nrwl/nx@1.4.0 jobs: main: docker: - image: cimg/node:lts-browsers steps: - checkout - run: npm ci - nx/set-shas - run: npx nx workspace-lint - run: npx nx format:check - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=lint --parallel=3 - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=test --parallel=3 --ci --code-coverage - run: npx nx affected --base=$NX_BASE --head=$NX_HEAD --target=build --parallel=3 workflows: build: jobs: - main ``` The `pr` and `main` jobs implement the CI workflow. ### Using CircleCI on private repository To use the [Nx Orb](https://github.com/nrwl/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.