groovy 变量声明中出现Jenkins管道错误ProxyException

w1jd8yoj  于 2022-11-01  发布在  Jenkins
关注(0)|答案(1)|浏览(237)
stage ("install_new_image") {
        when { expression {IMAGE_FILE_PATH_VAR} }
        steps {
                script { 
                    def IMAGE_FILE_VAR = IMAGE_FILE_PATH_VAR.split('/').last()
                    def INSTALL_IMAGE_ARGS = "./job_files/image_upload_job.py --testbed-file  ${TESTBED_FILE} --image_file ${IMAGE_FILE_VAR} --mail-to ${MAIL_TO_VAR}"
                    echo "args for install_new_image= ${INSTALL_IMAGE_ARGS}"
                }            

                build job: "/team_eng_ent_routing/Helper_Projects/PYATS_JOB_EXECUTOR", parameters: [
                string(name: "pyats_job_args", value: INSTALL_IMAGE_ARGS),
                string(name: "branch_name", value: BRANCH_NAME_VAR),
                string(name: "platform_name", value: PLATFORM_NAME_VAR)
                ]
        }
    }

由于以下错误,上述阶段失败,初始化和使用变量的正确方法是什么?

hudson.remoting.ProxyException: groovy.lang.MissingPropertyException: No such property: INSTALL_IMAGE_ARGS for class: WorkflowScript
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
utugiqy6

utugiqy61#

您可以在管道之前全局声明变量,然后在管道中的任意点初始化它们,这些变量可以从管道中的任意位置访问;或者您可以在script块中声明/初始化变量,这些变量只能在script块的作用域内访问。
在您的情况下,要轻松地解决这个问题,您可以将所有内容移到脚本块中。

stage ("install_new_image") {
    when { expression {IMAGE_FILE_PATH_VAR} }
    steps {
            script { 
                def IMAGE_FILE_VAR = IMAGE_FILE_PATH_VAR.split('/').last()
                def INSTALL_IMAGE_ARGS = "./job_files/image_upload_job.py --testbed-file  ${TESTBED_FILE} --image_file ${IMAGE_FILE_VAR} --mail-to ${MAIL_TO_VAR}"
                echo "args for install_new_image= ${INSTALL_IMAGE_ARGS}"

                build job: "/team_eng_ent_routing/Helper_Projects/PYATS_JOB_EXECUTOR", parameters: [
                string(name: "pyats_job_args", value: INSTALL_IMAGE_ARGS),
                string(name: "branch_name", value: BRANCH_NAME_VAR),
                string(name: "platform_name", value: PLATFORM_NAME_VAR)
                ]
            }                
    }
}

相关问题