【问题标题】:setting status of a build in jenkins在詹金斯中设置构建状态
【发布时间】:2020-03-22 17:05:44
【问题描述】:

我有一份詹金斯的工作。 这很简单:从 git 中提取,然后运行构建。

构建只是一步:

执行窗口命令批处理

在我的用例中,我需要运行一些 python 脚本。 有些会失败,有些不会。

python a.py
python b.py

什么决定了构建的最终状态? 看来我可以通过以下方式进行编辑:

echo @STABLE > build.proprieties

但是如果用户没有指定,如何分配 STABLE/UNSTABLE 状态? 如果 b.py 引发错误并失败会怎样?

【问题讨论】:

    标签: jenkins build continuous-integration devops


    【解决方案1】:

    如果命令返回不等于零的退出代码,Jenkins 会将管道解释为失败。

    在内部使用currentBuild.currentResult 设置构建状态,它可以有三个值:SUCCESSUNSTABLEFAILURE

    如果您想自己控制管道的失败/成功,您可以捕获异常/退出代码并手动设置currentBuild.currentResult 的值。插件也使用这个属性来改变管道的结果。

    例如:

    stage {
     steps {
      script {
        try {
            sh "exit 1" // will fail the pipeline
            sh "exit 0" // would be marked as passed
            currentBuild.currentResult = 'SUCCESS'
        } catch (Exception e) {
            currentBuild.currentResult = 'FAILURE'
            // or currentBuild.currentResult = 'UNSTABLE'
        }
      }
    }}
    

    【讨论】:

    • 仅当返回不等于零的退出代码时。如果脚本返回 2,该阶段也将被标记为失败。只有返回值 0 让 Jenkins 将阶段标记为成功,因此您可以在脚本中考虑返回值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2020-07-04
    • 2016-07-16
    • 2014-07-18
    • 2012-10-13
    相关资源
    最近更新 更多