Maps are the structures which can be very helpful. I’m not a developer, so I’m sure that many of you will take this post with a grain of salt. Even though, If you use Jenkins, you can face the same problem I faced some time ago. My experience with Groovy is limited to Jenkins job DSL configuration, but I always try to create a configuration as simple as possible. So sometimes my scripts are full of loops, conditionals, and variables. In one of my job, I tried to use a map and create a nested loop, where one of the keys should be equal to another key. In other words, I wanted to use a variable as a Groovy map key. Hard to understand? Let me show you what I mean.
What’s the problem?
Please take a look at the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def groovyApps = [ names: [ 'first', 'second', 'third' ], envs: [ first: [ 'dev', 'prod' ], second: [ 'dev', 'prod' ], third: ['dev', 'stage', 'live'] ] ] for (i in groovyApps.names) { print "App is: $i \n" for (j in groovyApps.envs.$i) { <--- the problem is there print "Brand for App is: $j \n" } print "========\n\n" } |
What is the suspected output of the code above? Something like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
App is: first Brand for App is: dev Brand for App is: prod ======== App is: second Brand for App is: dev Brand for App is: prod ======== App is: third Brand for App is: dev Brand for App is: stage Brand for App is: live ======== |
But after the code execution, we won’t see that output. We will this:
1 2 3 4 5 6 7 8 |
App is: first ======== App is: second ======== App is: third ======== |
It is not what we want.
How to properly use a variable as a Groovy map key?
We have to use quotation marks. Yes, you read this well. Quotation marks. Probably it’s something obvious for Developers, but if someone is not familiar with any programming language, it could be a small surprise. Anyway, there is the proper way of the example I’ve shown you above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
def groovyApps = [ names: [ 'first', 'second', 'third' ], envs: [ first: [ 'dev', 'prod' ], second: [ 'dev', 'prod' ], third: ['dev', 'stage', 'live'] ] ] for (i in groovyApps.names) { print "App is: $i \n" for (j in groovyApps.envs."$i") { print "Brand for App is: $j \n" } print "========\n\n" } |
As the result you will get the following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
App is: first Brand for App is: dev Brand for App is: prod ======== App is: second Brand for App is: dev Brand for App is: prod ======== App is: third Brand for App is: dev Brand for App is: stage Brand for App is: live ======== |
And that’s all. Now you can use a variable as a Groovy map key in your code. It’s very useful for every Jenkins Administrator – it is possible to create a very flexible and scalable code of your job configurations.
Hi
If I need to define the map based on the parameter input of the job, how can I do it?
Eg: user inputs person1, person2, person3 as input
map needs to be created for each person –
ie person1 [ id: ‘ ‘ , name: ‘ ‘]
person2 [ id: ‘ ‘, name: ‘ ‘]
etc