How can I list all the `env` properties within a Jenkins pipeline?

0 votes

Given a jenkins 2.1 build pipeline, jenkins injects a env variable into the node{}. For example, BRANCH_NAME can be accessed with

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

I want to echo all env properties within the jenkins pipeline.

I'm looking for code like

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

which would echo something like

 BRANCH_NAME is myBranch2
 CHANGE_ID is 44
 ...
Aug 9, 2018 in Jenkins by Tyrion anex
• 8,270 points
432 views

2 answers to this question.

+1 vote

One way of doing this is :

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

Another way is:

According to Jenkins documentation for declarative pipeline use :

sh 'printenv'

And for Jenkins scripted pipeline use:

echo sh(script: 'env|sort', returnStdout: true)
answered Aug 9, 2018 by Sophie may
• 9,530 points
+1 vote

Here's a quick script 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 and Jenkins variables

answered Aug 8 by Sirajul
• 25,340 points

Related Questions In Jenkins

0 votes
1 answer
0 votes
1 answer

Jenkins and Docker: How can I customize my jenkins pipeline to use docker?

Jenkins Pipeline is designed to easily use ...READ MORE

answered Aug 26 in Jenkins by Sirajul
• 25,340 points
287 views
+4 votes
7 answers

How to set the pipeline name and description for a Jenkinsfile?

You can rename pipeline jobs in jenkins ...READ MORE

answered Oct 11, 2018 in Jenkins by Hannah
• 14,070 points
6,730 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
147 views
0 votes
1 answer
0 votes
2 answers

How to trigger a Jenkins pipeline A in another Jenkins pipeline B?

If you are talking about executing another ...READ MORE

answered Jul 31 in Jenkins by Sirajul
• 25,340 points
4,151 views
0 votes
1 answer

How can I exchange jobs between 2 different Jenkins instances?

java -jar jenkins-cli.jar -s http://server get-job myjob ...READ MORE

answered Jul 6, 2018 in Jenkins by Sophie may
• 9,530 points
47 views