Retrieve all properties of env in Jenkinsfile

15,479

Solution 1

Make sure you're not running the pipeline script in sandboxed mode and you should be able to use:

env.getEnvironment()

Note, if you're running in sandbox mode in a pipeline, you should approve the method at the script approval page: http://jenkins-host/scriptApproval/

Solution 2

To retrieve all env properties using a Jenkinsfile written in either declarative or scripted DSL you can use:

sh 'env'                       

or

sh 'printenv'

Solution 3

As said over here: https://stackoverflow.com/a/42138466/618253

The declarative pipeline way of doing things:

node {
   echo sh(returnStdout: true, script: 'env')
}
Share:
15,479

Related videos on Youtube

Krzysztof Krasoń
Author by

Krzysztof Krasoń

Updated on September 16, 2022

Comments

  • Krzysztof Krasoń
    Krzysztof Krasoń over 1 year

    I would like to print all available properties (and their values) in env object inside Jenkinsfile.

    When I do

    print env
    

    I get:

    org.jenkinsci.plugins.workflow.cps.EnvActionImpl@112cebc2
    

    So it looks like toString is not implemented there, how can I access properties that are in this object if I don't know their names?

    • tim_yates
      tim_yates about 8 years
      Does println env.overriddenEnvironment shed more light on it?
    • Krzysztof Krasoń
      Krzysztof Krasoń almost 8 years
      It gives me null
    • tim_yates
      tim_yates almost 8 years
      Same with env.environment ?
    • Krzysztof Krasoń
      Krzysztof Krasoń almost 8 years
      yes, it gives null also
    • luka5z
      luka5z almost 8 years
      I've posted an answer for stackoverflow.com/questions/37083285/… which might be helpful.