Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding

Tags:

jenkins

groovy

We're upgrading to a newer version of Jenkins (2.60.1) and a groovy script which was working in the previous Jenkins version (1.596/2) no longer works.

This is a Jenkins build project, which is parameterized and we're using a Groovy script to provide the choices for a Choice Provider (the Choice Provider is set to System Groovy Choice Parameter).

We're trying to get access to the Jenkins environment variables and do so like this (this is part of the Groovy script):

import hudson.slaves.EnvironmentVariablesNodeProperty
import hudson.EnvVars

EnvironmentVariablesNodeProperty prop = jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class)
EnvVars env = prop.getEnvVars()

def MY_VAR = env['MY_JENKINS_VAR']

However, I'm getting the following error when running the script:

Failed to execute script

groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding
at groovy.lang.Binding.getVariable(Binding.java:63)
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:224)

It seems to me the "jenkins" reference is expected to be a built-in property provided by Jenkins or perhaps Groovy, but I can't find any information on what I need to do to make it accessible from the Groovy script.

Thanks for any help.

like image 387
Mark Hansen Avatar asked Nov 01 '17 21:11

Mark Hansen


People also ask

What is binding in Groovy?

The binding is defined in the Groovy API documentation as follows: “Represents the variable bindings of a script which can be altered from outside the script object or created outside of a script and passed into it.”.

What is missing property exception in Groovy?

Class MissingPropertyExceptionAn exception occurred if a dynamic property dispatch fails with an unknown property. Note that the Missing*Exception classes were named for consistency and to avoid conflicts with JDK exceptions of the same name. See Also: Serialized Form.

How Groovy is used in Jenkins?

Within a Pipeline Project (read plugin), Jenkins introduces a domain-specific language (DSL) based on 'Groovy', which can be used to define a new pipeline as a script. The flow that would typically require many “standard” Jenkins jobs chained together, can be expressed as a single script.

What is def in Groovy script?

The def keyword is used to define an untyped variable or a function in Groovy, as it is an optionally-typed language.


1 Answers

As pointed out by @Jayan in another post, the solution was to do the following

import jenkins.model.*
jenkins = Jenkins.instance

Then I was able to do the rest of my scripting the way it was.

like image 104
Mark Hansen Avatar answered Sep 21 '22 20:09

Mark Hansen