如何在 Jenkins 声明式语法中添加 try catch 块。?

Vin*_*kha 5 groovy jenkins

我正在尝试添加try catch blockJenkins 声明性管道,但最终出现以下错误,我阅读了有关为 Jenkins 的脚本化管道语法添加 try catch 块的文档(https://jenkins.io/doc/book/pipeline/syntax/ #post-conditions ) 但我没有得到任何关于声明性语法的信息。

pipeline {
agent any
    stages {
        try {
            stage('Checkout') {
                steps {
                    script {
                        if (ci_branches.contains(env.BRANCH_NAME)) {
                            // Pull the code from bitbucket repository
                            checkout scm
                        }
                    }
                }
            }
        }
        catch(all) {
            currentBuild.result='FAILURE'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jenkins ci 构建结果

[Bitbucket] Build result notified
    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
    WorkflowScript: 36: Expected a stage @ line 36, column 13.
                   try {
                   ^
    WorkflowScript: 35: No stages specified @ line 35, column 9.
               stages {
           ^
Run Code Online (Sandbox Code Playgroud)

Red*_*dio 7

使用声明性管道语法时,Try/catch 应该在脚本内。测试以下内容:

pipeline {
agent any
    stages {       
        stage('Checkout') {
            steps {
                script {
                    try {
                        if (ci_branches.contains(env.BRANCH_NAME)) {
                            // Pull the code from bitbucket repository
                            checkout scm
                        }
                    }
                    catch(all) {
                        currentBuild.result='FAILURE'
                    }   
                }
            }
        }        
    }
}
Run Code Online (Sandbox Code Playgroud)