Jenkins⾼级篇之Pipeline技巧篇-4-根据参数传⼊条件控制执⾏
不同stage
这篇我来介绍⼀下之前,很早的时候介绍pipeline语法的时候,有⼀个指令叫when 和expression,当时由于pipeline知识学习太少,不好举例⼦去学习消化。到了这⾥,其实这两个关键字就是⽤来控制stage的执⾏,如果你条件有好⼏个,可以精确控制让哪⼀些stage执⾏,让哪⼀些stage不执⾏。
我这⾥举例⼀个⾃动化测试中的例⼦,例如我写了多个stage,这个pipeline脚本执⾏执⾏冒烟测试,和集成测试。有时候,我们希望快速执⾏冒烟测试,想根据结果看看,不⼀定⼀上来就执⾏集成测试。为了达到这种控制效果,我们就需要使⽤逻辑控制。在pipeline中就使⽤when 和expression两个命令。例如,如果json⽂件中冒烟测试变量为true,我就只执⾏冒烟测试的stage,其他和冒烟测试⽆关的stage我就不去执⾏。如果冒烟测试变量值为fal,也就是默认要跑集成测试(不跑冒烟测试)。为了不搞得这么复杂,我们就分两个类型测试就好。下⾯,我⽤pipeline代码⽅式去实现。
1.json准备
大学英语六级词汇{
"NAME" : "Lucy",
"AGE" : "18",
"PHONE_NUMBER" : "139********",
"ADDRESS" : "Haidian Beijing",
"EMAIL" : "",
"GENDER" : "male",
"SMOKE": true,
behavior是什么意思"IS_MARRY" : fal
}
我还是在前⾯⽂章的/tmp/anthony/test.json⽂件基础上加了⼀个变量 SMOKE, 默认值是true。
2.pipeline stage groovy⽂件代码
由于我添加了⼀个SMOKE的变量,所以在初始化stage,我们新增⼀⾏代码,初始化SMOKE的变量。
del.*;
pipeline{
agent any
stages{
昆明在线
stage("Hello Pipeline") {
steps {
script {
println "Hello Pipeline!"
println env.JOB_NAME
println env.BUILD_NUMBER
外语学习网
}
}
}
stage("Init paramters in json") {
steps {
script {
println "read josn input file"
json_file = INPUT_JSON? im() : ""
prop = readJSON file : json_file
name = prop.NAME? im() : ""
println "Name:" + name
age = prop.AGE? im() : ""
println "Age:" + age
phone = prop.PHONE_NUMBER? prop.im() : ""
println "Phone:" + phone
println "Phone:" + phone
address = prop.ADDRESS? im() : ""
println "Address:" + address
email = prop.EMAIL? im() : ""
println "Email:" + email
gender = prop.GENDER? im() : ""
println "Gender:" + gender
is_marry = prop.IS_MARRY? prop.IS_MARRY : fal
rather的用法
println "is_marry:" + is_marry
is_smoke = prop.SMOKE? prop.SMOKE : fal
println "is_smoke:" + is_smoke
}
}
}
stage("call a method") {
steps {
script {
println "nd the parameter as map type"
model_call = load env.WORKSPACE + "/vy"
UrInfo(name:name, age:age, phone:phone, address:address, email:email, gender:gender, is_marry:is_marry) }
}
}
stage("check rive up") {
when {
expression {
return (is_smoke == true)
}
}
steps {
script {
println "SMOKE TEST: check rvice startup"
}
}
}
stage("check UI login") {
when {
expression {
return (is_smoke == true)
}
}
steps {
script {
println "SMOKE TEST: check UI login success"
}
}
}
stage("Integrate-ModelA") {
when {
expression {
return (is_smoke == fal)
}
}
steps {
script {
println "Integrate-ModelA"
}
}
}
stage("Integrate-ModelB") {
when {
expression {
return (is_smoke == fal)
}
}
}
steps {
script {
println "Integrate-ModelB"
}
}
}
}
}
3.测试效果
只跑冒烟测试,也就是SMOKE的值在json中是true
我这⾥直接贴出运⾏⽇志
Started by ur root
Rebuilds build #9
Rebuilds build #15
Rebuilds build #16
Rebuilds build #17
Obtained src/vy from /Anthonyliu86/pipeline-skills-demo.git Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/ProjectA-pipeline-demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential 03214975-2168-4795-981a-ddd935f62a76
> git rev-par --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git igin./Anthonyliu86/pipeline-skills-demo.git # timeout=10
Fetching upstream changes /Anthonyliu86/pipeline-skills-demo.git
> git --version # timeout=10
using GIT_ASKPASS to t credentials
> git fetch --tags --/Anthonyliu86/pipeline-skills-demo.git +refs/heads/*:refs/remotes/origin/* > git rev-par refs/remotes/origin/master^{commit} # timeout=10
> git rev-par refs/remotes/origin/origin/master^{commit} # timeout=10
中介出国留学Checking out Revision 35f0fbe78b205b5e0cdbf94444722411a7ebdb62 (refs/remotes/origin/master)
> git config core.sparcheckout # timeout=10
> git checkout -f 35f0fbe78b205b5e0cdbf94444722411a7ebdb62
Commit message: "fix"
> git rev-list --no-walk ff9cbf42c55cb87c863bb6b96d511ccc496eff80 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
ProjectA-pipeline-demo
[Pipeline] echo
18
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
catalogue[Pipeline] stage
[Pipeline] { (Init paramters in json)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
read josn input file
[Pipeline] readJSON
[Pipeline] echo
Name:Lucy
[Pipeline] echo
Age:18
[Pipeline] echo
Phone:139********
[Pipeline] echo
gagmanAddress:Haidian Beijing
[Pipeline] echo
Email:
[Pipeline] echo
Gender:male
jimmy eat world
[Pipeline] echo
is_marry:fal
[Pipeline] echo
is_smoke:true
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (call a method)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
nd the parameter as map type
[Pipeline] load
[Pipeline] { (/var/lib/jenkins/workspace/ProjectA-pipeline-demo/vy) [Pipeline] }
[Pipeline] // load
[Pipeline] echo
Lucy come from Haidian Beijing, he is 18 old. he's phone number is
139********, or you can contact he via , he is not marry yet.
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (check rive up)
[Pipeline] script
[Pipeline] {
烟台英语翻译
[Pipeline] echo
SMOKE TEST: check rvice startup
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (check UI login)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
SMOKE TEST: check UI login success
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integrate-ModelA)
Stage "Integrate-ModelA" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integrate-ModelB)
Stage "Integrate-ModelB" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
只跑集成测试,先去test.json把SMOKE的值改成fal
Started by ur root
Rebuilds build #9
Rebuilds build #15
Rebuilds build #16
Rebuilds build #17
Obtained src/vy from /Anthonyliu86/pipeline-skills-demo.git Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/ProjectA-pipeline-demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
using credential 03214975-2168-4795-981a-ddd935f62a76
> git rev-par --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
> git igin./Anthonyliu86/pipeline-skills-demo.git # timeout=10
Fetching upstream changes /Anthonyliu86/pipeline-skills-demo.git
> git --version # timeout=10
using GIT_ASKPASS to t credentials
> git fetch --tags --/Anthonyliu86/pipeline-skills-demo.git +refs/heads/*:refs/remotes/origin/* > git rev-par refs/remotes/origin/master^{commit} # timeout=10
> git rev-par refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 35f0fbe78b205b5e0cdbf94444722411a7ebdb62 (refs/remotes/origin/master)
> git config core.sparcheckout # timeout=10
> git checkout -f 35f0fbe78b205b5e0cdbf94444722411a7ebdb62
Commit message: "fix"
> git rev-list --no-walk ff9cbf42c55cb87c863bb6b96d511ccc496eff80 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
ProjectA-pipeline-demo
[Pipeline] echo