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,700 points
8,340 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
• 10,620 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, 2019 by Sirajul
• 59,230 points

Related Questions In Jenkins

0 votes
1 answer

Can I use multiple containers on a jenkins declarative pipeline?

Yes of course you can!! You try ...READ MORE

answered Apr 12, 2019 in Jenkins by Vedant
5,862 views
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, 2019 in Jenkins by Sirajul
• 59,230 points
1,617 views
0 votes
1 answer
0 votes
1 answer

Where can I learn the Jenkins pipeline?

Hi@akhtar You can find lots of resources available ...READ MORE

answered May 21, 2020 in Jenkins by MD
• 95,460 points
720 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
3,942 views
+2 votes
1 answer
+5 votes
3 answers

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

You can run the following code, its ...READ MORE

answered Jul 5, 2018 in Jenkins by Sophie may
• 10,620 points
119,399 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
• 10,620 points
747 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP