Jenkins 管道脚本中的 Try-catch 块 [英] Try-catch block in Jenkins pipeline script

查看:49
本文介绍了Jenkins 管道脚本中的 Try-catch 块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码来执行构建,最后,在构建成功时执行构建后操作.尽管如此,我还是收到了 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 块(用于脚本化管道),而是使用 post 部分.请参阅:https://jenkins.io/doc/book/pipeline/syntax/#后置条件

You're using the declarative style of specifying your pipeline, so you must not use try/catch blocks (which are for Scripted Pipelines), but the post section. See: https://jenkins.io/doc/book/pipeline/syntax/#post-conditions

这篇关于Jenkins 管道脚本中的 Try-catch 块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆