是否有任何管道步骤可用于中止具有成功状态的某些案例的构建?
可以使用error
步骤中止具有失败状态的构建。但我不了解成功状况。
答案 0 :(得分:1)
不,管道的正常流程是从头到尾。 但是你可以做的是测试你的成功状态,而不是在if或类似的东西中调用你的其余代码。功能可以帮助您轻松实现这一目标,例如: :
node() {
// Part 1
def isSuccess = part1();
if(!isSuccess) {
part2()
}
}
// Part 2
def function part2() {
// Part 2 code
}
但是,你应该小心这类事情,也许它突出了你的管道没有正确设计的事实。如果这不是您想要的,请提供更多详细信息,例如用例。
答案 1 :(得分:1)
正如在其他回复中所说的那样,没有以这种方式中止的步骤。正如Christopher建议你可以在中止代码周围使用try-catch并使用error()。我认为您需要跟踪构建的中止状态 - 您可以在管道中全局定义中止方法以设置此状态并引发错误,以便中止您阶段中的其他步骤。
如果您使用了声明性管道,则可以使用'当'在后期阶段使用表达式声明,以便在设置中止状态时不执行。
我自己对这个问题很感兴趣所以我在这里找到了一个管道示例:
/**
* Tracking if the build was aborted
*/
Boolean buildAborted = false
/**
* Abort the build with a message
*/
def abortBuild = { String abortMessage ->
buildAborted = true
error(abortMessage)
}
pipeline {
agent any
parameters {
string(name: 'FailOrAbort', defaultValue: 'ok', description: "Enter 'fail','abort' or 'ok'")
}
stages {
stage('One') {
steps {
echo "FailOrAbort = ${params.FailOrAbort}"
script {
try {
echo 'Doing stage 1'
if(params.FailOrAbort == 'fail') {
echo "This build will fail"
error("Build has failed")
}
else if(params.FailOrAbort == 'abort') {
echo "This build will abort with SUCCESS status"
abortBuild("This build was aborted")
}
else {
echo "This build is a success"
}
echo "Stage one steps..."
}
catch(e) {
echo "Error in Stage 1: ${e.getMessage()}"
if(buildAborted) {
echo "It was aborted, ignoring error status"
}
else {
error(e.getMessage())
}
}
}
}
post {
failure {
echo "Stage 1 failed"
}
}
}
stage('Two') {
when {
expression {
return !buildAborted
}
}
steps {
echo "Doing stage 2"
}
}
stage('Three') {
when {
expression {
return !buildAborted
}
}
steps {
echo "Doing stage 3"
}
}
}
post {
always {
echo "Build completed. currentBuild.result = ${currentBuild.result}"
}
failure {
echo "Build failed"
}
success {
script {
if(buildAborted) {
echo "Build was aborted"
} else {
echo 'Build was a complete success'
}
}
}
unstable {
echo 'Build has gone unstable'
}
}
}
作为旁注,有一个属性" currentBuild.result'您可以在管道中进行调整,但一旦设置为“失败”即可。它不能被清除回成功'成功' - 詹金斯模型不允许AFAIK 。
答案 2 :(得分:0)
首先,我不知道这样的一步。
但是,如果成功,您可以使用error
步骤中止构建并使用某个消息。在try{}catch(){}
块中捕获此错误,检查消息并将构建状态设置为成功。