Search code examples
jenkinsjenkins-pipelinejenkins-groovy

Whats is the difference between ${env.VAR}, ${VAR} or $VAR way of defining Jenkins declarative pipeline env varaibles?


I have seen different ways in which variables are referenced in Jenkins declarative pipelines. I would like to know if there is any difference in the following ways in which a variable can be referenced?

${env.VAR}
${VAR}
$VAR 

Solution

  • "${VAR}" and "$VAR" are equivalent. You use the curly braces to separate the variable name from the rest of the string you are interpolating the variable into to make sure the interpreter won't read more than you want as the name of the variable. See http://docs.groovy-lang.org/latest/html/documentation/#_string_interpolation for details. Let me demonstrate that.

    Consider the following snippet:

    VAR="foo"
    "$VARbar"
    

    This will error out as VARbar is not a valid variable in this scope.

    But if you try:

    VAR="foo"
    "${VAR}bar"
    

    , then you get "foobar" as a result, as expected.

    Now for the env.VAR form I still couldn't find conclusive evidence of what is the difference. I'm thinking that Jenkins automatically treats all global variables as ENV variables, similarish to how Make and Bash scripts work, but I'd love to see an actual reference for this behavior. ~I'm too lazy to test this throughly.~