Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline - try catch for particular stage and subsequent conditional step

Tags:

I'm trying to replicate the equivalent of a conditional stage in Jenkins pipeline using a try / catch around a preceding stage, which then sets a success variable, which is used to trigger the conditional stage.

It appears that a try catch block is the way to go, setting a success var to SUCCESS or FAILED, which is used as part of a when statement later (as part of the conditional stage).

The code I am using is as follows:

pipeline {     agent any     stages {         try{             stage("Run unit tests"){                 steps{                     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 ..                     '''                     currentBuild.result = 'SUCCESS'                 }             }         } catch(Exception e) {             // Do something with the exception              currentBuild.result = 'SUCCESS'         }          stage ('Speak') {             when {                 expression { currentBuild.result == 'SUCCESS' }             }             steps{                 echo "Hello, CONDITIONAL"             }         }     } } 

The latest syntax error I am receiving is as follows:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup  failed: WorkflowScript: 4: Expected a stage @ line 4, column 9.        try{ 

I've also tried lots of variations.

Am I taking the wrong approach here? This seems like a fairly common requirement.

Thanks.

like image 462
Xerphiel Avatar asked Apr 08 '17 11:04

Xerphiel


People also ask

How do I catch exceptions in Jenkins pipeline?

try/catch is scripted syntax. So any time you are using declarative syntax to use something from scripted in general you can do so by enclosing the scripted syntax in the scripts block in a declarative pipeline. So your try/catch should go inside stage >steps >script.

Can we run a step conditionally in Jenkins?

The Conditional BuildStep plugin lets users add conditional logic to Freestyle jobs from within the Jenkins web UI. It does this by: Adding two types of Conditional BuildStep ("Single" and "Multiple") - these build steps contain one or more other build steps to be run when the configured condition is met.

Can a Jenkins stage have multiple steps?

Jenkins Pipeline allows you to compose multiple steps in an easy way that can help you model any sort of automation process. Think of a "step" like a single command which performs a single action. When a step succeeds it moves onto the next step. When a step fails to execute correctly the Pipeline will fail.


1 Answers

This might solve your problem depending on what you are going for. Stages are only run when the preceding stages succeed, so if you actually have two stages like in your example, and if you want the second to only run when the first succeeds, you want to ensure that the first stage fails appropriately when tests fail. Catching will prevent the (desirable) failure. Finally will preserve the failure, and can also still be used to grab your test results.

So here, the second stage will only run when the tests pass, and the test results will be recorded regardless:

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"       }     }   } } 

Note that I'm actually using try in a declarative pipeline, but like StephenKing says, you can't just use try directly (you have to wrap arbitrary groovy code in the script step).

like image 124
burnettk Avatar answered Sep 19 '22 05:09

burnettk