Calling writeFile for every entry of a map requires somewhat ugly code

14 views
Skip to first unread message

David Karr

unread,
Jul 18, 2020, 2:28:32 PM7/18/20
to Jenkins Users
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.

Gianluca

unread,
Jul 18, 2020, 3:07:32 PM7/18/20
to Jenkins Users
I use a different approach ... but it may not fits your use case.
In any case, usually when I have to perform operation on each element of a Map, I end up on transform the Map on steps that I can feed to parallel

steps {
  stepsMap
= [:]

  stepsMap
= dataMap.collectEntries{[
   
"Name of step to perfom (that will appear on BlueOcean": {
       
// pipeline code, I usually use script block here to be able to do stuff not available in declarative
   
}
 
]}

  parallel stepsMap

}


By default collectEntries put the value into a variable called $it ... but the there is a syntax that allow to use key and value variables and use them inside.

Baptiste Mathus

unread,
Aug 13, 2020, 5:39:24 AM8/13/20
to jenkins...@googlegroups.com
Extract this in a shell script or so.
Use Jenkins pipeline for orchestration, not for programming.

It sounds tempting to use pipeline like it's Groovy. But first it's not *really* Groovy as you've discovered and second it'll make it very hard to develop locally if the build process steps are only runnable within Jenkins.

Cheers
-- Baptiste

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/569ba94d-b5e6-4956-ac7d-cd2e3bf4b692o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages