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

这篇我来介绍一下之前,很早的时候介绍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 root
Rebuilds build #9
Rebuilds build #15
Rebuilds build #16
Rebuilds build #17
Obtained src/Jenkinsfile/projectA-stages.groovy from git https://github.com/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-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/Anthonyliu86/pipeline-skills-demo.git # timeout=10
Fetching upstream changes from https://github.com/Anthonyliu86/pipeline-skills-demo.git
 > git --version # timeout=10
using 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=10
Checking out Revision 35f0fbe78b205b5e0cdbf94444722411a7ebdb62 (refs/remotes/origin/master)
 > git config core.sparsecheckout # 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:13912345678
[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] { (/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] 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] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

只跑集成测试,先去test.json把SMOKE的值改成false

Started by user root
Rebuilds build #9
Rebuilds build #15
Rebuilds build #16
Rebuilds build #17
Obtained src/Jenkinsfile/projectA-stages.groovy from git https://github.com/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-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/Anthonyliu86/pipeline-skills-demo.git # timeout=10
Fetching upstream changes from https://github.com/Anthonyliu86/pipeline-skills-demo.git
 > git --version # timeout=10
using 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=10
Checking out Revision 35f0fbe78b205b5e0cdbf94444722411a7ebdb62 (refs/remotes/origin/master)
 > git config core.sparsecheckout # 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:13912345678
[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] { (/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] 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] // node
[Pipeline] End of Pipeline
Finished: 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"
				}
			}
		}
	}

}

测试结果

  • 2
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值