Pipeline - Parallel execution of tasks

Article ID:230922168
1 minute readKnowledge base

Issue

  • I want to execute parallel tasks in Pipeline

Resolution

You need to use the parallel directive. The directive takes a map of String and Closure. The string is the display name of the parallel execution and the Closure the actual Pipeline code you wish to execute. Here are two examples to get you started:

def tasks = [:]

tasks["task_1"] = {
  stage ("task_1"){
    node('label_example1') {
        sh 'echo $NODE_NAME'
    }
  }
}
tasks["task_2"] = {
  stage ("task_2"){
    node('label_example2') {
        sh 'echo $NODE_NAME'
    }
  }
}

parallel tasks
def testList = ["a", "b", "c", "d"]
def branches = [:]

for (int i = 0; i < 4 ; i++) {
       int index=i, branch = i+1
       stage ("branch_${branch}"){
            branches["branch_${branch}"] = {
                node ('label_example'){
                    sh "echo 'node: ${NODE_NAME},  index: ${index}, i: ${i}, testListVal: " + testList[index] + "'"
                }
            }
      }
}

parallel branches

In case you are doing declarative Pipeline, make sure to review its documentation.