Jenkins基础:Jenkinsfile使用实例:8:使用post进行失败处理

阅读: 评论:0

Jenkins基础:Jenkinsfile使用实例:8:使用post进行失败处理

Jenkins基础:Jenkinsfile使用实例:8:使用post进行失败处理

Jenkinsfile以stage为逻辑功能实现和划分的方式使用起来非常方便,而一旦发生异常之后应该如何处理,如何根据stage执行的结果而进行特定处理则是实际Pipeline使用中经常会碰到的问题。在Groovy或者Java语言中一般这就是try {} catch{}的使用场景,而Jenkinsfile也将执行结果的状态进行共通化,可以根据执行结果的特定状态作为触发的条件,这就是post{}。而流水线的实现则只需要在对应的状态中执行预定的处理逻辑即可,这篇文章中将通过具体的示例进行介绍和演示。

post{}

根据执行记的结果决定处理内容,就像Java程序中catch块中做的那样,post{}根据pipeline或者stage的执行结果预先定义了多个条件,通过在流水线中声明这些条件和在这些条件之下的steps操作即可,类似回调函数的使用方法,你也可以把它看作是try…catch…的封装实现,从而使得对于异常的处理更加方便。

  • 使用限制:需要写在pipeline或者stage块中
  • 可选vs必选:可选
  • 支持的条件预置:
    • always: 无论pipeline或者stage的执行结果如何,此块中的预置操作都会执行。
    • changed:只有当pipeline或者stage的执行后,当前状态与之前发生了改变时,此块中的预置操作才会执行。
    • fixed:前一次运行为不稳定状态或者失败状态,而且本次运行成功结束,这两个条件同时满足时,此块中的预置操作才会执行。
    • regression: 本次运行状态为不稳定状态,失败状态或者是中止状态,而且前一次运行成功结束,这两个条件同时满足时,此块中的预置操作才会执行。
    • aborted:当前pipeline或者stage的状态为aborted时,此块中的预置操作才会执行。通常是由于流水线被手工中会导致此状态产生,而产生此状态后,通常在Jenkins的UI界面会显示为灰色。
    • failure:当前pipeline或者stage的状态为failed时,此块中的预置操作才会执行。而产生此状态后,通常在Jenkins的UI界面会显示为红色。
    • success:当前pipeline或者stage的状态为success时,此块中的预置操作才会执行。而产生此状态后,通常在Jenkins的UI界面会显示为绿色。
    • unstable: 当前pipeline或者stage的状态为unstable时,此块中的预置操作才会执行。通常情况下测试失败或者代码规约的违反都会导致此状态产生,而产生此状态后,通常在Jenkins的UI界面会显示为黄色。
    • unsuccessful:当前pipeline或者stage的状态不是success时,此块中的预置操作才会执行。
    • cleanup:无论pipeline或者stage的状态为何种状态,在post中的其他的条件预置操作执行之后,此块中的预置操作就会执行。

注意: cleanup和always的区别在于,cleanup会在其他任意一个条件预置操作执行之后就会执行。

示例

本文示例:
将流水线分按照功能分为逻辑上的三段:构建(Build) 、测试(Test)、部署(Deploy),顺序如下所示。

这里将本文中提到的条件分开在这三个stage中进行示例使用介绍。

环境准备

本文使用Easypack的LTS Jenkins 2.176.1版,环境准备请参看

全局工具设定

全集工具设定可以通过XML配置文件的方式,也可以直接在Jenkins的管理界面进行操作,详细可参照:

获取Jenkins-Crumb

使用如下示例代码获取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

使用如下代码示例创建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说明

本文示例所使用的Jenkinsfile信息如下所示,内容非常简单易读,简单说明如下:

  • pipeline是结构,在其中可以指定agent和stages等相关信息
  • agent用于指定执行job的节点,any为不做限制
  • stages用与设定具体的stage
  • stage为具体的节点,比如本文示例中模拟实际的 Build(构建)、测试(Test)、部署(Deploy)的过程。
  • post中可以根据stage的执行结果进行错误处理
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 ..."}}}

执行Job

使用如下命令或者直接在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小时内删除。

标签:实例   基础   Jenkins   post   Jenkinsfile
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23