跳过正文
  1. Posts/

如何让 Jenkins Pipeline 在特定错误发生时不中断失败

·268 字·1 分钟· ·
沈显鹏
作者
沈显鹏
目录

有时候,我们并不希望 Jenkins Pipeline 因为某个特定错误而直接失败。
这时可以使用 catchError 来捕获错误,并将阶段(stage)或构建(build)结果更新为 SUCCESSUNSTABLEFAILURE(如果需要的话)。

Catch Error Jenkins pipeline


Jenkinsfile 示例
#

pipeline {
    agent { node { label 'linux' } }

    stages {
        stage('Catch Error') {
            steps {
                catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE', message: 'abc: command not found') {
                    sh "abc"
                }
            }
        }
    }
}

Pipeline 输出示例
#

17:14:07  [Pipeline] Start of Pipeline
17:14:08  [Pipeline] node
17:14:08  Running on linux in /agent/workspace/Stage-Job/catch-error
17:14:08  [Pipeline] {
17:14:08  [Pipeline] stage
17:14:08  [Pipeline] { (Catch Error)
17:14:08  [Pipeline] catchError
17:14:08  [Pipeline] {
17:14:08  [Pipeline] sh
17:14:08  + abc
17:14:08  /agent/workspace/Stage-Job/catch-error@tmp/durable-303b03ca/script.sh: line 1: abc: command not found
17:14:08  [Pipeline] }
17:14:08  ERROR: abc: command not found
17:14:08  ERROR: script returned exit code 127
17:14:08  [Pipeline] // catchError
17:14:08  [Pipeline] }
17:14:08  [Pipeline] // stage
17:14:08  [Pipeline] }
17:14:08  [Pipeline] // node
17:14:08  [Pipeline] End of Pipeline
17:14:08  Finished: UNSTABLE

转载本文请注明作者与出处,禁止商业用途。欢迎关注公众号「DevOps攻城狮」。

相关文章

Jenkins Linux Agent 配置
·353 字·1 分钟
本文提供了 Jenkins Linux Agent 的逐步配置指南,包括 Java 运行时的准备、节点创建以及常见问题的排查方法。
Jenkins Windows Agent 配置
·608 字·2 分钟
本文提供 Jenkins Windows Agent 的详细配置步骤,包括 Java 运行时准备、节点创建以及常见问题的排查方法。
解决通过 Jenkins Artifactory plugin 上传 artifacts 失败的问题 “unable to find valid certification path to requested target”
·568 字·2 分钟
本文介绍了如何解决 Jenkins agent 上传 artifacts 到 Artifactory 时遇到的 SSL 证书验证问题,包括生成安全认证文件和导入到 Java 的 cacerts 中。
为什么我的 Jenkins Controller 越来越慢?可能犯了这些错误...
·3163 字·7 分钟
本文介绍了 Jenkins pipeline 的一些最佳实践,旨在帮助开发者和运维人员优化 Jenkins 的性能和可维护性。
如何在 Jenkins 多分支流水线中实现 [skip ci]
·420 字·1 分钟
本文介绍如何在 Jenkins 多分支流水线中实现 [skip ci] 功能,根据提交信息跳过构建。
如何启用、配置和禁用 Jenkins LDAP
·311 字·1 分钟
本文介绍如何在 Jenkins 中启用和配置 LDAP 身份验证,以及在需要时临时禁用它的方法。