Pipeline - How to print out env variables available in a build

Issue

How can I print all of the current environment variables in a Pipeline build?

Environment

  • Jenkins
  • CloudBees Jenkins Enterprise
  • Pipeline plugin

Resolution

There is a RFE for this issue as it can be seen on JENKINS-36528.

As a workaround: there are different resolution depending on the OS type of the agent/node. Add the following snippets in the scope where you wish to know the environment variables available

For linux env

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

A similar output that this one would be expected:

Started by user UserX
[Pipeline] Allocate node : Start
Running on principal in /jenkins_home/jobs/pipeline_example/workspace
[Pipeline] node {
[Pipeline] sh
[workspace] Running shell script
+ env
[Pipeline] readFile
[Pipeline] echo
LESSOPEN=| /usr/bin/lesspipe %s
[Pipeline] echo
JENKINS_HOME=/jenkins_home
[Pipeline] echo
...
SSH_AGENT_PID=1149
...
HUDSON_COOKIE=127483ad-7a1b-4669-bfe2-68ad2f49d733
...
MAVEN_HOME=/home/carlosrodlop/opt/maven/apache-maven-3.3.9
...
HTTP_ADDRESS=0.0.0.0
...
HTTP_PORT=8183
...
PATH=/usr/local/sbin:/usr/local/bin:...
...
EXAMPLE_JENKINS_VARIABLE=Test Variable
...
BUILD_TAG=jenkins-pipeline_example-1
...
SSH_AUTH_SOCK=/tmp/ssh-gAI1d7HUdJ4a/agent.1038
...
BUILD_NUMBER=1
...
HUDSON_SERVER_COOKIE=c76ea747cf0360cc
...
JOB_NAME=pipeline_example
...
PWD=/jenkins_home/jobs/pipeline_example/workspace
...
DEBUG_PORT=8193
...
[Pipeline] } //node
[Pipeline] Allocate node : End
[Pipeline] End of Pipeline
Finished: SUCCESS

For windows env

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

A similar output that this one would be expected:

Started by user UserX
[Pipeline] node
Running on master in C:\.jenkins\jobs\pipeline_example\workspace
[Pipeline] {
[Pipeline] bat
[workspace] Running batch script
C:\.jenkins\jobs\pipeline_example\workspace>set  1>env.txt 
[Pipeline] readFile
[Pipeline] echo
...
BUILD_DISPLAY_NAME=#29
...
BUILD_ID=29
...
BUILD_NUMBER=29
...
BUILD_TAG=jenkins-example_job-29
...
BUILD_URL=http://jenkins.example/job/pipeline_example/29/
...
HUDSON_HOME=C:\.jenkins
...
HUDSON_SERVER_COOKIE=5d726cd684fad3f3
... 
HUDSON_URL=http://jenkins.example:8080/
...
JENKINS_HOME=C:\.jenkins
... 
JENKINS_SERVER_COOKIE=durable-5fe1ef76e59e42e0d503e3d9c9f7613c
... 
JENKINS_URL=http://jenkins.example:8080:8080/
...
JOB_NAME=pipeline_example
...
JOB_URL=http://localhost:8080/job/pipeline_example/
...
NODE_LABELS=master
...
NODE_NAME=master
...
NUMBER_OF_PROCESSORS=1
...
OS=Windows_NT
...
Path=C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\
...
WORKSPACE=C:\Users\userx\.jenkins\jobs\pipeline_example\workspace
[Pipeline] }
[Pipeline] // node

[Pipeline] End of Pipeline
Finished: SUCCESS


References

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.