Jenkins管道脚本中的try-catch块

len*_*ovi 15 groovy try-catch jenkins jenkins-pipeline

我正在尝试使用以下代码来执行构建,最后,在构建成功时执行构建后操作.不过,我得到一个MultipleCompilationErrorsException,说我的try块不是有效的section定义.请帮助,我尝试了很多重组块但似乎无法解决问题.

#!/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")
                }   
            }   
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 39

尝试这样(没有双关语意味着btw)

script {
  try {
      sh 'do your stuff'
  } catch (Exception e) {
      sh 'Handle the exception!'
  }
}
Run Code Online (Sandbox Code Playgroud)

关键是将try ... catch放在声明性管道语法的脚本块中.然后它会工作.如果您想说尽管失败仍然继续管道执行(例如:测试失败,仍需要报告......),这可能很有用.

  • 请注意,通用“异常”是 Jenkins 安全沙箱(默认情况下)唯一允许的异常。 (3认同)

Mik*_*ike 20

您正在使用指定管道的声明式样式,因此您不能使用try/catch块(用于脚本管道),而是使用post部分.请参阅:https://jenkins.io/doc/book/pipeline/syntax/#post-conditions


Rob*_*sey 13

查找 Jenkins 的 AbortException 类。您应该能够使用这些方法来获取简单的消息或堆栈跟踪。在一个简单的情况下,在脚本块中进行调用时(正如其他人指出的那样),您可以调用 getMessage() 来获取要回显给用户的字符串。例子:

script {
        try {
            sh "sudo docker rmi frontend-test"
        } catch (err) {
            echo err.getMessage()
            echo "Error detected, but we will continue."
        }
        ...continue with other code...
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*son 8

这个答案对我有用:

pipeline {
  agent any
  stages {
    stage("Run unit tests"){
      steps {
        script {
          try {
            sh  '''
              # Run unit tests without capturing stdout or logs, generates cobetura reports
              cd ./python
              nosetests3 --with-xcoverage --nocapture --with-xunit --nologcapture --cover-package=application
              cd ..
              '''
          } finally {
            junit 'nosetests.xml'
          }
        }
      }
    }
    stage ('Speak') {
      steps{
        echo "Hello, CONDITIONAL"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Mai*_*nak 6

try/catch 是脚本语法。因此,任何时候您使用声明性语法来使用脚本中的某些内容时,通常都可以通过将脚本化语法包含在声明性管道的脚本块中来实现。所以你的 try/catch 应该进入 stage >steps >script 内部。

这也适用于您想在声明性管道中使用的任何其他脚本化管道语法。