Jenkins级联选择参数导致缺少属性异常:无此属性

v9tzhpje  于 2022-12-11  发布在  Jenkins
关注(0)|答案(1)|浏览(189)

我在Jenkinsfile中有两个动态参数(在这个简化的例子中),第一个是ChoiceParameter,第二个是CascadeChoiceParameter,取决于第一个。
下面的代码显示了Jenkinsfile中的两个参数CATEGORYUSER

properties([
    parameters([
        [
            $class: 'ChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            name: 'CATEGORY',
            script: [
                $class: 'GroovyScript',
                script: [ classpath: [], sandbox: true, script: 
                    """
                    return ['one', 'two']
                    """
                ]
            ]
        ],

        [
            $class: 'CascadeChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            referencedParameters: 'CATEGORY',
            name: 'USER',
            script: [
                $class: 'GroovyScript',
                script: [ classpath: [], sandbox: true, script:
                    """
                    if ( CATEGORY.equals( 'one' ) ) {
                        return ['user for category one']
                    } else {
                        return ['user for category two']
                    }
                    """
                ]
            ]
        ],

    ])
])

它们确实起作用(即第二个USER根据第一个CATEGORY而变化),但也会在Jenkins日志中产生以下异常:

Error executing script for dynamic parameter
groovy.lang.MissingPropertyException: No such property: CATEGORY for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    [...]
Caused: java.lang.RuntimeException: Failed to evaluate script: No such property: CATEGORY for class: groovy.lang.Binding
    at org.biouno.unochoice.model.GroovyScript.eval(GroovyScript.java:197)
    at org.biouno.unochoice.util.ScriptCallback.call(ScriptCallback.java:96)
    [...]

我尝试将第二个groovy脚本的IF语句从

if ( CATEGORY.equals( 'one' ) ) {

if ( "${CATEGORY}".equals( 'one' ) ) {

这消除了异常,但是(使用记录器)显示“${CATEGORY}”始终计算为"one",而从不计算为"two",即使在Jenkins UI中更改了ChoiceParameter“CATEGORY”的下拉值之后也是如此。
我必须如何访问Groovy脚本中的参数CATEGORY才能让它工作而不引发异常?

djmepvbi

djmepvbi1#

下面的似乎对我工作正常。尝试更新插件也许。

def CATEGORY = ""

properties([
    parameters([
        [
            $class: 'ChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            name: 'CATEGORY',
            script: [
                $class: 'GroovyScript',
                script: [ classpath: [], sandbox: true, script: 
                    """
                    return ['one', 'two']
                    """
                ]
            ]
        ],

        [
            $class: 'CascadeChoiceParameter',
            choiceType: 'PT_SINGLE_SELECT',
            referencedParameters: 'CATEGORY',
            name: 'USER',
            script: [
                $class: 'GroovyScript',
                script: [ classpath: [], sandbox: true, script:
                    """
                    if ( CATEGORY.equals( 'one' ) ) {
                        return ['user for category one']
                    } else {
                        return ['user for category two']
                    }
                    """
                ]
            ]
        ],

    ])
])

pipeline {
    agent any
    stages {
           
        stage('Hello') {

            steps {
                             
                echo 'Hello World'
                println "Category: " + CATEGORY
                println "User: " + USER
            }
        }
    }
}

相关问题