You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多Jenkinsfile导入ActiveChoices脚本触发ChoiceParameter错误如何解决

报错原因

这个报错是因为Jenkins无法识别你代码中使用的ChoiceParameter、CascadeChoiceParameter参数类型,同时代码存在语法转义错误,具体有两个核心问题:

  • 缺少必备插件:你用到的两个参数类型是Active Choices插件提供的自定义类型,Jenkins实例没有安装该插件或版本不兼容,导致Jenkins无法解析对应的参数类,才会抛出找不到对应List实现的错误。
  • 代码存在转义错误:你脚本里的"是HTML格式的双引号转义符,直接写在Groovy代码中不会被识别为合法的字符串边界,会被当做普通字符处理,进一步触发参数解析异常。

修复方案

1. 安装Active Choices插件

进入Jenkins后台「插件管理」页面,搜索Active Choices插件完成安装,重启Jenkins后生效。

2. 修正共享库代码

把所有"替换为正常双引号",修正后的choices.groovy代码如下:

def call() {
    def options = [buildDiscarder(logRotator(numToKeepStr: '5')),
        parameters([
            [
                $class: 'ChoiceParameter',
                choiceType: 'PT_SINGLE_SELECT',
                description: 'Select the ENVIRONMENT from the Dropdown List',
                filterLength: 1,
                filterable: true,
                name: 'ENVIRONMENT',
                script: [
                    $class: 'GroovyScript',
                    fallbackScript: [
                        classpath: [],
                        sandbox: true,
                        script: 'return ["ERROR"]'
                    ],
                    script: [
                        classpath: [],
                        sandbox: true,
                        script: '''
                            return ['', 'UAT', 'Staging', 'Production']
                        '''.stripIndent()
                    ]
                ]
            ],
            [
                $class: 'CascadeChoiceParameter',
                choiceType: 'PT_SINGLE_SELECT',
                description: 'Select the CONTEXT from the Dropdown List',
                filterLength: 1,
                filterable: true,
                name: 'CONTEXT',
                referencedParameters: 'ENVIRONMENT',
                script: [
                    $class: 'GroovyScript',
                    fallbackScript: [
                        classpath: [],
                        sandbox: true,
                        script: 'return ["ERROR"]'
                    ],
                    script: [
                        classpath: [],
                        sandbox: true,
                        script: '''
                            if (ENVIRONMENT == 'Production') {
                                return ['', 'GE', 'FR', 'PO', 'HG']
                            } else if (ENVIRONMENT == 'UAT') {
                                return ['', 'GE', 'FR', '']
                            } else if (ENVIRONMENT == 'Staging') {
                                return ['', 'FR']
                            } else {
                                return ['Select ENVIRONMENT']
                            }
                        '''.stripIndent()
                    ]
                ]
            ]
        ])
    ]
    return options
}
return this

3. 审批脚本权限

如果修复后仍有权限类报错,进入Jenkins后台「脚本审批」页面,批准对应Groovy方法的调用权限即可正常运行。


内容的提问来源于stack exchange,提问作者rbr86

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 02:36:08