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