matandked matandked - 3 months ago 158
Groovy Question

How to get environment variable in Jenkins Groovy script console?

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

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

I've tried to find appropriate solution (for example 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 works for me.

I've tried for example:


and'myVar') //no manager error


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


import jenkins.model.Jenkins


You can get global properties like this:

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

I referred to the link below, about how to set global properties programatically.!topic/jenkinsci-users/KgCGuDmED1Q