SecureGroovyScript.script期望类java.lang.String但收到了SecureGroovyScript

时间:2019-06-25 10:21:29

标签: jenkins groovy jenkins-pipeline

我正在使用SecureGroovyScript和CascadeChoiceParameter编写一个允许动态参数的jenkinsfile。这是我的代码


properties([
    parameters([
        choice(choices: "\nDev" + "\nQA" + "\nStage" +"\nProd",description: 'Please select an environment',name: 'Env'), 
        [$class: 'CascadeChoiceParameter', 
            choiceType: 'PT_SINGLE_SELECT', 
            description: 'Select the Server from the Dropdown List', 
            filterLength: 25, 
            filterable: true, 
            name: 'Server', 
            referencedParameters: 'Env', 
            script: [
                $class: 'GroovyScript', 
                fallbackScript: [
                    sandbox: false, 
                    script: 
                        'return[\'Could not get Environment from Env Param\']'
                ], 
                script: [
                    sandbox: false, 
                    script: new SecureGroovyScript(''' if (Env.equals("Dev"))
                    {
                        return["Dev1","Dev2","Dev3","Dev4"]
                    }
                    else if(Env.equals("QA")){
                        return["QA1","QA2","QA3"]
                    }
                    else if(Env.equals("Stage")){
                        return["Stg1","Stg2","Stg3"]
                    }
                    else if(Env.equals("Prod")){
                        return["Prod1","Prod2"]
                    }''',false,[])
                ]
            ]
        ]
    ])
])

执行代码时,它一直显示以下错误

java.lang.ClassCastException: org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript.script expects class java.lang.String but received class org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:421)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:341)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:282)
Caused: java.lang.IllegalArgumentException: Could not instantiate {sandbox=false, script=org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript@69525ebd} for SecureGroovyScript(script: String, sandbox: boolean, classpath: ClasspathEntry(path: String)[])
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:287)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.coerce(DescribableModel.java:403)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.buildArguments(DescribableModel.java:341)
    at org.jenkinsci.plugins.structs.describable.DescribableModel.instantiate(DescribableModel.java:282)

我怀疑问题是否出自SecureGroovyScript的实例化,该实例以错误的方式识别输入脚本。有什么办法可以解决这个问题?

---更新--- 谢谢@metalisticpain的建议。我还有其他意见。 我要实现的是从动态选择的路径中读取文件。这是我尝试过但仍未成功的方法

                    script: 
                    '''
                    if (!Env.isEmpty()){
                        GString path = "converge-cloud-params/${Env}-flavors.txt"
                        File file = new File(path)
                        result = file.readLines()
                        return result
                    }
                    '''

1 个答案:

答案 0 :(得分:1)

我会尝试按照它的意思做,而不是

                script: new SecureGroovyScript(''' if (Env.equals("Dev"))
                {
                    return["Dev1","Dev2","Dev3","Dev4"]
                }
                else if(Env.equals("QA")){
                    return["QA1","QA2","QA3"]
                }
                else if(Env.equals("Stage")){
                    return["Stg1","Stg2","Stg3"]
                }
                else if(Env.equals("Prod")){
                    return["Prod1","Prod2"]
                }''',false,[])

                script: (''' if (Env.equals("Dev"))
                {
                    return["Dev1","Dev2","Dev3","Dev4"]
                }
                else if(Env.equals("QA")){
                    return["QA1","QA2","QA3"]
                }
                else if(Env.equals("Stage")){
                    return["Stg1","Stg2","Stg3"]
                }
                else if(Env.equals("Prod")){
                    return["Prod1","Prod2"]
                }''',false,[])

删除新的SecureGroovyScript,因为它可能需要一个字符串并将为您处理。

有关动态内容,请尝试

        script: ("""
                if (!Env.isEmpty()){
                    return readFile file:'converge-cloud-params/${Env}-flavors.txt'
                }
                """

请参见https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#readfile-read-file-from-workspace以供参考。