jenkins流水线-环境变量

列出环境变量

使用printenv | sort命令组合来获取环境变量的排序列表

1
2
3
4
5
6
7
8
9
10
11
pipeline {
agent any

stages {
stage("Env Variables") {
steps {
sh "printenv | sort"
}
}
}
}

读取环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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."'
}
}
}
}
}

设置环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
pipeline {
agent any
stages {
stage("Env Variables") {
steps {
script {
env.TEST_VARIABLE = "some test value"
TEST_VARIABLE2 = "some test value2"

echo "ENV TEST_VARIABLE = ${env.TEST_VARIABLE}"
echo "ENV TEST_VARIABLE2 = ${env.TEST_VARIABLE2}"

echo "TEST_VARIABLE = ${TEST_VARIABLE}"
echo "TEST_VARIABLE2 = ${TEST_VARIABLE2}"
}
echo "ENV TEST_VARIABLE = ${env.TEST_VARIABLE}"
echo "ENV TEST_VARIABLE2 = ${env.TEST_VARIABLE2}"

echo "TEST_VARIABLE = ${TEST_VARIABLE}"
echo "TEST_VARIABLE2 = ${TEST_VARIABLE2}"
}
}
}
}

image-20211021185750858

  • 脚本式和声明式 env.设置的变量可以通过任何方式读取

  • 使用 设置的只能用读取

  • 可以使用environment { }block 来声明性地设置环境变量 不能再steps中设置变量

  • 必须使用env.或命令来设置环境变量withEnv([“VARIABLE_NAME=“]) {}。

覆盖环境变量

  • 该withEnv([“env=value]) { }块可以覆盖任何环境变量。
  • 使用environment {}块设置的变量不能使用命令式env.VAR = “value”赋值覆盖。
  • 命令式env.VAR = “value”分配只能覆盖使用命令式创建的环境变量。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
pipeline {
agent any

environment {
FOO = "bar"
NAME = "Joe"
}

stages {
stage("Env Variables") {
environment {
NAME = "Alan" // overrides pipeline level NAME env variable
BUILD_NUMBER = "2" // overrides the default BUILD_NUMBER
}

steps {
echo "FOO = ${env.FOO}" // prints "FOO = bar"
echo "NAME = ${env.NAME}" // prints "NAME = Alan"
echo "BUILD_NUMBER = ${env.BUILD_NUMBER}" // prints "BUILD_NUMBER = 2"

script {
env.SOMETHING = "1" // creates env.SOMETHING variable
}
}
}

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"
}

withEnv(["BUILD_NUMBER=1"]) {
echo "BUILD_NUMBER = ${env.BUILD_NUMBER}" // prints "BUILD_NUMBER = 1"
}
}
}
}
}

image-20211021190715697

将布尔值存储在环境变量中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
pipeline {
agent any

environment {
IS_BOOLEAN = false
}

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)}"
}

stages {
stage("Env Variables") {
steps {
echo "LS = ${env.LS}"
}
}
}
}