Search code examples
jenkinsjenkins-pipelinejenkins-declarative-pipeline

Jenkins declarative pipeline parallel steps executors


I am migrating a job from multijob to a Jenkins Declarative pipeline job. I am unable to run the parallel steps on multiple executors.

For example in the pipeline below, I see only one executor being used when I run the pipeline.

I was wondering why only a single executor is used. The idea is that each parallel step would be inoking a make target that would build a docker image.

pipeline {
  agent none
  stages {
    stage('build libraries') {
      agent { label 'master' }
      steps {
        parallel(
          "nodejs_lib": {
            dir(path: 'nodejs_lib') {
                sh 'sleep 110'
            }
          },
          "python_lib": {
            dir(path: 'python_lib') {
              sh 'sleep 100'
            }
          }
        )
      }
    }
  }
  options {
    ansiColor('gnome-terminal')
    buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: '30'))
    timestamps()
  }
}

Solution

  • You can try the following way to perform parallel tasks execution for your pipeline job:

    def tasks = [:]
    
                tasks["TasK No.1"] = {
                  stage ("TASK1"){    
                    node('master') {  
                        sh '<docker_build_command_here>'
                    }
                  }
                }
                tasks["task No.2"] = {
                  stage ("TASK2"){    
                    node('master') {  
                        sh '<docker_build_command_here>'
                    }
                  }
                }
                tasks["task No.3"] = {
                  stage ("TASK3"){    
                    node('remote_node') {  
                        sh '<docker_build_command_here>'
                    }
                  }
                }
    
     parallel tasks       
    

    If you want to execute parallel tasks on a single node and also want to have the same workspace for both the tasks then you can go with the following approach:

    node('master') { 
    
    def tasks = [:]
    
                    tasks["TasK No.1"] = {
                      stage ("TASK1"){    
    
                            sh '<docker_build_command_here>'
                      }
                    }
                    tasks["task No.2"] = {
                      stage ("TASK2"){    
    
                            sh '<docker_build_command_here>'
                      }
                    }
    
    
         parallel tasks       
    }