You can also use environment block to inject an environment variable.
(Side note: sh
is not needed for echo)
pipeline { agent none environment { FOO = "bar" } stages { stage("first") { steps { echo "${env.FOO}" // or echo "${FOO}" } } }}
You can even define the env var inside the stage block to limit the scope:
pipeline { agent none stages { stage("first") { environment { FOO = "bar" } steps { // prints "bar" echo "${env.FOO}" // or echo "${FOO}" } } stage("second") { steps { // prints "null" echo "${env.FOO}" // or echo "${FOO}", pipeline would fail here } } }}