Mark
Mark

Reputation: 418

Jenkins pipeline script created dynamically

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?

Upvotes: 2

Views: 1344

Answers (1)

arasio
arasio

Reputation: 1316

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

Upvotes: 4

Related Questions