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.
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.
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.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With