How to list all the `env` properties in a jenkins pipeline job?

Given the creation of jenkins 2.1 build, Jenkins introduces an env variable in node{} . For example, BRANCH_NAME can be obtained using

 node { echo ${env.BRANCH_NAME} ... 

I want to highlight all the env properties as part of a jenkins project.

I am looking for code like

 node { for(e in env){ echo e + " is " + ${e} } ... 

which will resemble something like

  BRANCH_NAME is myBranch2 CHANGE_ID is 44 ... 
+65
jenkins groovy jenkins-pipeline
May 7 '16 at a.m.
source share
12 answers

Another, more concise way:

 node { echo sh(returnStdout: true, script: 'env') // ... } 

Wed https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#code-sh-code-shell-script

+60
Feb 09 '17 at 13:49 on
source share

According to the Jenkins documentation for the declarative pipeline :

 sh 'printenv' 

For Jenkins script :

 echo sh(script: 'env|sort', returnStdout: true) 

The above also sorts your env vars for convenience.

+46
Jun 19 '17 at 17:38 on
source share

You can execute the result using the sh / bat step and readFile :

 node { sh 'env > env.txt' readFile('env.txt').split("\r?\n").each { println it } } 

Unfortunately, env.getEnvironment() returns a very limited map of environment variables.

+14
May 7 '16 at
source share

The following works:

 @NonCPS def printParams() { env.getEnvironment().each { name, value -> println "Name: $name -> Value $value" } } printParams() 

Note that this is likely to crash on first run and will require the approval of various groovy methods to work in the jenkins sandbox. This is done in the "jenkins / in-process script management statement"

The list I included:

  • BUILD_DISPLAY_NAME
  • BUILD_ID
  • BUILD_NUMBER
  • BUILD_TAG
  • BUILD_URL
  • CLASSPATH
  • HUDSON_HOME
  • HUDSON_SERVER_COOKIE
  • HUDSON_URL
  • JENKINS_HOME
  • JENKINS_SERVER_COOKIE
  • JENKINS_URL
  • JOB_BASE_NAME
  • JOB_NAME
  • JOB_URL
+11
Dec 27 '16 at 13:00
source share

I use the Blue Ocean plugin and do not like each environment record to receive its own block. I want one block with all rows.

It prints poorly:

 sh 'echo `env`' 

It prints poorly:

 sh 'env > env.txt' for (String i : readFile('env.txt').split("\r?\n")) { println i } 

Good prints:

 sh 'env > env.txt' sh 'cat env.txt' 

Good prints: (as @mjfroehlich mentioned)

 echo sh(script: 'env', returnStdout: true) 
+6
May 09 '17 at 13:52
source share

Here is a quick script that you can add as a pipeline job to list all environment variables:

 node { echo(env.getEnvironment().collect({environmentVariable -> "${environmentVariable.key} = ${environmentVariable.value}"}).join("\n")) echo(System.getenv().collect({environmentVariable -> "${environmentVariable.key} = ${environmentVariable.value}"}).join("\n")) } 

This will list both system variables and Jenkins variables.

+6
Jan 17 '18 at 18:28
source share

The answers above are now deprecated due to the new pipeline syntax. Environment variables are printed below.

 script { sh 'env > env.txt' String[] envs = readFile('env.txt').split("\r?\n") for(String vars: envs){ println(vars) } } 
+3
Mar 28 '17 at 17:45
source share

Why is all this complicated?

 sh 'env' 

does what you need (under * nix)

+2
Aug 23 '18 at 10:52
source share

Cross-platform way of listing all environment variables:

 if (isUnix()) { sh env } else { bat set } 
+2
Nov 14 '18 at 20:45
source share

Another way to get exactly the result indicated in the question:

 envtext= "printenv".execute().text envtext.split('\n').each { envvar=it.split("=") println envvar[0]+" is "+envvar[1] } 

This can be easily expanded to build a map with a subset of env vars matching the criteria:

 envdict=[:] envtext= "printenv".execute().text envtext.split('\n').each { envvar=it.split("=") if (envvar[0].startsWith("GERRIT_")) envdict.put(envvar[0],envvar[1]) } envdict.each{println it.key+" is "+it.value} 
0
Sep 29 '17 at 8:28
source share

I suppose you need this in the form of a script, but if someone else just wants to view the Jenkins GUI, this list can be found by selecting the "Environment Variables" section in the contextual left menu of each assembly. Select project => Select build => Environment Variables

enter image description here

0
Jul 12 '18 at 8:25
source share

if you really want env list just do:

def envs = sh(returnStdout: true, script: 'env').split('\n') envs.each { name → println "Name: $name" }

0
Sep 05 '18 at 15:14
source share



All Articles