Manual Distributed Task Execution on Jenkins
Using Nx Agents is the easiest way to distribute task execution, but it your organization may not be able to use hosted Nx Agents. You can set up manual distributed task execution on your own CI provider using the recipe below.
Distribute Tasks Across Agents on Jenkins
Run agents directly on Jenkins with 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 --distribute-on='manual' --stop-agents-after='e2e-ci'"
17 sh "npx nx-cloud record -- nx format:check"
18 sh "npx nx affected --base=HEAD~1 -t lint,test,build,e2e-ci --configuration=ci --parallel=2"
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 --distribute-on='manual' --stop-agents-after='e2e-ci'"
29 sh "npx nx-cloud record -- nx format:check"
30 sh "npx nx affected --base origin/${env.CHANGE_TARGET} -t lint,test,build,e2e-ci --parallel=2 --configuration=ci"
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.
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.
Rerunning jobs with DTE
Rerunning only failed jobs results in agent jobs not running, which causes the CI pipeline to hang and eventually timeout. This is a common pitfall when using a CI providers "rerun failed jobs", or equivalent, feature since agent jobs will always complete successfully.
To enforce rerunning all jobs, you can set up your CI pipeline to exit early with a helpful error. For example:
You reran only failed jobs, but CI requires rerunning all jobs. Rerun all jobs in the pipeline to prevent this error.
At a high level:
- Create a job that always succeeds and uploads an artifact on the pipeline with the run attempt number of the pipeline.
- The main and agent jobs can read the artifact file when starting and assert they are on the same re-try attempt.
- If the reattempt number does not match, then error with a message stating to rerun all jobs. Otherwise, the pipelines are on the same rerun and can proceed as normally.