如何在Jenkins中的多个从站上并行运行相同的作业,在那里我从XML文件中获取从站名称和编号。目前

时间:2018-06-19 09:22:49

标签: jenkins-pipeline

请参阅以下代码以供参考,此处并行的两个分支是硬编码的。如果我有3个奴隶并且我想使用三个分支怎么办?我想动态添加第三个分支。

File file1 = new File("C:\\COPIER\\Completed\\Slave.txt");
def slave = file1.readLines()
echo slave[0]
echo slave[1]
parallel (
            firstBranch: 
            {
               build job: "Instance_Pipeline_TIACopy", parameters: [ [$class: 'StringParameterValue', name: 'NODE', value: slave[1]], [$class: 'StringParameterValue', name: 'TIABuildLocation', value: "C:\\TestParallelExecution\\FirstBranch"], [$class: 'StringParameterValue', name: 'TIASlaveLocation', value: "E:\\TestParallelExecution\\FirstBranch"] ], propagate: false
               echo "First Branch Build Completed"
            }, 
            secondBranch: 
            {
               build job: "Instance_Pipeline_TIACopy", parameters: [ [$class: 'StringParameterValue', name: 'NODE', value: slave[1]], [$class: 'StringParameterValue', name: 'TIABuildLocation', value: "C:\\TestParallelExecution\\SecondBranch"], [$class: 'StringParameterValue', name: 'TIASlaveLocation', value: "E:\\TestParallelExecution\\SecondBranch"] ], propagate: false
               echo "Second Branch Build Completed"
            },
        )

1 个答案:

答案 0 :(得分:1)

只需创建并行步骤所需的结构:

File agentsFile = new File("C:\\COPIER\\Completed\\Slave.txt");
def agents = file1.readLines()

def parallelBranches = [:]
for (int i = 0; i < agents.size(); i++) {
  parallelBranches["branches${i}"] = {
    build job: "Instance_Pipeline_TIACopy", parameters: [ [$class: 'StringParameterValue', name: 'NODE', value: agents[i]], [$class: 'StringParameterValue', name: 'TIABuildLocation', value: "C:\\TestParallelExecution\\FirstBranch"], [$class: 'StringParameterValue', name: 'TIASlaveLocation', value: "E:\\TestParallelExecution\\${i}"] ], propagate: false
    echo "Branch #${i} Build Completed"
  }
}

parallel parallelBranches

请注意,根据Jenkins glossary

,正确的名称是agent(而不是slave)。