Jenkins – How to get environment variable in Jenkins Groovy script console

groovyJenkins

In Jenkins configuration (http://JenkinsURL/configure) within "Global properties" I defined some "Environment variables".

How can I access them in the Groovy Script console (http://JenkinsURL/script)?

I've tried to find appropriate solution (for example the solutions mentioned in: Access to build environment variables from a groovy script in a Jenkins build step (Windows))
but it seems that none of them work for me.

I've tried for example:

System.getenv("myVar")

and

manager.build.getEnvironment(listener).get('myVar') //no manager error

and

import jenkins.model.Jenkins
Jenkins.instance.getProperty('myVar') //No signature of method: hudson.model.Hudson.getProperty() is applicable for argument types: (java.lang.String)

and

import jenkins.model.Jenkins
Jenkins.instance.ParameterValue("DEV_local")

Best Answer

You can get global properties like this:

import jenkins.model.Jenkins
def envVars = Jenkins.instance.getGlobalNodeProperties()[0].getEnvVars() 
println envVars['myVar']

I referred to the link below, about how to set global properties programatically. https://groups.google.com/forum/#!topic/jenkinsci-users/KgCGuDmED1Q

Related Topic