这篇我来介绍下,pipeline代码中如何把两个JOB(A 和B)给串接起来。我们知道,在图形界面的jenkins中的自由风格的job的设置页眉,我们可以选择,执行完job A之后触发执行Job B, 那么pipeline又是如何实现这一过程呢。
环境准备
当前我的JobA 就是前面几篇文章一直介绍的那个ProjectA-pipeline-demo,为了演示这个过程,我基于Job A给复制了一个Job B, 名称为ProjectB-pipeline-demo.
从上面我们看到,Job B我们没有任何构建历史记录,待会我们通过JobA的pipeline代码去调用触发执行Job B, 当然,这里由于是clone JobA, 所以Job B里面执行的git代码和Job A是一样。实际项目肯定不一样的,代码分支可能一样,但是参数提供肯定不一样,不然Job B没有存在意义。
调用下一个job的pipeline代码(post块里build方法)
这里,我在前面一篇文章基础上,在post块下添加调用Job B的代码。这里提一下,post字面意思是事后的意思,也就是做完JobA里面不同stage,这个事情之后,所有的操作都写在post块里。这里,我们写在当Job A构建结果为success的时候触发构建Job B.
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 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 post {
106 always {
107 script {
108 println "Do some actins when always need."
109 }
110 }
111 failure {
112 script {
113 println "Do some actins when build failed."
114 }
115 }
116 success {
117 script {
118 println "Here we kickoff run job B"
119 jobB = build job: 'ProjectB-pipeline-demo', propagate: false, wait: true, parameters: [
120 string(name:'INPUT_JSON', value: "${json_file}")
121 ]
122 println jobB.getResult()
123 }
124 }
125 }
126
127
128}
129
130
131
测试一下
下面这个stage view是JobA的执行结果
我们主要下Post Actions这个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 1Integrate-ModelB
2[Pipeline] }
3[Pipeline] // script
4[Pipeline] }
5[Pipeline] // stage
6[Pipeline] stage
7[Pipeline] { (Declarative: Post Actions)
8[Pipeline] script
9[Pipeline] {
10[Pipeline] echo
11Do some actins when always need.
12[Pipeline] }
13[Pipeline] // script
14[Pipeline] script
15[Pipeline] {
16[Pipeline] echo
17Here we kickoff run job B
18[Pipeline] build (Building ProjectB-pipeline-demo)
19Scheduling project: ProjectB-pipeline-demo
20Starting building: ProjectB-pipeline-demo #1
21[Pipeline] echo
22ABORTED
23[Pipeline] }
24[Pipeline] // script
25[Pipeline] }
26[Pipeline] // stage
27[Pipeline] }
28[Pipeline] // withEnv
29[Pipeline] }
30[Pipeline] // node
31[Pipeline] End of Pipeline
32Finished: SUCCESS
33
你在jenkins的日志控制台点击ProjectB-pipeline-demo #1, 确实可以跳转到Job B的构建日志了, 这里由于我两个job都使用了同一套pipeline代码,所以会出现一直在success里触发Job B, 会死循环,所以我手动给ABORTED了。
关于pipeline中如何串联上下游job就介绍到这里,注意build 这个函数的调用以及里面参数是map成对,特别不要漏冒号和变量parameters中中括号等等。