Configuring CI Using Jenkins and Nx

There are two general approaches to setting up CI with Nx - using a single job or distributing tasks across multiple jobs. For smaller repositories, a single job is faster and cheaper, but once a full CI run starts taking 10 to 15 minutes, using multiple jobs becomes the better option. Nx Cloud's distributed task execution allows you to keep the CI pipeline fast as you scale. As the repository grows, all you need to do is add more agents.

Process Only Affected Projects With One Job on Jenkins

Below is an example of an Jenkins setup that runs on a single job, building and testing only what is affected. This uses the nx affected command to run the tasks only for the projects that were affected by that PR.

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 format:check" 17 sh "npx nx affected --base=HEAD~1 -t lint --parallel=3" 18 sh "npx nx affected --base=HEAD~1 -t test --parallel=3" 19 sh "npx nx affected --base=HEAD~1 -t build --parallel=3" 20 } 21 } 22 stage('PR') { 23 when { 24 not { branch 'main' } 25 } 26 agent any 27 steps { 28 sh "npm ci" 29 sh "npx nx format:check" 30 sh "npx nx affected --base origin/${env.CHANGE_TARGET} -t lint --parallel=3" 31 sh "npx nx affected --base origin/${env.CHANGE_TARGET} -t test --parallel=3 --configuration=ci" 32 sh "npx nx affected --base origin/${env.CHANGE_TARGET} -t build --parallel=3" 33 } 34 } 35 } 36 } 37 } 38} 39

Get the Commit of the Last Successful Build

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 an 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) repositories for more information.

We also have to set NX_BRANCH explicitly.

Distribute Tasks Across Agents on Jenkins

To set up Distributed Task Execution (DTE), you can copy and paste the workflow below:

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

This configuration is setting up two types of jobs - a main job and three agent jobs.

The main job tells Nx Cloud to use DTE and then runs normal Nx commands as if this were a single pipeline set up. Once the commands are done, it notifies Nx Cloud to stop the agent jobs.

The agent jobs set up the repo and then wait for Nx Cloud to assign them tasks.

Two Types of Parallelization

The agents and the --parallel flag both parallelize tasks, but in different ways. The way this workflow is written, there will be 3 agents running tasks and each agent will try to run 2 tasks at once. If a particular CI run only has 2 tasks, only one agent will be used.