Nicholas Smith Nicholas Smith - 1 month ago 21
Groovy Question

Groovy .each only iterates one time

The script is not iterating through all the values of the 'modules' array.

class Module {
public String name = '';
public Boolean isCustom = false;
public Module(String name, Boolean custom){
this.name = name;
this.isCustom = custom;
}
}

//creates array from the ext_module env var
modules = [];
EXT_MODULE.split(',').each {
modules.add(new Module(it, false));
}


println modules;
modules.each {
println "MODULE NAME ::::: ${it.name}"
if(it.isCustom)
{
println "install custom";
} else {
println "install non custom";
}
};


This is the result of the run. The array shows 4 elements, but the code inside the .each black only executes once.


Running: Print Message
[Module@71f09325, Module@e1ddb41, Module@7069a674, Module@1f68f952]

Running: Print Message
MODULE NAME ::::: puppetlabs-ntp

Running: Print Message
install non custom

Running: End of Workflow

Finished: SUCCESS

Answer

The messages "Running: Print Message" and "Running: End of Workflow" indicate that you are using the new workflow plugin: https://wiki.jenkins-ci.org/display/JENKINS/Workflow+Plugin. This plugin currently has a bug causing at least some Groovy iterations involving a closure to be aborted after one iteration: https://issues.jenkins-ci.org/browse/JENKINS-26481