gpt4 book ai didi

jenkins - 如何使用 Jenkins Pipeline 插件实现 Post-Build 阶段?

转载 作者:行者123 更新时间:2023-12-02 09:24:01 39 4
gpt4 key购买 nike

读完 Jenkins tutorial 后解释Pipeline插件,看来插件应该可以实现Post-Build步骤。然而,文档在具体说明方面相当有限。

例如我想知道如何实现:

  • Run only if build succeeds
  • Run only if build succeeds or is unstable
  • Run regardless of build result
  • 仅在构建成功时运行

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

    (或将 -Dmaven.test.failure.ignore=false 添加到 MAVEN_OPTS)

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

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

    (或将 -Dmaven.test.failure.ignore=true 添加到 MAVEN_OPTS)

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

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

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

最佳答案

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

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(声明式管道)

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 - 如何使用 Jenkins Pipeline 插件实现 Post-Build 阶段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36651432/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com