Home > Back-end >  Jenkins Pipeline Error ProxyException in variable declaration
Jenkins Pipeline Error ProxyException in variable declaration

Time:07-04

    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)
                ]
        }
    }   

The above stage fails because of the below error,, whats the correct way to initialise and use variables?

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)

CodePudding user response:

You can either declare variables globally, before the pipeline, and then initialize them at any point in the pipeline, these will be accessible from anywhere in the pipeline, or you can declare/initialize variables within a script block, these variables are accessible only within the scope of the script block.

In your case, to easily resolve this you can move everything into the script block.

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)
                ]
            }                
    }
} 
  • Related