Reputation: 359
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.
Upvotes: 24
Views: 231048
Reputation: 11
Based on the previous answers and my personal similar issue (missing class written by me), even though it existed, just like jenkins here, recheck everything related to syntax on the missing class mentioned, its import, CRLF related issues, make sure its extension is groovy(not java) or position in folders compared to package declared import.
Upvotes: 0
Reputation: 61
in my case I have used -
(Hyphen) in my script name in case of Jenkinsfile Library.
Got resolved after replacing -
(Hyphen) with _
(Underscore)
Upvotes: 4
Reputation: 29
For me this problem occurred because I had a some invalid character in my Groovy script. In our case this was an extra blank line after the closing bracket of the script.
Upvotes: 2
Reputation: 359
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.
Upvotes: 8
Reputation: 1588
Please double check that jenkins is not blocking this import. Go to script approvals and check to see if it is blocking it. If it is click allow.
https://jenkins.io/doc/book/managing/script-approval/
Upvotes: 5