Jenkinsfile:在具有相同标签的所有代理中运行任务

时间:2017-05-09 13:40:31

标签: jenkins groovy jenkins-pipeline

我想在所有具有相同标签的代理商的Jenkins管道中运行一项特殊任务。我为此目的得到了以下代码。但它没有按预期工作。 它只在带有标签" my_label"的第一个奴隶中运行。然后退出我需要用这个标签在所有Jenkins Slaves上运行这个工作。

任何帮助都将受到高度赞赏。

def labels=["my_label"] def builders=[:] for (x in labels) {
  def label=x builders[label]= {
    node(label) {
      // build steps that should happen on all nodes go here
      // Step 4
      stage('Run deployment on all agents in the given environment') {
        sh "echo Run deployment" sh "echo release_version = ${params.release_version}" sh "echo environment = ${params.environment}"
      }
    }
  }
}

parallel builders

谢谢, Arun S

1 个答案:

答案 0 :(得分:0)

我发现这个代码符合你的要求。但是,您需要取消选中' sandbox'

// The script triggers PayloadJob on every node.
// It uses Node and Label Parameter plugin to pass the job name to the payload job.
// The code will require approval of several Jenkins classes in the Script Security mode
def branches = [:]
def names = nodeNames()
for (int i=0; i<names.size(); ++i) {
  def nodeName = names[i];
  // Into each branch we put the pipeline code we want to execute
  branches["node_" + nodeName] = {
    node(nodeName) {
      echo "Triggering on " + nodeName
    }
  }
}

// Now we trigger all branches
parallel branches

// This method collects a list of Node names from the current Jenkins instance
@NonCPS
def nodeNames() {
  return jenkins.model.Jenkins.instance.nodes.collect { node -> node.name }
}