使用 Groovy 脚本的 Jenkins Active Choice 参数调用函数

Jenkins Active Choice parameter call function with Groovy script

尝试调用活动选择参数中定义的函数 'script' 部分似乎不起作用:

def JustTest() {
  xxx = ['a','b']
  return xxx
}

properties([
    parameters([
        [$class: 'ChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            ////
            some code omitted
            ////
            script: [
                classpath: [], sandbox: false, 
                script:
                """
                def mymy = JustTest()
                return mymy
                """
                ]
            ]
        ]
    ])
])

pipeline {
  some code
}

尝试使用参数构建时收到错误

p.s。 'input'不适合我,我需要先选择参数再开始

在没有看到错误的情况下,我认为这只是确保您向 choices 参数提供正确数据类型(字符串)的问题。

对于 return 类型的列表,将它们与换行符连接在一起,如下所示。如果它是其他东西,你将需要进一步操纵它

    List JustTest() {
         List xxx = ['a','b']
         return xxx
    }

    properties([
        parameters([
            choice(name: 'PARAM', choices: JustTest().join('\n'), description: 'Choice'),
        ])
    ])

我认为不需要如此冗长的选择实现,但也许我们的 jenkins 插件存在差异。或许可以试试我的较短的版本,而不用 $class 冗长。