最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I pass global variable in Groovy scripted Jenkins functions with DynamicReferenceParameter class - Stack Overflow

programmeradmin0浏览0评论

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?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论