本文共 12303 字,大约阅读时间需要 41 分钟。
这篇我来介绍一下之前,很早的时候介绍pipeline语法的时候,有一个指令叫when 和expression,当时由于pipeline知识学习太少,不好举例子去学习消化。到了这里,其实这两个关键字就是用来控制stage的执行,如果你条件有好几个,可以精确控制让哪一些stage执行,让哪一些stage不执行。
我这里举例一个自动化测试中的例子,例如我写了多个stage,这个pipeline脚本执行执行冒烟测试,和集成测试。有时候,我们希望快速执行冒烟测试,想根据结果看看,不一定一上来就执行集成测试。为了达到这种控制效果,我们就需要使用逻辑控制。在pipeline中就使用when 和expression两个命令。例如,如果json文件中冒烟测试变量为true,我就只执行冒烟测试的stage,其他和冒烟测试无关的stage我就不去执行。如果冒烟测试变量值为false,也就是默认要跑集成测试(不跑冒烟测试)。为了不搞得这么复杂,我们就分两个类型测试就好。下面,我用pipeline代码方式去实现。
1.json准备
{ "NAME" : "Lucy", "AGE" : "18", "PHONE_NUMBER" : "13912345678", "ADDRESS" : "Haidian Beijing", "EMAIL" : "lucy@demo.com", "GENDER" : "male", "SMOKE": true, "IS_MARRY" : false}
我还是在前面文章的/tmp/anthony/test.json文件基础上加了一个变量 SMOKE, 默认值是true。
2.pipeline stage groovy文件代码
由于我添加了一个SMOKE的变量,所以在初始化stage,我们新增一行代码,初始化SMOKE的变量。
import hudson.model.*;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? INPUT_JSON.trim() : "" prop = readJSON file : json_file name = prop.NAME? prop.NAME.trim() : "" println "Name:" + name age = prop.AGE? prop.AGE.trim() : "" println "Age:" + age phone = prop.PHONE_NUMBER? prop.PHONE_NUMBER.trim() : "" println "Phone:" + phone address = prop.ADDRESS? prop.ADDRESS.trim() : "" println "Address:" + address email = prop.EMAIL? prop.EMAIL.trim() : "" println "Email:" + email gender = prop.GENDER? prop.GENDER.trim() : "" println "Gender:" + gender is_marry = prop.IS_MARRY? prop.IS_MARRY : false println "is_marry:" + is_marry is_smoke = prop.SMOKE? prop.SMOKE : false println "is_smoke:" + is_smoke } } } stage("call a method") { steps { script { println "send the parameter as map type" model_call = load env.WORKSPACE + "/groovy/projectA-model.groovy" model_call.getUserInfo(name:name, age:age, phone:phone, address:address, email:email, gender:gender, is_marry:is_marry) } } } stage("check serive up") { when { expression { return (is_smoke == true) } } steps { script { println "SMOKE TEST: check service 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 == false) } } steps { script { println "Integrate-ModelA" } } } stage("Integrate-ModelB") { when { expression { return (is_smoke == false) } } steps { script { println "Integrate-ModelB" } } } }}
3.测试效果
只跑冒烟测试,也就是SMOKE的值在json中是true
我这里直接贴出运行日志
Started by user rootRebuilds build #9Rebuilds build #15Rebuilds build #16Rebuilds build #17Obtained src/Jenkinsfile/projectA-stages.groovy from git https://github.com/Anthonyliu86/pipeline-skills-demo.gitRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /var/lib/jenkins/workspace/ProjectA-pipeline-demo[Pipeline] {[Pipeline] stage[Pipeline] { (Declarative: Checkout SCM)[Pipeline] checkoutusing credential 03214975-2168-4795-981a-ddd935f62a76 > git rev-parse --is-inside-work-tree # timeout=10Fetching changes from the remote Git repository > git config remote.origin.url https://github.com/Anthonyliu86/pipeline-skills-demo.git # timeout=10Fetching upstream changes from https://github.com/Anthonyliu86/pipeline-skills-demo.git > git --version # timeout=10using GIT_ASKPASS to set credentials > git fetch --tags --progress https://github.com/Anthonyliu86/pipeline-skills-demo.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10Checking out Revision 35f0fbe78b205b5e0cdbf94444722411a7ebdb62 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 35f0fbe78b205b5e0cdbf94444722411a7ebdb62Commit 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] echoHello Pipeline![Pipeline] echoProjectA-pipeline-demo[Pipeline] echo18[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Init paramters in json)[Pipeline] script[Pipeline] {[Pipeline] echoread josn input file[Pipeline] readJSON[Pipeline] echoName:Lucy[Pipeline] echoAge:18[Pipeline] echoPhone:13912345678[Pipeline] echoAddress:Haidian Beijing[Pipeline] echoEmail:lucy@demo.com[Pipeline] echoGender:male[Pipeline] echois_marry:false[Pipeline] echois_smoke:true[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (call a method)[Pipeline] script[Pipeline] {[Pipeline] echosend the parameter as map type[Pipeline] load[Pipeline] { (/var/lib/jenkins/workspace/ProjectA-pipeline-demo/groovy/projectA-model.groovy)[Pipeline] }[Pipeline] // load[Pipeline] echo Lucy come from Haidian Beijing, he is 18 old. he's phone number is 13912345678, or you can contact he via lucy@demo.com, he is not marry yet. [Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (check serive up)[Pipeline] script[Pipeline] {[Pipeline] echoSMOKE TEST: check service startup[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (check UI login)[Pipeline] script[Pipeline] {[Pipeline] echoSMOKE 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 PipelineFinished: SUCCESS
只跑集成测试,先去test.json把SMOKE的值改成false
Started by user rootRebuilds build #9Rebuilds build #15Rebuilds build #16Rebuilds build #17Obtained src/Jenkinsfile/projectA-stages.groovy from git https://github.com/Anthonyliu86/pipeline-skills-demo.gitRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] nodeRunning on Jenkins in /var/lib/jenkins/workspace/ProjectA-pipeline-demo[Pipeline] {[Pipeline] stage[Pipeline] { (Declarative: Checkout SCM)[Pipeline] checkoutusing credential 03214975-2168-4795-981a-ddd935f62a76 > git rev-parse --is-inside-work-tree # timeout=10Fetching changes from the remote Git repository > git config remote.origin.url https://github.com/Anthonyliu86/pipeline-skills-demo.git # timeout=10Fetching upstream changes from https://github.com/Anthonyliu86/pipeline-skills-demo.git > git --version # timeout=10using GIT_ASKPASS to set credentials > git fetch --tags --progress https://github.com/Anthonyliu86/pipeline-skills-demo.git +refs/heads/*:refs/remotes/origin/* > git rev-parse refs/remotes/origin/master^{commit} # timeout=10 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10Checking out Revision 35f0fbe78b205b5e0cdbf94444722411a7ebdb62 (refs/remotes/origin/master) > git config core.sparsecheckout # timeout=10 > git checkout -f 35f0fbe78b205b5e0cdbf94444722411a7ebdb62Commit 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] echoHello Pipeline![Pipeline] echoProjectA-pipeline-demo[Pipeline] echo18[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (Init paramters in json)[Pipeline] script[Pipeline] {[Pipeline] echoread josn input file[Pipeline] readJSON[Pipeline] echoName:Lucy[Pipeline] echoAge:18[Pipeline] echoPhone:13912345678[Pipeline] echoAddress:Haidian Beijing[Pipeline] echoEmail:lucy@demo.com[Pipeline] echoGender:male[Pipeline] echois_marry:false[Pipeline] echois_smoke:true[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (call a method)[Pipeline] script[Pipeline] {[Pipeline] echosend the parameter as map type[Pipeline] load[Pipeline] { (/var/lib/jenkins/workspace/ProjectA-pipeline-demo/groovy/projectA-model.groovy)[Pipeline] }[Pipeline] // load[Pipeline] echo Lucy come from Haidian Beijing, he is 18 old. he's phone number is 13912345678, or you can contact he via lucy@demo.com, he is not marry yet. [Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (check serive up)[Pipeline] script[Pipeline] {[Pipeline] echoSMOKE TEST: check service startup[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (check UI login)[Pipeline] script[Pipeline] {[Pipeline] echoSMOKE 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 PipelineFinished: SUCCESS
再看看stage view的图片效果
其实,如果你还可以利用第二个参数,写在expression中,来控制当什么条件符合的时候,执行全部的冒烟加上集成测试,这样的效果都是可以实现的。
json文件如下
{ "NAME" : "Lucy", "AGE" : "18", "PHONE_NUMBER" : "13912345678", "ADDRESS" : "Haidian Beijing", "EMAIL" : "lucy@demo.com", "GENDER" : "male", "SMOKE": false, "FULL_TEST":true, "IS_MARRY" : false}
stage 代码修改如下
import hudson.model.*;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? INPUT_JSON.trim() : "" prop = readJSON file : json_file name = prop.NAME? prop.NAME.trim() : "" println "Name:" + name age = prop.AGE? prop.AGE.trim() : "" println "Age:" + age phone = prop.PHONE_NUMBER? prop.PHONE_NUMBER.trim() : "" println "Phone:" + phone address = prop.ADDRESS? prop.ADDRESS.trim() : "" println "Address:" + address email = prop.EMAIL? prop.EMAIL.trim() : "" println "Email:" + email gender = prop.GENDER? prop.GENDER.trim() : "" println "Gender:" + gender is_marry = prop.IS_MARRY? prop.IS_MARRY : false println "is_marry:" + is_marry is_smoke = prop.SMOKE? prop.SMOKE : false println "is_smoke:" + is_smoke full_test = prop.FULL_TEST? prop.FULL_TEST : false } } } stage("call a method") { steps { script { println "send the parameter as map type" model_call = load env.WORKSPACE + "/groovy/projectA-model.groovy" model_call.getUserInfo(name:name, age:age, phone:phone, address:address, email:email, gender:gender, is_marry:is_marry) } } } stage("check serive up") { when { expression { return (is_smoke == true || full_test == true) } } steps { script { println "SMOKE TEST: check service startup" } } } stage("check UI login") { when { expression { return (is_smoke == true || full_test == true) } } steps { script { println "SMOKE TEST: check UI login success" } } } stage("Integrate-ModelA") { when { expression { return (is_smoke == false || full_test == true) } } steps { script { println "Integrate-ModelA" } } } stage("Integrate-ModelB") { when { expression { return (is_smoke == false || full_test == true) } } steps { script { println "Integrate-ModelB" } } } }}
测试结果
转载地址:http://qkows.baihongyu.com/