Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throw exception in jenkins pipeline?

I have handled the Jenkins pipeline steps with try catch blocks. I want to throw an exception manually for some cases. but it shows the below error.

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new java.io.IOException java.lang.String 

I checked the scriptApproval section and there is no pending approvals.

like image 853
Yahwe Raj Avatar asked Mar 10 '17 12:03

Yahwe Raj


People also ask

How do you throw an error in Jenkins pipeline?

If you want to abort your program on exception, you can use pipeline step error to stop the pipeline execution with an error. Example : try { // Some pipeline code } catch(Exception e) { // Do something with the exception error "Program failed, please read logs..." }

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.

How do you skip failed stage in Jenkins pipeline?

To ignore a failed step in declarative pipeline you basically have two options: Use script step and try-catch block (similar to previous proposition by R_K but in declarative style)

How do you skip stage in Jenkins scripted pipeline?

So to mark a stage as skipped you need to call static method markStageSkippedForConditional passing the name of the stage you are skipping. If you're using a version of Jenkins older than mid 2019, you must uncheck Use Groovy Sandbox checkbox, since the Utils method was not yet whitelisted for external use.


1 Answers

If you want to abort your program on exception, you can use pipeline step error to stop the pipeline execution with an error. Example :

try {   // Some pipeline code } catch(Exception e) {    // Do something with the exception      error "Program failed, please read logs..." } 

If you want to stop your pipeline with a success status, you probably want to have some kind of boolean indicating that your pipeline has to be stopped, e.g:

boolean continuePipeline = true try {   // Some pipeline code } catch(Exception e) {    // Do something with the exception      continuePipeline = false    currentBuild.result = 'SUCCESS' }  if(continuePipeline) {    // The normal end of your pipeline if exception is not caught.  } 
like image 197
Pom12 Avatar answered Sep 18 '22 14:09

Pom12