Jenkins:即使构建失败,管道也总是返回“ SUCCESS”

时间:2019-03-04 22:12:26

标签: jenkins jenkins-pipeline

更新:这似乎是Pipeline: Declarative插件版本1.3.5中引入的错误-降级为1.3.4.1可解决此问题。票证位于:


在远程执行器上使用时,如何获取构建的真实状态?

困境:

  • 使用声明性管道
  • 内部运行在远程执行器上
  • 构建失败
  • 发帖{失败{}}
  • currentBuild.result为NULL,currentBuild.currentResult为成功

在远程执行器上执行构建时,如何访问实际的故障?

代码视图:


pipeline {
    agent any
    stages {
        stage("test run") {
            steps {
                sh "exit 1"
            }
        }
    }

    post {
        always { 
            echo "I always run: ${currentBuild.result} <> ${currentBuild.currentResult}"
        }
        success {
            echo "I'm successful: ${currentBuild.result} <> ${currentBuild.currentResult}"
        }
        failure {
            echo "I failed: ${currentBuild.result} <> ${currentBuild.currentResult}"
        }
        fixed {
            echo "I'm fixed!: ${currentBuild.result} <> ${currentBuild.currentResult}"
        }
    }
}

输出:

[Pipeline] Start of Pipeline
[Pipeline] node
Running on build-096575a3-e6af-4fff-9ca1-84cc46ba4b86-f9b8d29c in /var/vcap/data/jenkins-slave/workspace/test-job
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test run)
[Pipeline] sh
+ exit 1
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
I always run: null <> SUCCESS
[Pipeline] echo
I failed: null <> SUCCESS
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

2 个答案:

答案 0 :(得分:1)

(为我的问题发布“官方答复”)

这是Pipeline: Declarative插件版本1.3.5中引入的“错误”-降级为1.3.4.1可解决此问题。票证位于:

在用户和开发人员之间来回回荡是否确实是一个错误。开发人员建议不要再使用currentBuild.result,但许多人指出这是一次BC中断,并导致很多问题。

该问题已标记为Critical-可能会回滚或重新开发以便更好地实施。

如果您依靠currentBuild.resultcurrentBuild.currentResult的此功能,建议您对此错误发表评论。

答案 1 :(得分:0)

您可以使用try catch:

                        try {
                        sh "exit 1"
                        currentBuild.result = 'SUCCESS'
                        }catch (Exception err) {
                        currentBuild.result = 'FAILURE'
                        }