In our Jenkinsfile we have a lot of parameters (parameterized build) and in this case I want to check if each parameter is toggled and act on that. These parameters have similar names but end with a different decimal, so I would like to iterate over them to achieve this.
I have something like:
if ("${TEST_00}" == "true") { testTasksToRun.add(testsList[0]) }
if ("${TEST_01}" == "true") { testTasksToRun.add(testsList[1]) }
if ("${TEST_02}" == "true") { testTasksToRun.add(testsList[2]) }
if ("${TEST_03}" == "true") { testTasksToRun.add(testsList[3]) }
if ("${TEST_04}" == "true") { testTasksToRun.add(testsList[4]) }
if ("${TEST_05}" == "true") { testTasksToRun.add(testsList[5]) }
But I would like to have something like:
for(int i=0; i<testsList.size(); i++) {
if ("${TEST_0${i}}" == "true") {
testTasksToRun.add(testsList[i])
}
}
I tried to search for solutions and experimented on the GroovyConsole but didn't manage to get anything to work. Looks like it has something to do with "binding", but I am not familiar with that.
params
is a GlobalVariable
that when accessed returns an unmodifiable map. You can see the implementation here.
Because it returns a Map
, you can use the same strategies to iterate over it as you would for normal Groovy maps.
params.each { key, value ->
// do things
}
for (entry in params) {
// entry.key or entry.value
}
Newer versions of the Groovy CPS libraries should handle most iteration cases since JENKINS-26481 has been resolved.