如何使用 Jenkins Pipeline 插件实现 Post-Build 阶段? [英] How to implement Post-Build stage using Jenkins Pipeline plug-in?

查看:33
本文介绍了如何使用 Jenkins Pipeline 插件实现 Post-Build 阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读 Jenkins 教程后解释 Pipeline 插件,看来该插件应该可以实现Post-Build 步骤.然而,文档在特定说明方面相当有限.

After reading Jenkins tutorial explaining Pipeline plug-in, it seems that plug-in should make it possible to implement Post-Build steps. However documentation is rather limited in regard to specific instructions.

例如我想知道如何实现:

For example I wonder how to implement:

  • 仅在构建成功时运行
  • 仅在构建成功或不稳定时运行
  • 无论构建结果如何都运行

  • 仅在构建成功时运行

    stage 'build'
    ... build
    ... tests
    stage 'post-build'
    ...
    

    (或者在MAVEN_OPTS中添加-Dmaven.test.failure.ignore=false)

    仅在构建成功或不稳定时运行

    stage 'build'
    ... build
    try {
        ... tests
    } catch {
        ...
    }
    stage 'post-build'
    ...
    

    (或者在MAVEN_OPTS中添加-Dmaven.test.failure.ignore=true)

    无论构建结果如何都运行 - 可以使用 try/catch/finally 来完成吗?

    Run regardless of build result - could it be done using try / catch / finally ?

    try {
        stage 'build'
        ...
    } catch {
        ...
    } finally {
        stage 'post-build'
        ...
    }
    

  • (我注意到最终构建状态设置为 SUCCESS,即使某些阶段,即构建",因为它基于最后一个阶段的设置而失败.这是否意味着最终构建状态需要显式设置,即currentBuild.result = 'UNSTABLE'? )

    (I've noticed that final build status is set as SUCCESS even though some stages, ie. 'build', have failed as it set based on last stage. Does that mean final build status need to explicitly set, ie.currentBuild.result = 'UNSTABLE'? )

    推荐答案

    最好的方法是在管道脚本中使用后期构建操作.

    The best way is to use post build action in the pipeline script.

    处理故障
    声明式流水线支持稳健失败默认情况下通过其 post 部分进行处理,该部分允许声明不同后置条件"的数量,例如:始终、不稳定、成功、失败和改变.管道语法部分提供有关如何使用各种后置条件的更多详细信息.

    Handling Failures
    Declarative Pipeline supports robust failure handling by default via its post section which allows declaring a number of different "post conditions" such as: always, unstable, success, failure, and changed. The Pipeline Syntax section provides more detail on how to use the various post conditions.

    Jenkinsfile(声明式流水线)

    Jenkinsfile (Declarative Pipeline)

    pipeline {
        agent any
        stages {
            stage('Test') {
                steps {
                    sh 'make check'
                }
            }
        }
        post {
            always {
                junit '**/target/*.xml'
            }
            failure {
                mail to: team@example.com, subject: 'The Pipeline failed :('
            }
        }
    }
    

    文档如下https://jenkins.io/doc/book/pipeline/syntax/#post

    这篇关于如何使用 Jenkins Pipeline 插件实现 Post-Build 阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆