2

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mark
  • 418
  • 3
  • 12

1 Answers1

4

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
arasio
  • 1,260
  • 10
  • 14
  • Could you provide a complete working solution? This seems to not work any longer, does it? – lony Jan 09 '18 at 15:29