Jenkins, pipeline multibranch and exception

123 views
Skip to first unread message

sbru...@gmail.com

unread,
Jul 11, 2018, 4:49:52 AM7/11/18
to Jenkins Users
Hello everybody,

I'm new to the mailing list and new pipeline-multibranch user in Jenkins, I didn't know Groovy at the beginning of the week.

I'm trying to throw an exception in a 'Jenkinsfile' file after calling the 'sh' command.
Example:

if (! sh (script: filename, returnStatus: true)) {
     throw new RuntimeException ("Errrrooooorrrrrrrrrrr!")
}

I still get the following message:
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Java.Lang.String

Is it possible to throw an exception in a Jenkinsfile? What's the problem ?


Regards,
Samuel

Victor Martinez

unread,
Jul 11, 2018, 12:03:13 PM7/11/18
to Jenkins Users
does it work the below snippet?

retCode = sh script: filename, returnStatus: true
if (retCode != 0) {
    error "Errrrooooorrrrrrrrrrr!"
}

Cheers

Samuel Brunel

unread,
Jul 11, 2018, 2:49:05 PM7/11/18
to jenkins...@googlegroups.com
Victor,

It’s ok to throw exception but I need to add 
currentBuild.result = 'FAILURE
to exit with failure from Jenkinsfile.

the documentation is not easy to navigate, impossible to find 'error' for example.


Thanks,

Samuel

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/cf58c5c7-a5dd-45d1-b493-8b3e693abbdb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reinhold Füreder

unread,
Jul 12, 2018, 2:59:30 AM7/12/18
to jenkins...@googlegroups.com

Hi Victor,

 

In case you don’t have to do any special/custom/proprietary additional action/handling, then just

sh script: filename

… should do the trick as well: because by default a non-0 exit code of sh step will exit the pipeline with an error/failure IIRC.

 

HTH Reinhold

Reply all
Reply to author
Forward
0 new messages