Configuring CI Using Jenkins 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 Jenkins

Below is an example of a Jenkins setup for an Nx workspace only building and testing what is affected.

Unlike GitHub Actions and CircleCI, you don't have the metadata to help you track the last successful run on main. In the example below, the base is set to HEAD~1 (for push) or branching point (for pull requests), but a more robust solution would be to tag a SHA in the main job once it succeeds, and then use this tag as a base. See the nx-tag-successful-ci-run and nx-set-shas (version 1 implements tagging mechanism) repos for more information.

We also have to set NX_BRANCH explicitly.

1pipeline {
2    agent none
3    environment {
4        NX_BRANCH = env.BRANCH_NAME.replace('PR-', '')
5    }
6    stages {
7        stage('Pipeline') {
8            parallel {
9                stage('Main') {
10                    when {
11                        branch 'main'
12                    }
13                    agent any
14                    steps {
15                        sh "npm ci"
16                        sh "npx nx workspace-lint"
17                        sh "npx nx format:check"
18                        sh "npx nx affected --base=HEAD~1 --target=lint --parallel=3"
19                        sh "npx nx affected --base=HEAD~1 --target=test --parallel=3"
20                        sh "npx nx affected --base=HEAD~1 --target=build --parallel=3"
21                    }
22                }
23                stage('PR') {
24                    when {
25                        not { branch 'main' }
26                    }
27                    agent any
28                    steps {
29                        sh "npm ci"
30                        sh "npx nx workspace-lint"
31                        sh "npx nx format:check"
32                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=lint --parallel=3"
33                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=test --parallel=3 --ci  --code-coverage"
34                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=build --parallel=3"
35                    }
36                }
37            }
38        }
39    }

The pr and main jobs implement the CI workflow.

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.

In order to use distributed task execution, we need to start agents and set the NX_CLOUD_DISTRIBUTED_EXECUTION flag to true. Once all tasks are finished, we must not forget to cleanup used agents.

1pipeline {
2    agent none
3    environment {
4        NX_BRANCH = env.BRANCH_NAME.replace('PR-', '')
6    }
7    stages {
8        stage('Pipeline') {
9            parallel {
10                stage('Main') {
11                    when {
12                        branch 'main'
13                    }
14                    agent any
15                    steps {
16                        sh "npm ci"
17                        sh "npx nx-cloud start-ci-run"
18                        sh "npx nx workspace-lint"
19                        sh "npx nx format:check"
20                        sh "npx nx affected --base=HEAD~1 --target=lint --parallel=3"
21                        sh "npx nx affected --base=HEAD~1 --target=test --parallel=3 --ci --code-coverage"
22                        sh "npx nx affected --base=HEAD~1 --target=build --parallel=3"
23                        sh "npx nx-cloud stop-all-agents"
24                    }
25                }
26                stage('PR') {
27                    when {
28                        not { branch 'main' }
29                    }
30                    agent any
31                    steps {
32                        sh "npm ci"
33                        sh "npx nx-cloud start-ci-run"
34                        sh "npx nx workspace-lint"
35                        sh "npx nx format:check"
36                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=lint --parallel=3"
37                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=test --parallel=3 --ci --code-coverage"
38                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=build --parallel=3"
39                        sh "npx nx-cloud stop-all-agents"
40                    }
41                }
42            }
43        }
44    }

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