Jenkins管道脚本中的try-catch块

时间:2017-05-16 14:07:26

标签: jenkins groovy try-catch 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")
                }   
            }   
        }
    }
}

5 个答案:

答案 0 :(得分:28)

尝试这样(没有双关语意图顺便说一句)

script {
  try {
      sh 'do your stuff'
  } catch (Exception e) {
      sh 'Handle the exception!'
  }
}

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

答案 1 :(得分:17)

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

答案 2 :(得分:1)

查找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...
}

答案 3 :(得分:1)

try / catch是脚本化的语法。因此,任何时候只要您使用声明性语法来使用脚本中的某些内容,就可以通过将脚本化语法包含在声明性管道中的scripts块中来进行操作。因此,您的try / catch应该进入stage> steps> script。

对于在声明性管道中也要使用的任何其他脚本化管道语法,这同样适用。

答案 4 :(得分:0)

这个answer为我工作:

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"
      }
    }
  }
}
相关问题