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

Jenkins声明式流水线中如何获取CascadeChoiceParameter全部参数值?

解决Jenkins CascadeChoiceParameter获取所有参数值的问题

问题背景

在Jenkinsfile中定义了CascadeChoiceParameter参数,该参数返回"ABC"、"PQR"、"WRQ"等值,需要获取该参数的所有值并存储为列表。参考Extended Choice Parameter的实现方式编写代码后,运行任务时报错:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: org.biouno.unochoice.CascadeChoiceParameter.getValue() is applicable for argument types: () values: []

原代码如下:

import hudson.model.*
import org.jenkinsci.plugins.workflow.job.*
import org.biouno.unochoice.CascadeChoiceParameter

@NonCPS
def getJob(name) {
    def hi = Hudson.instance
    return hi.getItemByFullName(name, Job)
}

@NonCPS
def getParam(String jobName, String paramName) {
    def job = getJob(jobName)
    def prop = job.getProperty(ParametersDefinitionProperty.class)
    for (param in prop.getParameterDefinitions()) {
        if (param.name == paramName) {
            return param
        }
    }
    return null
}

def param = getParam(JOB_NAME, "Test")
def all_param_values =  param.getValue().replaceAll("\\s","").tokenize(',').tail()
def Test_name = ("${TEST_NAME}".replace('Test:', '')).toString().tokenize(',[]')

错误原因

CascadeChoiceParameter(Uno Choice插件提供)没有getValue()方法,该方法是Extended Choice Parameter特有的。CascadeChoiceParameter的选项值存储逻辑与前者不同,需要根据参数的配置类型(静态列表/动态脚本)调用对应的属性或方法获取值。

修正方案

情况1:参数为静态预定义列表

如果你的CascadeChoiceParameter是通过静态列表配置的选项,直接调用choices属性即可获取值,修正后的代码如下:

import hudson.model.*
import org.jenkinsci.plugins.workflow.job.*
import org.biouno.unochoice.CascadeChoiceParameter

@NonCPS
def getJob(name) {
    def hi = Hudson.instance
    return hi.getItemByFullName(name, Job)
}

@NonCPS
def getParam(String jobName, String paramName) {
    def job = getJob(jobName)
    def prop = job.getProperty(ParametersDefinitionProperty.class)
    if (!prop) return null
    for (param in prop.getParameterDefinitions()) {
        if (param.name == paramName && param instanceof CascadeChoiceParameter) {
            return param
        }
    }
    return null
}

def param = getParam(JOB_NAME, "Test")
if (param) {
    // 获取静态选项列表,处理为字符串列表
    def all_param_values = param.choices.split(',').collect { it.trim() }
    // 处理当前选中的参数值
    def Test_name = "${TEST_NAME}".replace('Test:', '').trim().tokenize(',[]')
    println "所有参数值:${all_param_values}"
    println "选中的参数值:${Test_name}"
} else {
    println "未找到指定的CascadeChoiceParameter参数"
}

情况2:参数为动态脚本生成(如Groovy脚本)

如果参数的选项是通过动态脚本生成的,需要调用参数的getChoices()方法(需在@NonCPS方法中执行,避免序列化问题),示例代码如下:

import hudson.model.*
import org.jenkinsci.plugins.workflow.job.*
import org.biouno.unochoice.CascadeChoiceParameter

@NonCPS
def getJob(name) {
    def hi = Hudson.instance
    return hi.getItemByFullName(name, Job)
}

@NonCPS
def getAllParamValues(String jobName, String paramName) {
    def job = getJob(jobName)
    def prop = job.getProperty(ParametersDefinitionProperty.class)
    if (!prop) return []
    for (param in prop.getParameterDefinitions()) {
        if (param.name == paramName && param instanceof CascadeChoiceParameter) {
            // 调用getChoices()获取动态生成的选项
            def choices = param.getChoices()
            return choices.collect { it.toString() }
        }
    }
    return []
}

// 获取所有参数值
def all_param_values = getAllParamValues(JOB_NAME, "Test")
// 处理当前选中的参数值
def Test_name = "${TEST_NAME}".replace('Test:', '').trim().tokenize(',[]')

println "所有参数值:${all_param_values}"
println "选中的参数值:${Test_name}"

关键说明

  • 确保Jenkins已安装Uno Choice插件(CascadeChoiceParameter依赖该插件)
  • 区分参数的配置类型(静态/动态)选择对应的获取方式
  • 所有涉及Jenkins内部对象操作的方法需添加@NonCPS注解,避免流水线序列化错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 23:31:05