I need to monitor what are the changes going with a job on jenkins(update the changes to a file). Need to list the env variables of a job. JOB_NAME,BUILD_NUMBER,BUILD_STATUS,GIT_URL for that build(all the builds of a job). I didn't find out a good example with the groovy. What is the best way to fetch all the info?
-
are you trying to run `groovy` script as a build step? – Paras Apr 18 '16 at 18:33
-
@pArAs yes. I'm trying to run a groovy script to find all env variables of build. – user6136315 Apr 18 '16 at 19:04
3 Answers
Depending on what you would like to achieve there are at least several approaches to retrieve and save environment variables for:
- current build
- all past builds
Get environments variables for current build (from slave)
Execute Groovy script
// Get current environment variables and save as
// a file in $WORKSPACE.
new File(".",'env.txt').withWriter('utf-8') { writer ->
System.getenv().each { key, value ->
writer.writeLine("${key}:${value}")
}
}
Using Groovy Plug-in.
Get environment variables for current build (from master)
Execute system Groovy script
// Get current environment variables and save as
// a file in $WORKSPACE.
import hudson.FilePath
def path = "env-sys.txt"
def file = null
if (build.workspace.isRemote()) {
file = new FilePath(build.workspace.channel, build.workspace.toString() + "/" + path)
} else {
file = new FilePath(build.workspace.toString() + "/" + path)
}
def output = ""
build.getEnvironment(listener).each { key, value ->
output += "${key}:${value}\n"
}
file.write() << output
Using Groovy Plug-in.
Environment variables returned by Groovy scripts are kept in map. If you don't need all of them, you can access individual values using standard operators/methods.
Get environment variables for all past builds (from master)
This approach expecst that you have installed EnvInject Plug-in and have access to $JENKINS_HOME folder:
$ find . ${JENKINS_HOME}/jobs/[path-to-your-job] -name injectedEnvVars.txt
...
ps. I suspect that one could analyze EnvInject Plug-in API and find a way to extract this information directly from Java/Groovy code.
Using EnvInject Plug-in.
To look for only specific variables you can utilize find, grep and xargs tools .
-
Everything looks great. I've one question. What is the function to get values for specific env variables(for eg: GIT_BRANCH)? instead of print all env variables. – user6136315 Apr 18 '16 at 23:00
-
You can used named indexing to get a specific environment variable, e.g. `build.getEnvironment(listener)["GIT_BRANCH"]` – TheEllis Apr 19 '16 at 12:20
You can use below script to get the Environment Variables
def thread = Thread.currentThread()
def build = thread.executable
// Get build parameters
def buildVariablesMap = build.buildVariables
// Get all environment variables for the build
def buildEnvVarsMap = build.envVars
String jobName = buildEnvVarsMap?.JOB_NAME // This is for JOB Name env variable.
Hope it helps!

- 3,197
- 2
- 20
- 30
-
This works, but the only problem is that it ends up using a deprecated method, namely `build.envVars` calls `build.getEnvVars()` which is deprecated. – TheEllis Apr 19 '16 at 12:23