我有一个带有 stage1
的声明性 Jenkins 管道, stage2
, stage3
等等。我想停止stage2
从运行 if stage1
设置构建不稳定/失败。
我知道我可以停止 stage1
中的步骤从使用 return
运行当构建不成功但找不到可以退出管道而不运行以下阶段的方法时 stage1
这是我所拥有的:
stage('stage1') {
steps {
script{
//somesteps
if ("${stdout}" == "1"){
currentBuild.result = 'UNSTABLE'
return
} //if
//somesteps
} //script
} //steps
} //stage
// run only when stage1 is success
stage('stage2'){
when {
expression {
params.name ==~ /x|y/
}
}
steps {
script{
//stage2 steps
}
}
}
如果
params.name ==~ /z/
第 3 阶段将跳过第 2 阶段执行注意:我不能在 stage1 中包含 stage2/3/.. 中的步骤。应该是这样的。基于构建参数 stage2/3/4... 将在 stage1 之后调用
最佳答案
您可以在一个阶段使用 post 退出,如下所示:
pipeline {
stages {
stage('stage 1') {
steps {
//step 1
}
}
stage('stage 2') {
steps {
script{
//step 2
}
}
post{
success {
}
failure {
script{
sh "exit 1"
//or
error "Failed, exiting now..."
}
}
aborted {
}
unstable {
script{
sh "exit 1"
//or
error "Unstable, exiting now..."
}
}
}
}
}
}
这将中止构建并且作业不会进一步运行。
关于jenkins - 如果阶段设置构建失败/不稳定状态,如何退出 Jenkins 管道?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51637782/