Jenkinsfile以stage为逻辑功能实现和划分的方式使用起来非常方便,而一旦发生异常之后应该如何处理,如何根据stage执行的结果而进行特定处理则是实际Pipeline使用中经常会碰到的问题。在Groovy或者Java语言中一般这就是try {} catch{}的使用场景,而Jenkinsfile也将执行结果的状态进行共通化,可以根据执行结果的特定状态作为触发的条件,这就是post{}。而流水线的实现则只需要在对应的状态中执行预定的处理逻辑即可,这篇文章中将通过具体的示例进行介绍和演示。
根据执行记的结果决定处理内容,就像Java程序中catch块中做的那样,post{}根据pipeline或者stage的执行结果预先定义了多个条件,通过在流水线中声明这些条件和在这些条件之下的steps操作即可,类似回调函数的使用方法,你也可以把它看作是try…catch…的封装实现,从而使得对于异常的处理更加方便。
注意: cleanup和always的区别在于,cleanup会在其他任意一个条件预置操作执行之后就会执行。
本文示例:
将流水线分按照功能分为逻辑上的三段:构建(Build) 、测试(Test)、部署(Deploy),顺序如下所示。
这里将本文中提到的条件分开在这三个stage中进行示例使用介绍。
本文使用Easypack的LTS Jenkins 2.176.1版,环境准备请参看
全集工具设定可以通过XML配置文件的方式,也可以直接在Jenkins的管理界面进行操作,详细可参照:
使用如下示例代码获取Jenkins-Crumb,为使用API方式为示例作准备。
liumiaocn:jenkins liumiao$ jenkins_host_url=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
liumiaocn:jenkins liumiao$ ls demo/pipeline/l
demo/pipeline/l
liumiaocn:jenkins liumiao$ cat demo/pipeline/l
<?xml version='1.1' encoding='UTF-8'?>
<flow-definition plugin="workflow-job@2.32"><actions><org.jenkinsci.deldefinition.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.tion.GitLabConnectionProperty plugin="gitlab-plugin@1.5.12"><gitLabConnection></gitLabConnection></com.tion.GitLabConnectionProperty></properties><definition class="org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition" plugin="workflow-cps@2.69"><script>pipeline {agent anystages {stage('Build') {steps {sh 'echo Build stage ...'}post {always {echo "post condition executed: always ..."}changed {echo "post condition executed: changed ..."}aborted {echo "post condition executed: aborted ..."}regression {echo "post condition executed: regression ..."}}}stage('Test'){steps {sh 'echo Test stage ...'}post {aborted {echo "post condition executed: aborted ..."}failure {echo "post condition executed: failure ..."}success {echo "post condition executed: success ..."}}}stage('Deploy') {steps {sh 'echo Deploy stage ...'}}}post {unstable {echo "post condition executed: unstable ..."}unsuccessful {echo "post condition executed: unsuccessful ..."}cleanup {echo "post condition executed: cleanup ..."}}}</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/l" ${jenkins_host_url}/createItem?name=pipeline_job_post
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$
本文示例所使用的Jenkinsfile信息如下所示,内容非常简单易读,简单说明如下:
pipeline {agent anystages {stage('Build') {steps {sh 'echo Build stage ...'}post {always {echo "post condition executed: always ..."}changed {echo "post condition executed: changed ..."}aborted {echo "post condition executed: aborted ..."}regression {echo "post condition executed: regression ..."}}}stage('Test'){steps {sh 'echo Test stage ...'}post {aborted {echo "post condition executed: aborted ..."}failure {echo "post condition executed: failure ..."}success {echo "post condition executed: success ..."}}}stage('Deploy') {steps {sh 'echo Deploy stage ...'}}}post {unstable {echo "post condition executed: unstable ..."}unsuccessful {echo "post condition executed: unsuccessful ..."}cleanup {echo "post condition executed: cleanup ..."}}}
使用如下命令或者直接在Jenkins上点击构建
liumiaocn:jenkins liumiao$ curl -X POST -u $user_passwd -H ${jenkins_crumb} ${jenkins_host_url}/job/pipeline_job_post/build
liumiaocn:jenkins liumiao$ echo $?
0
liumiaocn:jenkins liumiao$
使用如下命令可以确认相关的执行日志信息
liumiaocn:jenkins liumiao$ curl -u $user_passwd ${jenkins_host_url}/job/pipeline_job_post/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_post
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Build)
[Pipeline] sh
+ echo Build stage ...
Build stage ...
Post stage
[Pipeline] echo
post condition executed: always ...
[Pipeline] echo
post condition executed: changed ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] sh
+ echo Test stage ...
Test stage ...
Post stage
[Pipeline] echo
post condition executed: success ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
+ echo Deploy stage ...
Deploy stage ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
post condition executed: cleanup ...
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
liumiaocn:jenkins liumiao$
post用来做失败处理较为方便,一般来说会在末尾处进行处理,但是由于post实际上是支持pipeline或者stage的,在末尾的方式是pipeline块的post处理。的确,对失败进行统一处理的确是一种好的方式,但是也没有比较过于教条,正如我们使用try…catch那样,适合自己项目的方式才是最好的
本文发布于:2024-02-04 22:43:02,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170718039360369.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |