Jenkins 声明式管道并行步骤执行器

Jenkins declarative pipeline parallel steps executors

我正在将作业从多作业迁移到 Jenkins 声明式管道作业。我无法 运行 多个执行者的并行步骤。

例如在下面的管道中,当我 运行 管道时,我看到只有一个执行程序被使用。

我想知道为什么只使用一个执行器。这个想法是,每个并行步骤都会调用一个 make 目标,该目标将构建一个 docker 图像。

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()
  }
}

您可以尝试以下方式为您的管道作业执行并行任务:

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       

如果您想在单个节点上执行并行任务,并且还希望两个任务具有相同的工作区,那么您可以采用以下方法:

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       
}