0
votes

We have a Jenkins build which runs successfully on Jenkins 1.596.2 which we are migrating to a new build machine running Jenkins 2.60.1

As part of the build configuration we have an Extensible Choice parameter and use the System Groovy Choice Parameter as the Choice Provider. We then use a Groovy script to get the list of choices.

This Groovy script needs access to one of the Jenkins build environment variables, set in the Global Properties section of the build configuration (under Environment Variables).

Under the earlier version of Jenkins, we were able to get this environment variable value using the following code:

import hudson.slaves.EnvironmentVariablesNodeProperty
import hudson.EnvVars

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

def myVariable = env['MY_VAR']

However, when we run this under the new Jenkins build, we get the following error:

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

Can anyone please explain what we need to do to get access to the Jenkins build environment variables from within the Groovy scripts?

Are we missing a plugin or import which provides access to the "jenkins" object/variable?

I've found a few answers to this online, but none of the solutions work. I'm guessing that my Groovy script doesn't have access to something it needs to facilitate these solutions.

By the way, when the problem is that something is restricted, I do go to the in-process script approval page of the server and allow the item.

1
Not tried it, but shouldn't EnvironmentVariablesNodeProperty prop = jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class) be: EnvironmentVariablesNodeProperty prop = Jenkins.instance.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty)tim_yates
I still get the error No such property: Jenkins... In the earlier version, it is coded as I showed above and that code does run - just not on the new version of Jenkins. I'm not sure if it is due to just the version of Jenkins, or due to a missing plugin or perhaps something else.Mark Hansen

1 Answers

0
votes

I had following code..

import hudson.model.BuildableItem
import hudson.model.Job
import jenkins.model.*;


jenkins = Jenkins.instance;

This was used in some newer (2.60.3) version of Jenkins