退出声明式詹金斯管道成功的干净方法? [英] Clean way to exit declarative Jenkins pipeline as success?

查看:71
本文介绍了退出声明式詹金斯管道成功的干净方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找退出声明式Jenkins管道的最干净的方法, 具有成功状态. 使用错误步骤退出时,错误非常整洁,我找不到退出成功代码的任何相等方法. 例如:

I am looking for the cleanest way to exit a declarative Jenkins pipeline, with a success status. While exiting with an error is very neat using error step , I couldn't find any equal way to exit with success code. E.G:

stage('Should Continue?') {
  when {
    expression {skipBuild == true }
  }
  steps {
    echo ("Skiped Build")
    setBuildStatus("Build complete", "SUCCESS");
    // here how can I abort with sucess code?
    // Error Would have been:
    // error("Error Message")

  }
}
stage('Build') {
  steps {
    echo "my build..."
  }
}

例如,使用脚本构建的示例,可以通过以下代码实现:

For Example with a scripted build, I could achieve it with the following code:

if (shouldSkip == true) {
  echo ("'ci skip' spotted in all git commits. Aborting.")
  currentBuild.result = 'SUCCESS'
  return
}

虽然我知道可以添加脚本步骤到我声明性的pipieline,我希望找到一种更清洁的方法.

While I am aware of the ability to add a script step to my declarative pipieline, I was hoping to find a cleaner way.

另一种方法可能是抛出错误并在某个地方捕获错误,但又很混乱.

Another approach could be throwing an error and catch it somewhere down the line, but again it quite messy.

有没有更清洁的方法?

推荐答案

对我有用的解决方案是创建一个带有子阶段的阶段,并将支票置于顶层阶段.

A solution that worked for me was to create a stage with sub-stages and put the check in the top level stage.

stage('Run if expression ') {
    when {
        expression { skipBuild != true }
    }
    stages {
        stage('Hello') {
            steps {
                echo "Hello there"
            }
        }
    }
}

因此,我将所有要继续的阶段都放在了这个阶段中.以及其他所有内容.在您的情况下,您可以将所有构建阶段都放在带有check的阶段中.

So I put all the stages that I want to continue inside this stage. And everything else outside of it. In your case you would put all your build stages inside the stage with when check.

这篇关于退出声明式詹金斯管道成功的干净方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆