Jenkins高级篇之Pipeline技巧篇-4-根据参数传入条件控制执行不同stage

释放双眼,带上耳机,听听看~!

这篇我来介绍一下之前,很早的时候介绍pipeline语法的时候,有一个指令叫when 和expression,当时由于pipeline知识学习太少,不好举例子去学习消化。到了这里,其实这两个关键字就是用来控制stage的执行,如果你条件有好几个,可以精确控制让哪一些stage执行,让哪一些stage不执行。

我这里举例一个自动化测试中的例子,例如我写了多个stage,这个pipeline脚本执行执行冒烟测试,和集成测试。有时候,我们希望快速执行冒烟测试,想根据结果看看,不一定一上来就执行集成测试。为了达到这种控制效果,我们就需要使用逻辑控制。在pipeline中就使用when 和expression两个命令。例如,如果json文件中冒烟测试变量为true,我就只执行冒烟测试的stage,其他和冒烟测试无关的stage我就不去执行。如果冒烟测试变量值为false,也就是默认要跑集成测试(不跑冒烟测试)。为了不搞得这么复杂,我们就分两个类型测试就好。下面,我用pipeline代码方式去实现。

1.json准备


1
2
3
4
5
6
7
8
9
10
11
12
1{
2  "NAME" : "Lucy",
3  "AGE" : "18",
4  "PHONE_NUMBER" : "13912345678",
5  "ADDRESS" : "Haidian Beijing",
6  "EMAIL" : "lucy@demo.com",
7  "GENDER" : "male",
8  "SMOKE": true,
9  "IS_MARRY" : false
10}
11
12

我还是在前面文章的/tmp/anthony/test.json文件基础上加了一个变量 SMOKE, 默认值是true。

2.pipeline stage groovy文件代码

由于我添加了一个SMOKE的变量,所以在初始化stage,我们新增一行代码,初始化SMOKE的变量。


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
1import hudson.model.*;
2
3
4pipeline{
5  
6   agent any
7   stages{
8       stage("Hello Pipeline") {
9           steps {
10              script {
11                  println "Hello Pipeline!"
12                  println env.JOB_NAME
13                  println env.BUILD_NUMBER
14              }
15          }
16      }
17     
18      stage("Init paramters in json") {
19          steps {
20              script {
21                  println "read josn input file"
22                  json_file = INPUT_JSON? INPUT_JSON.trim() : ""
23                  prop = readJSON file : json_file
24                  name = prop.NAME? prop.NAME.trim() : ""
25                  println "Name:" + name
26                  age = prop.AGE? prop.AGE.trim() : ""
27                  println "Age:" + age
28                  phone = prop.PHONE_NUMBER? prop.PHONE_NUMBER.trim() : ""
29                  println "Phone:" + phone
30                  address = prop.ADDRESS? prop.ADDRESS.trim() : ""
31                  println "Address:" + address
32                  email = prop.EMAIL? prop.EMAIL.trim() : ""
33                  println "Email:" + email
34                  gender = prop.GENDER? prop.GENDER.trim() : ""
35                  println "Gender:" + gender
36                  is_marry = prop.IS_MARRY? prop.IS_MARRY : false
37                  println "is_marry:" + is_marry
38                  is_smoke = prop.SMOKE? prop.SMOKE : false
39                                        println "is_smoke:" + is_smoke
40              }
41          }
42      }
43      stage("call a method") {
44          steps {
45              script {
46                  println "send the parameter as map type"
47                  model_call = load env.WORKSPACE + "/groovy/projectA-model.groovy"
48                  model_call.getUserInfo(name:name, age:age, phone:phone, address:address, email:email, gender:gender, is_marry:is_marry)
49              }
50          }
51      }
52      stage("check serive up") {
53          when {
54              expression {
55                  return (is_smoke == true)
56              }
57          }
58          steps {
59              script {
60                  println "SMOKE TEST: check service startup"
61              }
62          }
63      }
64        stage("check UI login") {
65          when {
66              expression {
67                  return (is_smoke == true)
68              }
69          }
70          steps {
71              script {
72                  println "SMOKE TEST: check UI login success"
73              }
74          }
75      }
76     
77      stage("Integrate-ModelA") {
78          when {
79              expression {
80                  return (is_smoke == false)
81              }
82          }
83          steps {
84              script {
85                  println "Integrate-ModelA"
86              }
87          }
88      }
89     
90      stage("Integrate-ModelB") {
91          when {
92              expression {
93                  return (is_smoke == false)
94              }
95          }
96          steps {
97              script {
98                  println "Integrate-ModelB"
99              }
100         }
101     }
102 }
103
104}
105
106
107

3.测试效果

只跑冒烟测试,也就是SMOKE的值在json中是true

我这里直接贴出运行日志


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
1Started by user root
2Rebuilds build #9
3Rebuilds build #15
4Rebuilds build #16
5Rebuilds build #17
6Obtained src/Jenkinsfile/projectA-stages.groovy from git https://github.com/Anthonyliu86/pipeline-skills-demo.git
7Running in Durability level: MAX_SURVIVABILITY
8[Pipeline] Start of Pipeline
9[Pipeline] node
10Running on Jenkins in /var/lib/jenkins/workspace/ProjectA-pipeline-demo
11[Pipeline] {
12[Pipeline] stage
13[Pipeline] { (Declarative: Checkout SCM)
14[Pipeline] checkout
15using credential 03214975-2168-4795-981a-ddd935f62a76
16 > git rev-parse --is-inside-work-tree # timeout=10
17Fetching changes from the remote Git repository
18 > git config remote.origin.url https://github.com/Anthonyliu86/pipeline-skills-demo.git # timeout=10
19Fetching upstream changes from https://github.com/Anthonyliu86/pipeline-skills-demo.git
20 > git --version # timeout=10
21using GIT_ASKPASS to set credentials
22 > git fetch --tags --progress https://github.com/Anthonyliu86/pipeline-skills-demo.git +refs/heads/*:refs/remotes/origin/*
23 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
24 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
25Checking out Revision 35f0fbe78b205b5e0cdbf94444722411a7ebdb62 (refs/remotes/origin/master)
26 > git config core.sparsecheckout # timeout=10
27 > git checkout -f 35f0fbe78b205b5e0cdbf94444722411a7ebdb62
28Commit message: "fix"
29 > git rev-list --no-walk ff9cbf42c55cb87c863bb6b96d511ccc496eff80 # timeout=10
30[Pipeline] }
31[Pipeline] // stage
32[Pipeline] withEnv
33[Pipeline] {
34[Pipeline] stage
35[Pipeline] { (Hello Pipeline)
36[Pipeline] script
37[Pipeline] {
38[Pipeline] echo
39Hello Pipeline!
40[Pipeline] echo
41ProjectA-pipeline-demo
42[Pipeline] echo
4318
44[Pipeline] }
45[Pipeline] // script
46[Pipeline] }
47[Pipeline] // stage
48[Pipeline] stage
49[Pipeline] { (Init paramters in json)
50[Pipeline] script
51[Pipeline] {
52[Pipeline] echo
53read josn input file
54[Pipeline] readJSON
55[Pipeline] echo
56Name:Lucy
57[Pipeline] echo
58Age:18
59[Pipeline] echo
60Phone:13912345678
61[Pipeline] echo
62Address:Haidian Beijing
63[Pipeline] echo
64Email:lucy@demo.com
65[Pipeline] echo
66Gender:male
67[Pipeline] echo
68is_marry:false
69[Pipeline] echo
70is_smoke:true
71[Pipeline] }
72[Pipeline] // script
73[Pipeline] }
74[Pipeline] // stage
75[Pipeline] stage
76[Pipeline] { (call a method)
77[Pipeline] script
78[Pipeline] {
79[Pipeline] echo
80send the parameter as map type
81[Pipeline] load
82[Pipeline] { (/var/lib/jenkins/workspace/ProjectA-pipeline-demo/groovy/projectA-model.groovy)
83[Pipeline] }
84[Pipeline] // load
85[Pipeline] echo
86
87  Lucy come from Haidian Beijing, he is 18 old. he's phone number is
88  13912345678, or you can contact he via lucy@demo.com, he is not marry yet.
89 
90[Pipeline] }
91[Pipeline] // script
92[Pipeline] }
93[Pipeline] // stage
94[Pipeline] stage
95[Pipeline] { (check serive up)
96[Pipeline] script
97[Pipeline] {
98[Pipeline] echo
99SMOKE TEST: check service startup
100[Pipeline] }
101[Pipeline] // script
102[Pipeline] }
103[Pipeline] // stage
104[Pipeline] stage
105[Pipeline] { (check UI login)
106[Pipeline] script
107[Pipeline] {
108[Pipeline] echo
109SMOKE TEST: check UI login success
110[Pipeline] }
111[Pipeline] // script
112[Pipeline] }
113[Pipeline] // stage
114[Pipeline] stage
115[Pipeline] { (Integrate-ModelA)
116Stage "Integrate-ModelA" skipped due to when conditional
117[Pipeline] }
118[Pipeline] // stage
119[Pipeline] stage
120[Pipeline] { (Integrate-ModelB)
121Stage "Integrate-ModelB" skipped due to when conditional
122[Pipeline] }
123[Pipeline] // stage
124[Pipeline] }
125[Pipeline] // withEnv
126[Pipeline] }
127[Pipeline] // node
128[Pipeline] End of Pipeline
129Finished: SUCCESS
130

只跑集成测试,先去test.json把SMOKE的值改成false


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
1Started by user root
2Rebuilds build #9
3Rebuilds build #15
4Rebuilds build #16
5Rebuilds build #17
6Obtained src/Jenkinsfile/projectA-stages.groovy from git https://github.com/Anthonyliu86/pipeline-skills-demo.git
7Running in Durability level: MAX_SURVIVABILITY
8[Pipeline] Start of Pipeline
9[Pipeline] node
10Running on Jenkins in /var/lib/jenkins/workspace/ProjectA-pipeline-demo
11[Pipeline] {
12[Pipeline] stage
13[Pipeline] { (Declarative: Checkout SCM)
14[Pipeline] checkout
15using credential 03214975-2168-4795-981a-ddd935f62a76
16 > git rev-parse --is-inside-work-tree # timeout=10
17Fetching changes from the remote Git repository
18 > git config remote.origin.url https://github.com/Anthonyliu86/pipeline-skills-demo.git # timeout=10
19Fetching upstream changes from https://github.com/Anthonyliu86/pipeline-skills-demo.git
20 > git --version # timeout=10
21using GIT_ASKPASS to set credentials
22 > git fetch --tags --progress https://github.com/Anthonyliu86/pipeline-skills-demo.git +refs/heads/*:refs/remotes/origin/*
23 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
24 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
25Checking out Revision 35f0fbe78b205b5e0cdbf94444722411a7ebdb62 (refs/remotes/origin/master)
26 > git config core.sparsecheckout # timeout=10
27 > git checkout -f 35f0fbe78b205b5e0cdbf94444722411a7ebdb62
28Commit message: "fix"
29 > git rev-list --no-walk ff9cbf42c55cb87c863bb6b96d511ccc496eff80 # timeout=10
30[Pipeline] }
31[Pipeline] // stage
32[Pipeline] withEnv
33[Pipeline] {
34[Pipeline] stage
35[Pipeline] { (Hello Pipeline)
36[Pipeline] script
37[Pipeline] {
38[Pipeline] echo
39Hello Pipeline!
40[Pipeline] echo
41ProjectA-pipeline-demo
42[Pipeline] echo
4318
44[Pipeline] }
45[Pipeline] // script
46[Pipeline] }
47[Pipeline] // stage
48[Pipeline] stage
49[Pipeline] { (Init paramters in json)
50[Pipeline] script
51[Pipeline] {
52[Pipeline] echo
53read josn input file
54[Pipeline] readJSON
55[Pipeline] echo
56Name:Lucy
57[Pipeline] echo
58Age:18
59[Pipeline] echo
60Phone:13912345678
61[Pipeline] echo
62Address:Haidian Beijing
63[Pipeline] echo
64Email:lucy@demo.com
65[Pipeline] echo
66Gender:male
67[Pipeline] echo
68is_marry:false
69[Pipeline] echo
70is_smoke:true
71[Pipeline] }
72[Pipeline] // script
73[Pipeline] }
74[Pipeline] // stage
75[Pipeline] stage
76[Pipeline] { (call a method)
77[Pipeline] script
78[Pipeline] {
79[Pipeline] echo
80send the parameter as map type
81[Pipeline] load
82[Pipeline] { (/var/lib/jenkins/workspace/ProjectA-pipeline-demo/groovy/projectA-model.groovy)
83[Pipeline] }
84[Pipeline] // load
85[Pipeline] echo
86
87  Lucy come from Haidian Beijing, he is 18 old. he's phone number is
88  13912345678, or you can contact he via lucy@demo.com, he is not marry yet.
89 
90[Pipeline] }
91[Pipeline] // script
92[Pipeline] }
93[Pipeline] // stage
94[Pipeline] stage
95[Pipeline] { (check serive up)
96[Pipeline] script
97[Pipeline] {
98[Pipeline] echo
99SMOKE TEST: check service startup
100[Pipeline] }
101[Pipeline] // script
102[Pipeline] }
103[Pipeline] // stage
104[Pipeline] stage
105[Pipeline] { (check UI login)
106[Pipeline] script
107[Pipeline] {
108[Pipeline] echo
109SMOKE TEST: check UI login success
110[Pipeline] }
111[Pipeline] // script
112[Pipeline] }
113[Pipeline] // stage
114[Pipeline] stage
115[Pipeline] { (Integrate-ModelA)
116Stage "Integrate-ModelA" skipped due to when conditional
117[Pipeline] }
118[Pipeline] // stage
119[Pipeline] stage
120[Pipeline] { (Integrate-ModelB)
121Stage "Integrate-ModelB" skipped due to when conditional
122[Pipeline] }
123[Pipeline] // stage
124[Pipeline] }
125[Pipeline] // withEnv
126[Pipeline] }
127[Pipeline] // node
128[Pipeline] End of Pipeline
129Finished: SUCCESS
130
131

再看看stage view的图片效果

其实,如果你还可以利用第二个参数,写在expression中,来控制当什么条件符合的时候,执行全部的冒烟加上集成测试,这样的效果都是可以实现的。

json文件如下


1
2
3
4
5
6
7
8
9
10
11
12
13
1{
2  "NAME" : "Lucy",
3  "AGE" : "18",
4  "PHONE_NUMBER" : "13912345678",
5  "ADDRESS" : "Haidian Beijing",
6  "EMAIL" : "lucy@demo.com",
7  "GENDER" : "male",
8  "SMOKE": false,
9  "FULL_TEST":true,
10  "IS_MARRY" : false
11}
12
13

stage 代码修改如下


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
1import hudson.model.*;
2
3
4pipeline{
5  
6   agent any
7   stages{
8       stage("Hello Pipeline") {
9           steps {
10              script {
11                  println "Hello Pipeline!"
12                  println env.JOB_NAME
13                  println env.BUILD_NUMBER
14              }
15          }
16      }
17     
18      stage("Init paramters in json") {
19          steps {
20              script {
21                  println "read josn input file"
22                  json_file = INPUT_JSON? INPUT_JSON.trim() : ""
23                  prop = readJSON file : json_file
24                  name = prop.NAME? prop.NAME.trim() : ""
25                  println "Name:" + name
26                  age = prop.AGE? prop.AGE.trim() : ""
27                  println "Age:" + age
28                  phone = prop.PHONE_NUMBER? prop.PHONE_NUMBER.trim() : ""
29                  println "Phone:" + phone
30                  address = prop.ADDRESS? prop.ADDRESS.trim() : ""
31                  println "Address:" + address
32                  email = prop.EMAIL? prop.EMAIL.trim() : ""
33                  println "Email:" + email
34                  gender = prop.GENDER? prop.GENDER.trim() : ""
35                  println "Gender:" + gender
36                  is_marry = prop.IS_MARRY? prop.IS_MARRY : false
37                  println "is_marry:" + is_marry
38                  is_smoke = prop.SMOKE? prop.SMOKE : false
39                  println "is_smoke:" + is_smoke
40                  full_test = prop.FULL_TEST? prop.FULL_TEST : false
41              }
42          }
43      }
44      stage("call a method") {
45          steps {
46              script {
47                  println "send the parameter as map type"
48                  model_call = load env.WORKSPACE + "/groovy/projectA-model.groovy"
49                  model_call.getUserInfo(name:name, age:age, phone:phone, address:address, email:email, gender:gender, is_marry:is_marry)
50              }
51          }
52      }
53      stage("check serive up") {
54          when {
55              expression {
56                  return (is_smoke == true || full_test == true)
57              }
58          }
59          steps {
60              script {
61                  println "SMOKE TEST: check service startup"
62              }
63          }
64      }
65        stage("check UI login") {
66          when {
67              expression {
68                  return (is_smoke == true || full_test == true)
69              }
70          }
71          steps {
72              script {
73                  println "SMOKE TEST: check UI login success"
74              }
75          }
76      }
77     
78      stage("Integrate-ModelA") {
79          when {
80              expression {
81                  return (is_smoke == false || full_test == true)
82              }
83          }
84          steps {
85              script {
86                  println "Integrate-ModelA"
87              }
88          }
89      }
90     
91      stage("Integrate-ModelB") {
92          when {
93              expression {
94                  return (is_smoke == false || full_test == true)
95              }
96          }
97          steps {
98              script {
99                  println "Integrate-ModelB"
100             }
101         }
102     }
103 }
104
105}
106
107
108

测试结果

给TA打赏
共{{data.count}}人
人已打赏
安全经验

职场中的那些话那些事

2021-9-24 20:41:29

安全经验

elk+redis 搭建nginx日志分析平台

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索