pipeline { agent any environment { BUILD_NUMBER = "123" } stages { stage("Env Variables") { steps { echo"The build number is ${env.BUILD_NUMBER}" echo"You can also use \${BUILD_NUMBER} -> ${BUILD_NUMBER}" sh 'echo "I can access $BUILD_NUMBER in shell command as well."' script{ echo"The build number is ${env.BUILD_NUMBER}" echo"You can also use \${BUILD_NUMBER} -> ${BUILD_NUMBER}" sh 'echo "I can access $BUILD_NUMBER in shell command as well."' } } } } }
stage("Override Variables") { steps { script { env.FOO = "IT DOES NOT WORK!"// it can't override env.FOO declared at the pipeline (or stage) level env.SOMETHING = "2"// it can override env variable created imperatively }
echo "FOO = ${env.FOO}"// prints "FOO = bar" echo "SOMETHING = ${env.SOMETHING}"// prints "SOMETHING = 2"
withEnv(["FOO=foobar"]) { // it can override any env variable echo "FOO = ${env.FOO}"// prints "FOO = foobar" }
stages { stage("Env Variables") { steps { script { if (env.IS_BOOLEAN) { echo "You can see this message, because \"false\" String evaluates to Boolean.TRUE value" }
if (env.IS_BOOLEAN.toBoolean() == false) { echo "You can see this message, because \"false\".toBoolean() returns Boolean.FALSE value" } } } } } }
使用sh捕获环境变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
pipeline { agent any
environment { LS = "${sh(script:'ls -lah', returnStdout: true)}" }