Jenkins Global environment variables in Jenkinsfile

97.2k Views Asked by At

How do I invoke Global environment variables in Jenkinsfile?
For example, if I have a variable -

 name:credentialsId 
 value:xxxx-xxxx-xxxxx-xxxxxxxxx

How do I use it in the groovy script?

I tried ${credentialsId}, but it didn't work. It will just give error:

java.lang.NoSuchMethodError: No such DSL method '$' found among steps [ArtifactoryGradleBuild, ........
5

There are 5 best solutions below

4
On

Another syntax is $ENV:xxxx

node {
echo "Running $ENV.BUILD_ID on $ENV.JENKINS_URL" }

This worked for me

2
On

In a Jenkinsfile, you have the "Working with the Environment" which mentions:

The full list of environment variables accessible from within Jenkins Pipeline is documented at localhost:8080/pipeline-syntax/globals#env,

The syntax is ${env.xxx} as in:

node {
    echo "Running ${env.BUILD_ID} on ${env.JENKINS_URL}"
}

See also "Managing the Environment".

How can I pass the Global variables to the Jenkinsfile?
When I say Global variables - I mean in

Jenkins -> Manage Jenkins -> Configure System -> Global properties -> Environment variables

See "Setting environment variables"

Setting an environment variable within a Jenkins Pipeline can be done with the withEnv step, which allows overriding specified environment variables for a given block of Pipeline Script, for example:

Jenkinsfile (Pipeline Script)

node {
    /* .. snip .. */
    withEnv(["NAME=value"]) {
        ... your job
    }
}

What's the syntax for withEnv if I need to initialize the env variable with another env variable?

If you want to initialize an environment variable with the value of another environment variable in a Jenkinsfile, you can use the env object to reference the existing variable.
Here is an example:

node {
    withEnv(["NEW_VAR=${env.OLD_VAR}"]) {
        echo "New variable: ${env.NEW_VAR}"
    }
}

In this example, NEW_VAR is a new environment variable that gets its value from OLD_VAR, an existing environment variable.
The echo step then prints the value of NEW_VAR to the console.

0
On

Scripted pipeline To read an environment variable whose name you know, use env.NAME

To read an environment variable whose name is not known until runtime use env.getProperty(name).

For example, a value from a YAML config file represents an environment variable name:

config.yaml (in workspace)

myconfig:
  key: JOB_DISPLAY_URL

Jenkinsfile

node {
    println("Running job ${env.JOB_NAME}")
    def config = readYaml(file:'config.yaml')
    def value = env.getProperty(config.myconfig.key)
    println("Value of property ${config.myconfig.key} is ${value}")
}
1
On

For getting values all env.VAR, env['VAR'], env.getProperty('VAR') are fine.
For setting values the only safe way at the moment is withEnv. If you try to assign values to env.VAR it may not work in some cases like for parallel pipelines (like in JENKINS-59871).

0
On

When referring to env in Groovy scope, simply use env.VARIABLE_NAME, for example to pass on BUILD_NUMBER of upstream job to a triggered job:

stage ('Starting job') {
    build job: 'TriggerTest', parameters: [
        [$class: 'StringParameterValue', name: 'upstream_build_number', value: env.BUILD_NUMBER]
    ]
}