Try-catch block in jenkins pipeline script

I am trying to use the following code to complete assemblies and, in the end, perform post-assembly actions when the assemblies were successful. However, I get a MultipleCompilationErrorsException, saying that my try block is not a valid section definition. Help, I tried to reorganize the block a lot, but it seems I can not solve the problem.

#!/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")
                }   
            }   
        }
    }
}
+10
source share
3 answers

( , )

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

try... catch . . , , (, , ...)

+27

Find the AbortException class for Jenkins. You should be able to use methods to return simple messages or trace the stack. In the simple case, when you make a call in a script block (as others have indicated), you can call getMessage () to get a string that will be displayed to the user. Example:

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

Source: https://habr.com/ru/post/1677262/


All Articles