前面学习了参数的传递和调用,下面研究一下根据参数作为条件执行不同的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. 可变形参 Day07

    package com.sxt.kebianxingcan; /* * 可变形参 * 声明:数据类型...标识符 * 作用:将实参作为数组处理 * 规则:一个方法只能有一个可变形参并且作为最后一个形参 ...

  2. vue事件获取事件对象,vue获取事件源,vue event.currentTarget

    js的事件,如点击事件,可以直接用this获取事件对象,而jQuery可以使用$(this)来获取事件对象.vue必须借助事件的 event 对象 的 currentTarget 才能获取事件对象 v ...

  3. 阿里云MaxCompute 2019-4月刊

    摘要: 4月新功能发布,精彩技术好文推荐,5月线上线下活动抢先知道,尽在4月刊. 您好,MaxCompute 2019.4月刊为您带来产品最新动态和丰富的产品技术内容,欢迎阅读. 导读 [功能发布]4 ...

  4. @bzoj - 4381@ [POI2015] Odwiedziny

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵 n 个点的树,树上每条边的长度都为 1 ,第 i 个点 ...

  5. 15个非常重要的Apache开源项目汇总

    15个非常重要的Apache开源项目汇总 自1999年创立以来,Apache软件基金会如今已成了众多重要的开源软件项目之家.本文列举了15个多年来非常重要的Apache项目,这些项目不仅对开源运动来说 ...

  6. [Pytorch]基于混和精度的模型加速

    这篇博客是在pytorch中基于apex使用混合精度加速的一个偏工程的描述,原理层面的解释并不是这篇博客的目的,不过在参考部分提供了非常有价值的资料,可以进一步研究. 一个关键原则:“仅仅在权重更新的 ...

  7. Google Colab——用谷歌免费GPU跑你的深度学习代码

    Google Colab简介 Google Colaboratory是谷歌开放的一款研究工具,主要用于机器学习的开发和研究.这款工具现在可以免费使用,但是不是永久免费暂时还不确定.Google Col ...

  8. springboot 2.1.6.RELEASE pom 第一行报错

    eclipse创建springboot 2.1.6.RELEASE  pom第一行报错 在pom.xml 文件的properties中加入maven jar插件的版本号 <maven-jar-p ...

  9. 4-1 自动生成spider模板的命令

    scrapy genspider 爬虫名 爬取得网站url例:scrapy genspider jobble2 blog.jobbole.com

  10. Java语言中使用OpenMP

    从去年年中,开始学习Java,主要是维护公司用Java编写的服务器软件.目前,该服务器软件遇到一个问题,在下载大文件时,如果同时下载的用户很多, 服务器软件工作会出现异常,有的用户无法下载.服务器硬件 ...