Pass Groovy object into active choice parameter

回眸只為那壹抹淺笑 提交于 2020-08-10 19:17:50

问题


I use active choice reactive parameter with declarative pipeline. But I ran into a problem.

Is there any way to pass list object into script or call external method?

For example


environments = 'lab\nstage\npro'

List<String> someList = ['ccc', 'ddd']
def someMethod() { 
   return ['aaa', 'bbb']
}

properties([
    parameters([
        choice(name: 'ENVIRONMENT', choices: "${environments}"),
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT',
            description: 'Select a choice',
            filterLength: 1,
            filterable: true,
            name: 'choice1',
            referencedParameters: 'ENVIRONMENT',
            script: [$class: 'GroovyScript',
                fallbackScript: [
                    classpath: [], 
                    sandbox: true, 
                    script: 'return ["ERROR"]'
                ],
                script: [
                    classpath: [], 
                    sandbox: true, 
                    script: """
                        if (ENVIRONMENT == 'lab') { 
                            return someMethod() // !!! call method
                        }
                        else {
                            return someList // !!! return object
                        }
                    """.stripIndent()
                ]
            ]
        ]
    ])
])

pipeline {
    agent any
    ...
}

I would be grateful for any hints.


回答1:


Solved it by using string interpolation

List<String> someList = ['ccc', 'ddd']
def someMethod() { 
   return ['aaa', 'bbb']
}
...
script: """
    if (ENVIRONMENT == 'lab') { 
        return ["${someMethod().join('","')}"] // !!! call method
    }
    else {
        return ["${someList.join('","')}"] // !!! return object
    }
""".stripIndent()


来源:https://stackoverflow.com/questions/63093323/pass-groovy-object-into-active-choice-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!