将Groovy对象传递给活动选择参数 [英] Pass Groovy object into active choice parameter

查看:78
本文介绍了将Groovy对象传递给活动选择参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将主动选择反应参数与声明式管道一起使用.但是我遇到了一个问题.

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?

例如


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.

推荐答案

使用字符串插值解决了该问题

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()

这篇关于将Groovy对象传递给活动选择参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆