I have the following Groovy scripted Jenkins pipeline file where the variable partitions
is currently inside the function getPartitionParameter()
. Since this variable contains a lot of items in real environment, and I need to use this variable in multiple functions, I'd like to move this variable to the global area of the pipeline file. However, I was not able to do it successfully.
def parametersList = [
choice(name: 'DO_BUILD', choices: ["----","true"].join("\n"), description: 'Confirm to build image'),
]
parametersList.add(getImageParameter())
parametersList.add(getPartitionParameter())
properties([parameters(parametersList)])
stage('Create AMI'){
// stage code goes here
}
def getImageParameter() {
return [
$class: 'ChoiceParameter',
choiceType: 'PT_SINGLE_SELECT',
description: 'Select an image to build:',
name: 'image',
script: [
$class: 'GroovyScript',
script: [
classpath: [],
sandbox: false,
script: '''
return ['market', 'collection']
'''
]
]
]
}
def getPartitionParameter() {
return [
$class: 'DynamicReferenceParameter',
choiceType: 'ET_FORMATTED_HTML',
name: 'partition',
referencedParameters: 'image',
script: [
$class: 'GroovyScript',
script: [
classpath: [],
sandbox: false,
script: '''
def partitions = [
'market': ['app'],
'collection': ['app', 'web']
]
def html = "Select a partition for image <b>${image}</b> :<br>"
html += "<select name='partition'>"
partitions[image]?.each { partition ->
html += "<option value='${partition}'>${partition}</option>"
}
html += "</select>"
return html
'''
]
]
]
}
Please note that the two functions (getImageParameter()
and getPartitionParameter()
) are used for when I open the pipeline page and select the parameters before running the pipeline.
When I move the variable in the global area of the pipeline file, it cannot be accessed from inside the function getPartitionParameter()
. Even passing this variable to the function like parametersList.add(getPartitionParameter(partitions ))
does not work.
Is there a way to do this? If so, how can I do this?