Helping understand functions in Jenkins Groovy pipeline

Viewed 46

I'm trying to understand the syntax and methods of the following groovy script used in a jenkins pipeline:

def mapPackageNameToDeployParams(String packageName) {
    def deployParams = [:] <----- ##what [:] in var declaration means
    deployParams['content1'] = [
        var1: 'some-string1',
        var2: 'some-string2',
        var3: 'some-string2', ]

   deployParams['content2'] = [
        var1: 'some-string1',
        var2: 'some-string2',
        var3: 'some-string2',

return deployParams[packageName]
}
      

If I'm getting this correctly, the fucntion will return a dictionary based on what value I set under packageName?

1 Answers

This is a map that contains maps. [:] is the literal syntax for an empty map. Otherwise the map would be null and you couldn’t add anything to it. The map gets populated with 2 more maps. The argument you pass in decides which of the 2 maps is returned, if the argument doesn’t match either content1 or content2 the function returns null.

If you have groovy installed on your computer you can try your posted code out in groovyConsole and see what it does.

Related