首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >catchError()在只设置stageResult时设置stageResult

catchError()在只设置stageResult时设置stageResult
EN

Stack Overflow用户
提问于 2022-05-18 07:59:50
回答 1查看 648关注 0票数 1

我有一个这样的詹金斯档案:

代码语言:javascript
复制
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
            }
          }
        }
      }
    }
  }
}

Non-critical stage中,目标是当some error condition满足时,不要让构建失败。但只将舞台标记为UNSTABLE。这在詹金斯很好。构建结果将是SUCCESS,即使Non-critical stageUNSTABLE。但是,在post阶段的Parallel块中,currentBuild.currentResult设置为UNSTABLE

post块是执行的最后一件事。因此,我不明白如何在Jenkins中将构建结果显示为SUCCESS,而在正在执行的Jenkinsfile代码中,currentResult是不稳定的。

同样,当跳过Non-critical stage时,currentBuild.currentResult也是SUCCESS。因此,UNSTABLE的结果肯定是由error()调用引起的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-23 07:54:53

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

在像这样移动了后座之后:

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72285290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档