How can I list the variables already available inside Jenkinsfile?

19,082

Solution 1

To see environment variables, try:

node {
  sh 'env | sort'
}

Solution 2

From your Pipeline job configuration page, click 'Pipeline Syntax' link on the left nav. It opens and on its left nav click Global variables. It lists many variables which could be used within Pipeline script, including a set of environment variables are made available to all Jenkins projects.

An example of loading variable values from Groovy:

mail to: '[email protected]',
    subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) is waiting for input",
    body: "Please go to ${env.BUILD_URL} and verify the build"

Solution 3

in our case we used the following to get all groovy-defined variables within a jenkinsfile groovy pipeline:

groovyVars = [:] << getBinding().getVariables()
groovyVars.each  {k,v -> print "$k = $v"}
Share:
19,082
sorin
Author by

sorin

Another geek still trying to decipher the meaning of “42”. It seems that amount his main interest are: online communities of practice and the way they evolve in time product design, simplicity in design and accessibility productivity and the way the IT solutions are impacting it

Updated on June 09, 2022

Comments

  • sorin
    sorin almost 2 years

    I am trying to use some variables like repository name, branch names, build number,... from inside the Jenkinsfile but I am not able to find any documentation regarding this.

    Is there a way to print them so I can see what's available? How?

    I think it may be related to Printing out variables and values in a Groovy object

    Update

    this.binding.variables.each {k,v -> println "$k = $v"}
    

    I tried to include the code above but now execution fails with

    org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use field groovy.lang.Binding variables
        at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectField(StaticWhitelist.java:177)
    
  • Jesse Glick
    Jesse Glick about 8 years
    Yes that is the official documentation.
  • 诺 铁
    诺 铁 almost 7 years
    where is this "Global variables section" I can't find it.
  • gbonetti
    gbonetti over 6 years
    They are now under "Pipeline Sintax", at the end of the page. Or you can just append /pipeline-syntax/globals to your job URL
  • Harun Diluka Heshan
    Harun Diluka Heshan about 6 years
    pls provide more detailed solution.
  • Antonio Petricca
    Antonio Petricca about 6 years
    This does not show variables defined inside "script" blocks.
  • robross0606
    robross0606 over 3 years
    Like @AntonioPetricca I'd love to know how you do this for non-env (Groovy?) variables defined in blocks. Especially if you're using the declarative syntax.