前面学习了参数的传递和调用,下面研究一下根据参数作为条件执行不同的stage

使用叫when 和expression控制某一个stage的运行,

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

1 修改json文件

{
"NAME":"Lucy",
"AGE":"",
"PHONE_NUMBER":"",
"ADDRESS":"Haidian Beijing",
"EMAIL":"lucy@demo.com",
"GENDER":"male",
"SMOKE":"false",
"IS_MARRY":"false"
}

test.json文件基础上加了一个变量 SMOKE, 默认值是false。

2 使用的jenkinsfile文件

 import hudson.model.*;
pipeline{
agent any
environment {
INPUT_JSON = "/tmp/test.json"
}
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.trim() : 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.JENKINS_HOME + "/moodlegroovy/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 执行打印输出

控制台输出

Started by user darren ning
Obtained pipeline.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=
Fetching changes from the remote Git repository
> git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
> git --version # timeout=
> git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
Checking out Revision 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=
> git checkout -f 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0
Commit message: "Update pipeline.jenkinsfile"
> git rev-list --no-walk ad52c819b4e74df5566cc767b8d580ccea363470 # timeout=
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo [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:
[Pipeline] echo
Phone:
[Pipeline] echo
Address:Haidian Beijing
[Pipeline] echo
Email:lucy@demo.com
[Pipeline] echo
Gender:male
[Pipeline] echo
is_marry:false
[Pipeline] echo
is_smoke:false
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (call a method)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
send the parameter as map type
[Pipeline] load
[Pipeline] { (/root/.jenkins/moodlegroovy/model.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] echo Lucy come from Haidian Beijing, he is old. he's phone number is
, 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)
Stage "check serive up" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (check UI login)
Stage "check UI login" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integrate-ModelA)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Integrate-ModelA
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Integrate-ModelB)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Integrate-ModelB
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

4 修改json文件,把smoke的值改为true

{
"NAME":"Lucy",
"AGE":"",
"PHONE_NUMBER":"",
"ADDRESS":"Haidian Beijing",
"EMAIL":"lucy@demo.com",
"GENDER":"male",
"SMOKE":"false",
"IS_MARRY":"false"
}

5 点击构建测试

在最后的的两个false没有输出,true输出

控制台输出

Started by user darren ning
Obtained pipeline.jenkinsfile from git http://192.168.132.132/root/pipeline-parameter-test.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /root/.jenkins/workspace/pipeline_parameter_project
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
> git rev-parse --is-inside-work-tree # timeout=
Fetching changes from the remote Git repository
> git config remote.origin.url http://192.168.132.132/root/pipeline-parameter-test.git # timeout=10
Fetching upstream changes from http://192.168.132.132/root/pipeline-parameter-test.git
> git --version # timeout=
> git fetch --tags --progress http://192.168.132.132/root/pipeline-parameter-test.git +refs/heads/*:refs/remotes/origin/*
> git rev-parse refs/remotes/origin/master^{commit} # timeout=
> git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=
Checking out Revision 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=
> git checkout -f 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0
Commit message: "Update pipeline.jenkinsfile"
> git rev-list --no-walk 6133dc99fd9f35fb7ccb6a3effb45cf1e96e76b0 # timeout=
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello Pipeline)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
Hello Pipeline!
[Pipeline] echo
pipeline_parameter_project
[Pipeline] echo [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:
[Pipeline] echo
Phone:
[Pipeline] echo
Address:Haidian Beijing
[Pipeline] echo
Email:lucy@demo.com
[Pipeline] echo
Gender:male
[Pipeline] echo
is_marry:false
[Pipeline] echo
is_smoke:true
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (call a method)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
send the parameter as map type
[Pipeline] load
[Pipeline] { (/root/.jenkins/moodlegroovy/model.groovy)
[Pipeline] }
[Pipeline] // load
[Pipeline] echo Lucy come from Haidian Beijing, he is old. he's phone number is
, 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] echo
SMOKE TEST: check service 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] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

实验完成


参考文献:https://blog.csdn.net/u011541946/article/details/89322510

DEVOPS技术实践_22:根据参数传入条件控制执行不同stage的更多相关文章

  1. DEVOPS技术实践_23:判断文件下载成功作为执行条件

    在实际生产中,我们经常会需要通过判断一个结果作为一个条件去执行另一个内容,比如判断一个文件是否存在,判官一个命令是否执行成功等等 现在我们选择其中一个场景进行实验,当某个目录下存在,则执行操作 1. ...

  2. 第二部分 条件控制执行语句、循环语句、switch语句、跳转语句和其它语句

    条件控制执行语句: if语句 if....else....语句 循环语句: while语句 do....while语句 for语句 switch语句: 跳转语句: break; continue; r ...

  3. DEVOPS技术实践_19:Pipeline的多参数json调用

    在上一篇学习了把参数写进Json文件,然后通过去Json文件,调用参数的方法 1. 三元运算符介绍 调用的方法是通过一个三元运算符实现的 gender = prop.GENDER? prop.GEND ...

  4. DEVOPS技术实践_18:Jenkins的Pinpeline对于参数的使用

    因为最近使用Pipeline声明式语法构建项目,但是最近项目的参数设置较多,特地的来学习一下关于参数的调用和测试,主要式从一个大神那里学习的,结尾回贴上大神的博客链接 1 构建一个pipeline项目 ...

  5. DEVOPS技术实践_11:Jenkins集成Sonar

    前言 前面已经有介绍sonar的安装,简单应用,下面在简答的研究一下sonar和jenkins集成的简单使用,对于sonar的安装不做介绍 一 sonar的简单介绍 持续检查避免了低质量的代码,比如S ...

  6. DEVOPS技术实践_06:sonar与Jenksin集成

    代码质量管理平台 一.checkout和打包功能 1.1 gitlab在新建一个文件 后续在写入内容 1.2 Jenkins新建一个任务 两个参数 1.3 流水线配置 copy仓库地址: http:/ ...

  7. DEVOPS技术实践_04:Jenkins参数化构建

    一.参数化构建 1.1 各个参数的信息 凭据参数存储一个用户的账号密码信息,等等,运用最多的是选项参数 1.2 使用选项参数 构建已经变成参数化构建 1.3 获取这个值,修改Jenkinsfile文件 ...

  8. DEVOPS技术实践_21:Pipeline的嵌套以及流程控制的if和case语句

    1 if控制语句 使用一个简单的If控制语句 pipeline { agent any stages { stage('flow control') { steps { script { == ) { ...

  9. DEVOPS技术实践_20:串联多个job执行

    在jenkins可能会有战役中场景,就是在一个job执行完之后,把这个执行结果作为另一个job的执行条件 比如A执行完,如果A执行成功,则执行B,如果失败则执行C 1 前期准备 A任务 import ...

随机推荐

  1. cocos2d-x 特效集合

    本文转载自:http://www.cnblogs.com/linux-ios/archive/2013/04/09/3009292.html bool HelloWorld::init() { /// ...

  2. 03搭建docker私有仓库

    搭建docker私仓,可以使用docker官方提供的registry镜像.该镜像目前有2.0,2.3和2.3.1版本.它只与1.6.0以上版本的docker兼容.搭建私仓的步骤如下: 一:无代理.无认 ...

  3. @atcoder - AGC034E@ Complete Compress

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 N 个点的树,编号为 1, 2, ..., N.第 i ...

  4. 使用vux组件库常见报错($t)处理

    错误一: [Vue warn]: Property or method "$t" is not defined on the instance but referenced dur ...

  5. saltStack_Pillar

    Pillar是Salt非常重要的一个组件,它用于给特定的minion定义任何你需要的数据,这些数据可以被Salt的其他组件使用.这里可以看出Pillar的一个特点,Pillar数据是与特定minion ...

  6. php统计近一周和近30天的用户数据

    https://blog.csdn.net/shenpengchao/article/details/59073589 先上一张效果图 这边用的是echarts插件http://echarts.bai ...

  7. 带你进入 Activiti 工作流的世界

    Activiti 是一个针对企业用户.开发人员 .系统管理员的轻量级工作流业务管理平台,其核心是使用 java 开发的快速 . 稳定的 BPMN2.0 流程引擎 .它可以与 spring 完美集成. ...

  8. Mysql错误:#1054 - Unknown column 'id' in 'field list' 解决办法

    第一次用mysql,在插入数据时,竟然报这样的错误, #1054 - Unknown column 'id' in 'field list'

  9. hdu 1026 Ignatius and the Princess I(bfs)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  10. 从规则引擎到复杂事件处理(CEP)

    Drools Fusion既是规则引擎,又可以作为CEP.除了事件定义和时间推理之外,对于引擎本身也会有一些不同的使用.主要体现在会话时钟.流模式.滑动窗口和对事件的内存管理. 会话时钟 由于事件的时 ...