catchError()设置CurrentBuild.CurrentResult时仅应设置stageresult

发布于 2025-01-29 18:55:32 字数 1602 浏览 2 评论 0原文

我有一个像这样的Jenkinsfile:

pipeline {
  agent any
  stages {
    stage('Parallel') {
      parallel {
        stage('Non-critical stage') {
          steps {
            script {
              // Other steps...
              if (/*some error condition*/) {
                catchError(buildResult: null, stageResult: 'UNSTABLE') {
                  // Fail only the stage, but leave the buildResult as is
                  error("Error message")
                }
              }
            }
          }
        }
        stage('critical stage') {
          steps {
            // a different stage that really matters
          }
        }
      }
      post {
        always {
          script {
            if (currentBuild.currentResult != 'SUCCESS') {
              // When the Non-critical stage fails, the currentResult is UNSTABLE...
              // ...even though only the stageResult should have been set.
              // However, in the Jenkins UI the build result is SUCCESS
            }
          }
        }
      }
    }
  }
}

非关键阶段中,目标是在满足某些错误条件时不要让构建失败。但是,仅将阶段标记为不稳定。这在詹金斯效果很好。构建结果将是成功,即使非关键阶段不稳定。但是,在中, 并行 stage CurrentBuild.CurrentResult 设置为不稳定

post块是执行的最后一件事。因此,我不明白,当jenkinsfile代码的最后一个位置在jenkinsfile代码的最后位时,如何在詹金斯(Jenkins)中显示构建结果在詹金斯(Jenkins)中。

同样,当跳过非关键阶段时,currentBuild.currentResultsuccess> Success如所预期。因此,结果为不稳定绝对是由error()调用引起的。

I have a Jenkinsfile like this:

pipeline {
  agent any
  stages {
    stage('Parallel') {
      parallel {
        stage('Non-critical stage') {
          steps {
            script {
              // Other steps...
              if (/*some error condition*/) {
                catchError(buildResult: null, stageResult: 'UNSTABLE') {
                  // Fail only the stage, but leave the buildResult as is
                  error("Error message")
                }
              }
            }
          }
        }
        stage('critical stage') {
          steps {
            // a different stage that really matters
          }
        }
      }
      post {
        always {
          script {
            if (currentBuild.currentResult != 'SUCCESS') {
              // When the Non-critical stage fails, the currentResult is UNSTABLE...
              // ...even though only the stageResult should have been set.
              // However, in the Jenkins UI the build result is SUCCESS
            }
          }
        }
      }
    }
  }
}

In the Non-critical stage the goal is to not let the build fail, when some error condition is met. But to only mark the stage as UNSTABLE. This works fine in Jenkins. The build result will be SUCCESS, even when the Non-critical stage is UNSTABLE. However, in the post block of the Parallel stage currentBuild.currentResult is set to UNSTABLE.

The post block is the last thing being executed. So I do not understand, how the build result can be shown as SUCCESS in Jenkins, when the currentResult is UNSTABLE in the last bit of Jenkinsfile code being executed.

Also when the Non-critical stage is skipped, currentBuild.currentResult is SUCCESS as expected. So the result being UNSTABLE is definitely caused by the error() call.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的那么颓废 2025-02-05 18:55:32

我发现我的问题是,后阶段并不直接在管道块的范围内。而是直接位于阶段('Parallel')块中。

将邮政块移动后:

pipeline {
  agent any
  stages {
    stage('Parallel') {
      parallel {
        stage('Non-critical stage') {
          steps {
            script {
              // Other steps...
              if (/*some error condition*/) {
                catchError(buildResult: null, stageResult: 'UNSTABLE') {
                  // Fail only the stage, but leave the buildResult as is
                  error("Error message")
                }
              }
            }
          }
        }
        stage('critical stage') {
          steps {
            // a different stage that really matters
          }
        }
      }
    }
  }
  post {
    always {
      script {
        if (currentBuild.currentResult != 'SUCCESS') {
          // Now the currentResult is 'SUCCESS'
        }
      }
    }
  }
}

CurrentBuild.CurrentResult属性正确保存值Success

I found out that my problem was, that the post stage was not directly in the scope of the pipeline block. And instead was directly in the stage('Parallel') block.

After moving the post block down like this:

pipeline {
  agent any
  stages {
    stage('Parallel') {
      parallel {
        stage('Non-critical stage') {
          steps {
            script {
              // Other steps...
              if (/*some error condition*/) {
                catchError(buildResult: null, stageResult: 'UNSTABLE') {
                  // Fail only the stage, but leave the buildResult as is
                  error("Error message")
                }
              }
            }
          }
        }
        stage('critical stage') {
          steps {
            // a different stage that really matters
          }
        }
      }
    }
  }
  post {
    always {
      script {
        if (currentBuild.currentResult != 'SUCCESS') {
          // Now the currentResult is 'SUCCESS'
        }
      }
    }
  }
}

the currentBuild.currentResult property correctly holds the value SUCCESS

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文