Jenkins管道脚本中的Try-catch块 [英] Try-catch block in Jenkins pipeline script
问题描述
我试图使用下面的代码来执行构建,最后在构建成功时执行构建后的操作。不过,我得到一个MultipleCompilationErrorsException,说我的try块不是有效的段定义。请帮忙,我试了很多重组块,但似乎无法解决问题。
I'm trying to use the following code to execute builds, and in the end, execute post build actions when builds were successful. Still, I get a MultipleCompilationErrorsException, saying that my try block is Not a valid section definition. Please help, I tried a lot reorganize the block but can't seem to be able to solve the issue.
#!/usr/bin/env groovy
pipeline{
agent any
try {
stages{
stage("Parallel 1") {
steps {
parallel (
'firstTask' : {
build( "DSL-Controll-Demo-Fibonacci-1" )
},
'secondTask' : {
build( "DSL-Controll-Demo-Fibonacci-2" )
}
)
}
}
stage("Feature") {
steps {
build( "DSL-Controll-Demo-Fibonacci-5" )
build( "DSL-Controll-Demo-Fibonacci-6" )
}
}
stage("Parallel 2") {
steps{
parallel (
"thirdTask" : {
build( "DSL-Controll-Demo-Fibonacci-3" )
},
"forthTask" : {
build( "DSL-Controll-Demo-Fibonacci-4" )
}
)
}
}
}
}
catch(all) {
currentBuild.result = 'FAILURE'
}
if(currentBuild.result != 'FAILURE') {
stages{
stage("Post Build") {
steps {
build("DSL-Controll-Demo-Fibonacci-7")
}
}
}
}
}
推荐答案
您使用指定管道的声明式样式,因此您不得使用try / catch(用于脚本管道),但是发布部分。请参阅: https://jenkins.io/doc/book/pipeline/syntax/ #后期条件
Your using the declarative style of specifying your pipeline, so you must not use the try/catch (which is for Scripted Pipelines), but the post section. See: https://jenkins.io/doc/book/pipeline/syntax/#post-conditions
这篇关于Jenkins管道脚本中的Try-catch块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!