How to list all `env` properties within jenkins pipeline job?

JamesThomasMoon1979 picture JamesThomasMoon1979 · May 7, 2016 · Viewed 149.6k times · Source

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

...considering that I do not know all properties ahead of time.

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
 ...

Answer

Wimateeka picture Wimateeka · Jun 19, 2017

According to Jenkins documentation for declarative pipeline:

sh 'printenv'

For Jenkins scripted pipeline:

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

The above also sorts your env vars for convenience.