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

更新时间:2023-06-22 21:51:59 阅读: 评论:0

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,
"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
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
[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
Address:Haidian Beijing
[Pipeline] echo
海鲜带子Email:
[Pipeline] echo
Gender:male
[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

本文发布于:2023-06-22 21:51:59,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1050396.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:测试   冒烟   控制   集成   代码   学习
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图