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 NX_CLOUD_DISTRIBUTED_EXECUTION = true
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 }
45}
46