我正在寻找退出声明式 Jenkins 管道的最干净的方法, 具有成功状态。 虽然使用 error step 退出错误非常简洁,但我找不到任何相同的方法来退出成功代码。 例如:
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..."
}
}
对于脚本化构建的示例,我可以使用以下代码来实现它:
if (shouldSkip == true) {
echo ("'ci skip' spotted in all git commits. Aborting.")
currentBuild.result = 'SUCCESS'
return
}
虽然我知道可以将脚本step添加到我的声明性管道中,但我希望找到一种更干净的方法。
另一种方法可能是抛出错误并在某处捕获它,但同样很混乱。
有更干净的方法吗?
对我有用的解决方案是创建一个包含子阶段的阶段,并将检查放在顶层阶段。
stage('Run if expression ') {
when {
expression { skipBuild != true }
}
stages {
stage('Hello') {
steps {
echo "Hello there"
}
}
}
}
所以我把所有我想继续的阶段都放在这个阶段里。以及它之外的一切。在您的情况下,您可以将所有构建阶段放入带有何时检查的阶段中。
import hudson.model.Result
import jenkins.model.CauseOfInterruption
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
def haltBuildWithSuccess() {
// Mark the current build as a success
currentBuild.rawBuild.@result = Result.SUCCESS
// Create a custom interruption cause
def cause = new CauseOfInterruption.UserInterruption("Build halted programmatically with SUCCESS status")
// Throw a FlowInterruptedException to halt the build
throw new FlowInterruptedException(Result.SUCCESS, false, cause)
}
pipeline{
agent any
stages{
stage("First"){
steps{
script{
haltBuildWithSuccess()
}
}
}
stage("won't be executed"){
steps{
echo "You will Never see it"
}
}
}
}