DEVOPS技术实践_22:根据参数传入条件控制执行不同stage
前面学习了参数的传递和调用,下面研究一下根据参数作为条件执行不同的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的更多相关文章
- DEVOPS技术实践_23:判断文件下载成功作为执行条件
在实际生产中,我们经常会需要通过判断一个结果作为一个条件去执行另一个内容,比如判断一个文件是否存在,判官一个命令是否执行成功等等 现在我们选择其中一个场景进行实验,当某个目录下存在,则执行操作 1. ...
- 第二部分 条件控制执行语句、循环语句、switch语句、跳转语句和其它语句
条件控制执行语句: if语句 if....else....语句 循环语句: while语句 do....while语句 for语句 switch语句: 跳转语句: break; continue; r ...
- DEVOPS技术实践_19:Pipeline的多参数json调用
在上一篇学习了把参数写进Json文件,然后通过去Json文件,调用参数的方法 1. 三元运算符介绍 调用的方法是通过一个三元运算符实现的 gender = prop.GENDER? prop.GEND ...
- DEVOPS技术实践_18:Jenkins的Pinpeline对于参数的使用
因为最近使用Pipeline声明式语法构建项目,但是最近项目的参数设置较多,特地的来学习一下关于参数的调用和测试,主要式从一个大神那里学习的,结尾回贴上大神的博客链接 1 构建一个pipeline项目 ...
- DEVOPS技术实践_11:Jenkins集成Sonar
前言 前面已经有介绍sonar的安装,简单应用,下面在简答的研究一下sonar和jenkins集成的简单使用,对于sonar的安装不做介绍 一 sonar的简单介绍 持续检查避免了低质量的代码,比如S ...
- DEVOPS技术实践_06:sonar与Jenksin集成
代码质量管理平台 一.checkout和打包功能 1.1 gitlab在新建一个文件 后续在写入内容 1.2 Jenkins新建一个任务 两个参数 1.3 流水线配置 copy仓库地址: http:/ ...
- DEVOPS技术实践_04:Jenkins参数化构建
一.参数化构建 1.1 各个参数的信息 凭据参数存储一个用户的账号密码信息,等等,运用最多的是选项参数 1.2 使用选项参数 构建已经变成参数化构建 1.3 获取这个值,修改Jenkinsfile文件 ...
- DEVOPS技术实践_21:Pipeline的嵌套以及流程控制的if和case语句
1 if控制语句 使用一个简单的If控制语句 pipeline { agent any stages { stage('flow control') { steps { script { == ) { ...
- DEVOPS技术实践_20:串联多个job执行
在jenkins可能会有战役中场景,就是在一个job执行完之后,把这个执行结果作为另一个job的执行条件 比如A执行完,如果A执行成功,则执行B,如果失败则执行C 1 前期准备 A任务 import ...
随机推荐
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛: B. Coin 【概率题】【数论】
Bob has a not even coin(就是一个不均匀的硬币,朝上的概率不一定是1/2), every time he tosses the coin, the probability tha ...
- 模板—扩展GCD*2
有必要重新学一下扩展GCD emmmm. 主要是扩展GCD求解线性同余方程$ax≡b (mod p)$. 1.方程有解的充分必要条件:b%gcd(a,p)=0. 证明: $ax-py=b$ 由于求解整 ...
- laravel 队列重启
我在job中写了邮件发送 ,线下环境测试是无问题的 ,现在放到线上出现了问题. 问题描述: 部分时候邮件功能可用,部分时间邮件功能不可用. 邮件功能不可用的时候,job发送失败,失败原因是无发送人,打 ...
- Oracle包utl_inaddr
作用:用于取得局域网或Internet环境中的主机名和IP地址. 1.utl_inaddr.get_host_address 环境中IP地址 如果查询失败,则提示系统错误 查询www.qq.com的I ...
- oracle函数 mod(x,y)
[功能]返回x除以y的余数 [参数]x,y,数字型表达式 [返回]数字 [示例] select mod(23,8),mod(24,8) from dual; 返回:7,0
- @bzoj - 4922@ [Lydsy1706月赛]Karp-de-Chant Number
目录 @description@ @solution@ @accepted code@ @details@ @description@ 卡常数被称为计算机算法竞赛之中最神奇的一类数字,主要特点集中于令 ...
- LA 4119 Always an integer (数论+模拟)
ACM-ICPC Live Archive 一道模拟题,题意是问一个给出的多项式代入正整数得到的值是否总是整数. 这题是一道数论题,其实对于这个式子,我们只要计算1~最高次项是否都满足即可. 做的时候 ...
- 添加SuperSocket的启动代码到 Windows Azure 的 WorkRole 项目
与其它SuperSocket程序相同,启动代码同样也要写到程序的入口处,如 Windows Azure 的 WorkRole 项目的OnStart() 方法: public override bool ...
- vue-router在新窗口打开页面
1. <router-link>标签实现新窗口打开: <router-link target="_blank" :to="{path:'/app/dat ...
- Python--day61--ORM介绍及Django使用ORM创建表
ORM: 使用django的ORM详细步骤:(pymysql操作数据库:) #1,自己动手创建数据库(create database 数据库名字;) #2,在Django项目中(setting.py文 ...