Configuring CI Using Jenkins and Nx

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    }
40}
41

The pr and main jobs implement the CI workflow.

Distributed CI with Nx Cloud

In order to use distributed task execution, we need to start agents and set the NX_CLOUD_DISTRIBUTED_EXECUTION flag to true.

Read more about the Distributed CI setup with Nx Cloud.

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-cloud start-ci-run --stop-agents-after='build'"
17                        sh "npx nx workspace-lint"
18                        sh "npx nx format:check"
19                        sh "npx nx affected --base=HEAD~1 --target=lint --parallel=3"
20                        sh "npx nx affected --base=HEAD~1 --target=test --parallel=3 --ci --code-coverage"
21                        sh "npx nx affected --base=HEAD~1 --target=build --parallel=3"
22                    }
23                }
24                stage('PR') {
25                    when {
26                        not { branch 'main' }
27                    }
28                    agent any
29                    steps {
30                        sh "npm ci"
31                        sh "npx nx-cloud start-ci-run --stop-agents-after='build'"
32                        sh "npx nx workspace-lint"
33                        sh "npx nx format:check"
34                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=lint --parallel=3"
35                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=test --parallel=3 --ci --code-coverage"
36                        sh "npx nx affected --base origin/${env.CHANGE_TARGET} --target=build --parallel=3"
37                    }
38                }
39
40                # Add as many agent you want
41                stage('Agent1') {
42                   agent any
43                   steps {
44                    sh "npm ci"
45                    sh "npx nx-cloud start-agent"
46                   }
47                }
48                stage('Agent2') {
49                   agent any
50                   steps {
51                    sh "npm ci"
52                    sh "npx nx-cloud start-agent"
53                   }
54                }
55                stage('Agent3') {
56                   agent any
57                   steps {
58                    sh "npm ci"
59                    sh "npx nx-cloud start-agent"
60                   }
61                }
62            }
63        }
64    }
65}
66