Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try/catch/finally masks Jenkinsfile problems in case of groovy compiler exceptions

Tags:

I have code similar to the one below in my Jenkinsfile:

node {    checkout scm    // do some stuff    try {        // do some maven magic    } catch (error) {        stage "Cleanup after fail"        emailext attachLog: true, body: "Build failed (see ${env.BUILD_URL}): ${error}", subject: "[JENKINS] ${env.JOB_NAME} failed", to: '[email protected]'        throw error    } finally {        step $class: 'JUnitResultArchiver', testResults: '**/TEST-*.xml'    } } 

If the above code fails because of some jenkins-pipeline related errors in the try { } (e.g. using unapproved static method) the script fails silently. When I remove the try/catch/finally I can see the errors. Am I doing something wrong? Shouldn't rethrowing error make the pipeline errors appear in the log?

EDIT: I've managed to nail down the problem to groovy syntax, when e.g. I use a variable that hasn't been assigned yet. Example: echo foo If foo is not declared/assigned anywhere Jenkins will fail the build and won't show the reason if it is inside the try/catch/finally which rethrows the exception.

like image 347
Krzysztof Krasoń Avatar asked May 16 '16 11:05

Krzysztof Krasoń


People also ask

Is Jenkinsfile written in Groovy?

The Jenkinsfile is written using the Groovy Domain-Specific Language and can be generated using a text editor or the Jenkins instance configuration tab. The Declarative Pipelines is a relatively new feature that supports the concept of code pipeline.

Does Jenkins support Groovy?

Jenkins features a Groovy script console which allows one to run arbitrary Groovy scripts within the Jenkins controller runtime or in the runtime on agents.

How do you use try catch 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.


1 Answers

This happens when an additional exception is thrown inside the finally block or before the re-throw inside catch. In these cases the RejectedAccessException is swallowed and script-security does not catch it.

like image 130
amuniz Avatar answered Oct 20 '22 01:10

amuniz