概述
如何根据指定的条件进行不同的逻辑处理,这是很多场景中需要碰到的情况。将Pipeline封装成很多条不需要考虑逻辑的多条流水线的做法,在分支逻辑稍微复杂的情况下变得完全不具有可操作性,在这篇文章中将会结合使用示例介绍如何在Jenkinsfile进行条件分支的处理。
when{}
根据给定条件决定实际的分支处理逻辑,可以考虑在Jenkinsfile中使用when{}来达到这一目的。在本文中将仅仅介绍最为简单的逻辑分支处理方式,至于嵌套和多层逻辑处理将在后续的文章中进一步地展开。
- 使用限制:需要写在stage块中
- 可选vs必选:可选
内建条件类型
when所支持内建条件类型非常丰富,详细如下所示
内建条件类型 | 使用说明 | 备注 |
---|---|---|
branch | 指定分支构建时触发 | 仅对于多分支Pipeline有效 |
buildingTag | 构建tag时触发 | - |
changelog | SCM的变更日志包含指定内容时触发 | 常与正则表达式结合使用 |
changeset | SCM的changeset包含指定文件时触发 | 常与*等表达式结合使用,缺省状态是不区分大小写的,但是可以通过指定caseSensitive为true来达到区分大小写的目的 |
changeRequest | 变更请求发生时触发(比如Github的Pull Request、Gitlab的Merge Request以及Gerrit的变更等) | 如未指定参数则标明每次变更请求都会被触发,分支信息/标题/author/邮件地址等都可以作为参数传入,从而限制只有这些信息被匹配的时候才会发生触发。同时还可以与正则表达式结合使用。 |
environment | 当某环境变量被设定为某特定值时会被触发 | - |
equals | 某变量与期待值相等时被触发 | - |
expression | 表达式为true时被触发 | 注意返回的字符串必须转化为布尔类型才能正确动作。 |
tag | TAG_NAME变量满足匹配模式时会被触发 | - |
not | 当条件为false时会被触发 | - |
allOf | 当所有嵌套条件均为true时会被触发 | 至少包含一个条件 |
anyOf | 嵌套条件中至少有一个为true时会被触发 | 至少包含一个条件 |
triggeredBy | 当前构建为指定方式所触发时 | 触发方式为:SCMTrigger/TimerTrigger/UpstreamCause等。 |
环境准备
本文使用Easypack的LTS Jenkins 2.167.1版,环境准备请参看
- https://liumiaocn.blog.csdn.net/article/details/92764938
获取Jenkins-Crumb
使用如下示例代码获取Jenkins-Crumb,为使用API方式生成示例作准备。
liumiaocn:jenkins liumiao$ jenkins_host_url=http://localhost:32002
liumiaocn:jenkins liumiao$ user_passwd="root:liumiaocn"
liumiaocn:jenkins liumiao$ jenkins_crumb=`curl -u $user_passwd ${jenkins_host_url}'/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)' 2>/dev/null`
liumiaocn:jenkins liumiao$ echo $jenkins_crumb
Jenkins-Crumb:83d748ee92512c4dccd589aaa5c55a9a
liumiaocn:jenkins liumiao$
创建Job
使用如下代码示例创建Job
liumiaocn:jenkins liumiao$ ls demo/pipeline/when/config.xml
demo/pipeline/when/config.xml
liumiaocn:jenkins liumiao$ cat demo/pipeline/when/config.xml
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="workflow-job@2.32">
<actions>
<org.jenkinsci.plugins.pipeline.modeldefinition.actions.DeclarativeJobAction plugin="pipeline-model-definition@1.3.8"/>
</actions>
<description>Pipeline Job Sample</description>
<keepDependencies>false</keepDependencies>
<properties>
<hudson.plugins.jira.JiraProjectProperty plugin="jira@3.0.7"/>
<com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty plugin="gitlab-plugin@1.5.12">
<gitLabConnection></gitLabConnection>
</com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty>
</properties>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.69">
<script>pipeline {
agent any
environment {
ENVIRONMENT_TEST_FLAG = 'NO'
}
stages {
stage('Init') {
steps {
script {
BUILD_EXPRESSION = true
DEPLOY_USER = 'liumiaocn'
}
}
}
stage('Build') {
when {
expression { BUILD_EXPRESSION }
}
steps {
sh 'echo Build stage ...'
}
}
stage('Test'){
when {
environment name: 'ENVIRONMENT_TEST_FLAG',
value: 'YES'
}
steps {
sh 'echo Test stage ...'
}
}
stage('Deploy') {
when {
equals expected: 'liumiaocn',
actual: DEPLOY_USER
}
steps {
sh 'echo Deploy stage ...'
}
}
}
}</script>
<sandbox>false</sandbox>
</definition>
<triggers/>
<disabled>false</disabled>
</flow-definition>
liumiaocn:jenkins liumiao$ curl -X POST -u $user_passwd -H ${jenkins_crumb} -H "Content-Type:application/xml" --data-binary "@demo/pipeline/when/config.xml" ${jenkins_host_url}/createItem?name=pipeline_job_when
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$
Jenkinsfile说明
本文示例所使用的Jenkinsfile信息如下所示,内容非常简单易读,简单说明如下:
- pipeline是结构,在其中可以指定agent和stages等相关信息
- agent用于指定执行job的节点,any为不做限制
- stages用与设定具体的stage
- stage为具体的节点,比如本文示例中模拟实际的 Build(构建)、测试(Test)、部署(Deploy)的过程。
- when 模块用于设定此stage是否执行
pipeline {
agent any
environment {
ENVIRONMENT_TEST_FLAG = 'NO'
}
stages {
stage('Init') {
steps {
script {
BUILD_EXPRESSION = true
DEPLOY_USER = 'liumiaocn'
}
}
}
stage('Build') {
when {
expression { BUILD_EXPRESSION }
}
steps {
sh 'echo Build stage ...'
}
}
stage('Test'){
when {
environment name: 'ENVIRONMENT_TEST_FLAG',
value: 'YES'
}
steps {
sh 'echo Test stage ...'
}
}
stage('Deploy') {
when {
equals expected: 'liumiaocn',
actual: DEPLOY_USER
}
steps {
sh 'echo Deploy stage ...'
}
}
}
}
执行Job
使用BlueOcean执行之后,结果信息如下所示
执行之后使用如下命令可以确认相关的执行日志信息
liumiaocn:jenkins liumiao$ curl -u $user_passwd ${jenkins_host_url}/job/pipeline_job_when/1/consoleText
Started by user root
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /data/jenkins/workspace/pipeline_job_when
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
+ echo Build stage ...
Build stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
Stage "Test" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
+ echo Deploy stage ...
Deploy stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
liumiaocn:jenkins liumiao$
修改执行条件,将Init的stage中的设定值做如下修正,保证Build和Deploy不执行,只执行Test
变量 | 修改前 | 修改后 |
---|---|---|
BUILD_EXPRESSION | true | false |
ENVIRONMENT_TEST_FLAG | ‘NO’ | ‘YES’ |
DEPLOY_USER | ‘liumiaocn’ | ‘liumiao’ |
执行日志如下所示
liumiaocn:jenkins liumiao$ curl -u $user_passwd ${jenkins_host_url}/job/pipeline_job_when/2/consoleText
Started by user root
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /data/jenkins/workspace/pipeline_job_when
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Init)
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build)
Stage "Build" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ echo Test stage ...
Test stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
Stage "Deploy" skipped due to when conditional
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
liumiaocn:jenkins liumiao$
页面结果如下所示
总结
when可以用于多种情况下的条件判断,这篇文章以expression、environment和equals为例介绍了常见的使用方式,需要注意的是environment的环境变量和其他变量不同的地方,声明的地方在stages块之外。
最后
以上就是飘逸书本为你收集整理的Jenkins基础:Jenkinsfile使用实例:5:使用when进行条件分支处理when{}环境准备获取Jenkins-Crumb创建JobJenkinsfile说明执行Job总结的全部内容,希望文章能够帮你解决Jenkins基础:Jenkinsfile使用实例:5:使用when进行条件分支处理when{}环境准备获取Jenkins-Crumb创建JobJenkinsfile说明执行Job总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复