I'm trying to build a parameterized pipeline-job, where I want to populate the list of ENVIRONMENTs, based on the selected ACCOUNTs. I'm using activeChoiceParam
and activeChoiceReactiveParam
for that but I need to pass list variables to the script to generate the appropriate list of ENVs. This is (something similar) what I have atm:
def valACCS = [
'"npr - Non-Production [E002N]"',
'"prd - Live/Production [E002P]"',
]
def valNprENVS = [
'"dev - Development"',
'"int - Integration"',
]
def valPrdENVS = [
'"ppr - Pre-Production"',
'"liv - Live/Production"',
]
pipelinejob('iac_deploy') {
parameters {
activeChoiceParam('ACCOUNTS') {
choiceType('SINGLE_SLELCT')
groovyScript {
script('[' valACCS.join(', ') ']')
}
}
activeChoiceReactiveParam('ENVIRONMENTS') {
choiceType('SINGLE_SLELCT')
groovyScript {
script('''
if (ACCOUNTS.split('-')[0].trim() == 'npr') {
return [valNprENVS.join(', ')]
} else if (ACCOUNTS.split('-')[0].trim() == 'prd') {
return [valPrdENVS.join(', ')]
} else {
return ['NONE']
}
''')
}
referencedParameter('ACCOUNTS')
}
}
}
That's the way, it's working okay for valNprENVS
but the values for valNprENVS
or valPrdENVS
not expanding at all. After building the job, this is what I get for activeChoiceReactiveParam
:
but for activeChoiceParam
it's expanding correctly with the values:
I'm trying to do something similar for activeChoiceReactiveParam as well. Any idea how can I do that?
CodePudding user response:
After a bit of tug-of-war, I managed to do it and since didn't find any clear reference in the nest for this type of issue, answering to my own question, just in case someone else is also after the same. The trick is to use Triple Double-Quote String in the script()
block and the variable then will expand inside it, using ${varName}
syntax.
This is what I ended up doing, which did the job for me:
activeChoiceReactiveParam('ENVIRONMENTS') {
choiceType('SINGLE_SLELCT')
groovyScript {
script("""\
if (ACCOUNTS.split('-')[0].trim().equals('npr')) {
return ${valNprENVS}
} else if (ACCOUNTS.split('-')[0].trim().equals('prd')) {
return ${valPrdENVS.join}
} else {
return ['NONE']
}
""".stripIndent())
}
referencedParameter('ACCOUNTS')
}
Rest of the changes are ornamental: """\
removes the first new_line; Replaced ==
with .equals
; .stripIndent
removes leading spaces on every line etc. but not related to solving the issue at hand.
Hope that helps others!