Jenkins pipeline script created dynamically

谁都会走 提交于 2019-12-10 23:08:52

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/40082261/jenkins-pipeline-script-created-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!