在詹金斯中设置构建状态

00_*_*_00 3 continuous-integration build jenkins devops

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

构建只是一步:

执行窗口命令批处理

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

python a.py
python b.py
Run Code Online (Sandbox Code Playgroud)

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

echo @STABLE > build.proprieties
Run Code Online (Sandbox Code Playgroud)

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

mke*_*erz 5

如果命令返回不等于零的退出代码,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'
    }
  }
}}
Run Code Online (Sandbox Code Playgroud)