I'm posting this mostly so people can find it. Perhaps it's already well-known, and perhaps I'm missing some simple thing that makes this easier to do, but I think not.
In my Jenkinsfile, I need to iterate over a map created with Groovy, calling "writeFile" with the key as the file name, and the value as the contents. If you haven't had to do this, you might assume this is simple. If you have, you either discovered what I did, or you gave up.
What I tried to write first was this:
dataMap.each {
writeFile(file: "target/generated." + key, text: value)
}
This gets serialization errors. I then discovered that calling pipeline steps within closures would not work, so I changed it to a plainer iteration:
for(Map.Entry entry : dataMap) {
writeFile(file: "target/generated." + entry.getKey(), text: entry.getValue())
}
This also does not work. It appears that you simply can't call a pipeline step with a map anywhere in scope.
What DOES work is something like the following:
def keyValueList = []
for (Map.Entry entry : dataMap) {
keyValueList.add(entry.getKey())
keyValueList.add(entry.getValue())
}
dataMap = null
for (int ctr = 0; ctr < keyValueList.size(); ctr += 2) {
writeFile(file: "target/generated." + keyValueList.get(ctr), text: keyValueList.get(ctr + 1))
}
keyValueList = null
Suggestions would be useful, or perhaps other people will just do the same thing I've done.