Reputation: 586
According to the Jenkins docs, this is how one sets a Global Environment Variable for a Declarative Pipeline:
pipeline {
agent {
label 'my-label'
}
environment {
value = 'World'
}
stages {
stage("Test") {
steps {
sh 'echo Hello, ${value}'
}
}
}
}
The output is "Hello, World" as expected.
What is the correct way to do this in a Scripted Pipeline? The following does not error, but it does not work:
node('my-label') {
environment {
value = 'World'
}
stage("Test") {
sh 'echo Hello, ${value}'
}
}
The output is "Hello, ". That is not as expected.
Upvotes: 29
Views: 40062
Reputation: 1576
In Scripted Pipelines (and in script sections of Declarative Pipelines) you can set environment variables directly via the "env" global object.
node {
env.MY_VAR = 'my-value1'
}
You can also set variables dynamically like this:
node {
def envVarName = 'MY_VAR'
env.setProperty(envVarName, 'my-value2')
}
Upvotes: 22
Reputation: 1423
Click Toggle Scripted Pipeline at this link
Jenkinsfile (Scripted Pipeline)
node {
withEnv(['DISABLE_AUTH=true',
'DB_ENGINE=sqlite']) {
stage('Build') {
sh 'printenv'
}
}
}
Your script should look something like the following:
node('my-label') {
withEnv(['value=World']) {
stage('Test') {
sh 'echo Hello, ${value}'
}
}
}
Upvotes: 28