Home > Blockchain >  Jenkins method cannot get variable (groovy.lang.MissingPropertyException: No such property: variable
Jenkins method cannot get variable (groovy.lang.MissingPropertyException: No such property: variable

Time:07-12

Currently I'm trying to make this (simplified) Jenkins pipeline script. Please help me find out how to pass the string VAR1 correctly. I tried using bared VAR1, {var1}, "{VAR1}", or like below (I know that some seems stupid but I'm kind of desperate) yet nothing worked.

def VAR1 = 'build architectures'
def BuildANew(variables)
{
   echo "Build declaration"
   build job: 'LocalJenkins', parameters:[
        extendedChoice(name: 'VAR1', value: ${VAR1}),
        param(name:'Variables', value:variables)
    ]
}

pipeline{
   ...
   stages{
      stage("Build"){
         parallel{
            stage("Build1"){
               agent{
                  label 'label'
               }
               steps{
                  BuildANew(parameters)
               }
            }
         }
      }
   }
}

And it returned this error:

groovy.lang.MissingPropertyException: No such property: VAR1 for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:271)
    at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
    at WorkflowScript.BuildANew(WorkflowScript:39)
    at WorkflowScript.run(WorkflowScript:155)

Thank you.

CodePudding user response:

In a Groovy script, scopes can be different from a typical class itself. Hence you need to explicitly change the scope of global variables if you are accessing them from other functions. In order to change the variable scope, you can use @Field annotation which changes the scope of the variable from a local to a Script class variable.

You have two options here.

Use @Field annotation to change the scope of the variable

import groovy.transform.Field

@Field def VAR1 = 'build architectures'

def BuildANew(variables)
{
   echo "Build declaration"
   build job: 'LocalJenkins', parameters:[
        extendedChoice(name: 'VAR1', value: ${VAR1}),
        param(name:'Variables', value:variables)
    ]
}

pipeline{
   ...
   stages{
      stage("Build"){
         parallel{
            stage("Build1"){
               agent{
                  label 'label'
               }
               steps{
                  BuildANew(parameters)
               }
            }
         }
      }
   }
}

Pass the variable to the function from your pipeline

def VAR1 = 'build architectures'
def BuildANew(variables, VAR1)
{
   echo "Build declaration"
   build job: 'LocalJenkins', parameters:[
        extendedChoice(name: 'VAR1', value: ${VAR1}),
        param(name:'Variables', value:variables)
    ]
}

pipeline{
   ...
   stages{
      stage("Build"){
         parallel{
            stage("Build1"){
               agent{
                  label 'label'
               }
               steps{
                  BuildANew(parameters, VAR1)
               }
            }
         }
      }
   }
}

If you want to read more. There is a good explanation here.

CodePudding user response:

I found the solution, could be imperfect but it works, for now.

VAR1 = 'build architectures'
def BuildANew(variables)
{
   echo "Build declaration"
   build job: 'LocalJenkins', parameters:[
        extendedChoice(name: 'VAR1', value: ${VAR1}),
        param(name:'Variables', value:variables)
    ]
}
  • Related