gpt4 book ai didi

Jenkins 文件 : Run a task in all Agents with same label

转载 作者:行者123 更新时间:2023-12-04 03:12:34 26 4
gpt4 key购买 nike

我想在所有具有相同标签的代理中的 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

谢谢,阿伦小号

最佳答案

我发现这段代码可以满足您的要求。但是,您需要取消选中“沙盒”

// 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 }
}

关于 Jenkins 文件 : Run a task in all Agents with same label,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43871471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com