matandked matandked - 1 month ago 59
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:

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")

Answer

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. https://groups.google.com/forum/#!topic/jenkinsci-users/KgCGuDmED1Q

Comments