Cutton Eye
Cutton Eye

Reputation: 3569

Jenkins Pipline - loop over array of nodes

The aim is to have a dynamic pipeline. I've several nodes with specific configurations. I would like to build my job on all of them, not reguarding the number of nodes and without copy&paste.

The code below would work but it is throwing an exception. Is there any known workaround out there to accomplish a dynamic pipeline? (declarative or like this example the scripted pipeline)

for (aSlave in hudson.model.Hudson.instance.slaves) {
    ActiveNode=aSlave.getLabelString()
    node(ActiveNode){

    node(aSlave.getLabelString()) { //java.io.NotSerializableException: hudson.plugins.libvirt.VirtualMachineLauncher

        stage(aSlave.name){
            echo 'Hello World'
        }
    }
  }
}

Upvotes: 0

Views: 1822

Answers (1)

Javier Delgado
Javier Delgado

Reputation: 181

Wrap the method returnin a non serializable object in a method annotated with @NonCPS.

aSlave.getLabelString() is not the culprit, but aSlave. A possible solution would be:

@NonCPS
def agentNames() {
  hudson.model.Hudson.instance.slaves.collect{[(it.labelString):(it.name)]}​
}

for (agent in agentNames()) {
  node(agent.keySet()[0]) {
    stage(agent.values()[0]){
      echo 'Hello World'
    }
  }
}

Upvotes: 3

Related Questions