I searched a lot for this problem but couldn't find working solution anywhere. Can anybody please help me out? I want to get already existing env vars value through jenkins script console.
Asked
Active
Viewed 3,003 times
3
-
You can print all the environment variables, using - sh 'printenv' – user_9090 Jun 07 '19 at 06:36
2 Answers
2
You need to distinguish:
-
def myVar = build.getBuildVariables().get('myVar')
-
System.getenv('MY_VARIABLE')
If you see
groovy.lang.MissingPropertyException: No such property: manager for class: Script1
Check this answer, and define build
first:
import hudson.model.*
def build = Thread.currentThread().executable
def buildNumber = build.number

VonC
- 1,262,500
- 529
- 4,410
- 5,250
-
I'm using build env vars..Writing only this in jenkins script console : def myVar = build.getBuildVariables().get('BUILD_NUMBER') is giving : groovy.lang.MissingPropertyException: No such property: build for class: Script1 . output. – Tarishi Jain Jun 06 '19 at 08:55
-
-
getting this error now: groovy.lang.MissingPropertyException: No such property: executable for class: java.lang.Thread ..Please help! – Tarishi Jain Jun 06 '19 at 09:57
-
@TarishiJain Check if https://stackoverflow.com/a/38130496/6309 can help for that second error message. – VonC Jun 06 '19 at 09:58
0
According to this answer, in order to access env vars from Jenkins script console, do as follows :
import jenkins.model.*;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.EnvVars;
jenkins = Jenkins.instance;
EnvironmentVariablesNodeProperty prop = jenkins.getGlobalNodeProperties().get(EnvironmentVariablesNodeProperty.class)
EnvVars env = prop.getEnvVars()
def myVariable = env['MY_VAR']
The env vars listed in http://<JENKINS_URL>/env-vars.html
are available for each build. In order to access these variables in the Jenkins script console you need to define first the build :
build = Jenkins.instance.getItemByFullName('JOB_NAME').getBuildByNumber(BUILD_NUMBER)
envvars = build.getEnvironment()
envvars.each{envvar ->
println envvar
}

SmartTom
- 691
- 7
- 14
-
-
This gives you the env vars defined globally on every nodes (EnvironmentVariablesNodeProperty). What env vars do you want ? For a specific build or a specific node ? When do you want to access it ? While running a build or through the script console once the build is runned ? – SmartTom Jun 06 '19 at 10:26
-
I want to get the values of env vars listed here: http://
/env-vars.html/ ..and want to acess them via jenkins script console (by writing groovy script in there). – Tarishi Jain Jun 06 '19 at 11:25 -
I edited my answer. The env vars you want access are defined for each build, so you need first to define the build. Replace `JOB_NAME` with the name of your job and `BUILD_NUMBER` by the number of the build you want to access its env vars. – SmartTom Jun 10 '19 at 09:24