在我们的 Jenkins Pipeline 工作中,我们有几个阶段,我想要的是,如果任何一个阶段失败,那么构建就会停止,而不是继续到进一步的阶段。
以下是其中一个阶段的示例:
stage('Building') {
def result = sh returnStatus: true, script: './build.sh'
if (result != 0) {
echo '[FAILURE] Failed to build'
currentBuild.result = 'FAILURE'
}
}
脚本将失败,构建结果将更新,但作业将继续进行到下一个阶段。发生这种情况时,我该如何中止或停止作业?
基本上就是这样sh
步骤确实如此。如果您不将结果捕获到变量中,则可以运行:
sh "./build"
如果脚本返回非零退出代码,这将退出。
如果您需要先做一些事情,并且需要捕获结果,您可以使用 shell 步骤来退出作业
stage('Building') {
def result = sh returnStatus: true, script: './build.sh'
if (result != 0) {
echo '[FAILURE] Failed to build'
currentBuild.result = 'FAILURE'
// do more stuff here
// this will terminate the job if result is non-zero
// You don't even have to set the result to FAILURE by hand
sh "exit ${result}"
}
}
但以下会给你同样的结果,但看起来更明智
stage('Building') {
try {
sh './build.sh'
} finally {
echo '[FAILURE] Failed to build'
}
}
也可以在代码中调用 return。然而,如果你在一个stage
它只会从那个阶段返回。所以
stage('Building') {
def result = sh returnStatus: true, script: './build.sh'
if (result != 0) {
echo '[FAILURE] Failed to build'
currentBuild.result = 'FAILURE'
return
}
echo "This will not be displayed"
}
echo "The build will continue executing from here"
不会退出工作,但是
stage('Building') {
def result = sh returnStatus: true, script: './build.sh'
}
if (result != 0) {
echo '[FAILURE] Failed to build'
currentBuild.result = 'FAILURE'
return
}
will.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)