Jenkins管道脚本动态创建 [英] Jenkins pipeline script created dynamically

查看:285
本文介绍了Jenkins管道脚本动态创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用詹金斯管道项目。在脚本中,我想以动态方式编写 parallel 块,因为节点数量可能会发生变化。例如,从这个:

  parallel(
node1:{
node(){
stage1()
stage2()
...
}
},
node2:{
node(){
stage1 )
stage2()
...
}
},
...



0; i <$ NODE_NUMBER; i ++){
node $ {i}:{
node('namenode-'+ $ {i}){
something()


$ / code>

但这种方式不起作用,Groovy / Jenkins不是很高兴这个语法。有人可以提出一个更好的方法来做到这一点吗?

解决方案

您可以像分支

code> first,然后将它们作为并行分支执行。

  def numNodes = 4 
def branches = [:]

for(int i = 0; i branches [node $ {i }] = {
node(namenode - $ {i}){
something()
}
}
}
并行分支


I am using a jenkins pipeline project. In the script I would like to write the parallel block in a dynamic way, since the number of nodes can change. For instance, from this:

parallel(
node1: {
    node(){
        stage1()
        stage2()
        ...
    }
},
node2: {
    node(){
        stage1()
        stage2()
        ...
    }
},
...
)

to something like this

for (int i = 0; i < $NODE_NUMBER; i++) {
  "node${i}": {
    node (’namenode-' + ${i}) {
      something()
    }
}

but this way doesn’t work, Groovy/Jenkins is not happy about this syntax. Can someone suggest a better way for doing this?

解决方案

You can define node map like branches first, and then execute them as parallel branches.

def numNodes = 4
def branches = [:]

for(int i = 0; i < numNodes; i++) {
    branches["node${i}"] = {
        node("namenode-${i}") {
            something()
        }
    }
}
parallel branches

这篇关于Jenkins管道脚本动态创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆