Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins Pipeline: Try/catch inside a retry block

I have problems with my Jenkins pipeline. I want to do the following: if the Build Environment already exists in the Workspace then execute a (incremental) build using the previous Environment.

If that fails retry again with a clean build (delete previous Build Environment, then build again)

I am trying to do this by:

retry(1) {
        try {
            prepareEnvironment()
            setupBuildEnvironment() // sets up environment if it is not present yet
            runBuild()
        } catch (e) {
            echo 'Err: Incremental Build failed with Error: ' + e.toString()
            echo '     Trying to build with a clean Workspace'
            removeOldBuildEnvironment()
        } finally {
            cleanupEnvironment()
        }
    }

When I run this in Jenkins and the runBuild() step fails I get:

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

I think the problem is the try/catch inside the retry(1) block. Any suggestions how to fix that

I also tried it without the retry. Here I had the problem that if the

bat "..." 

step inside runBuild() returns a exception the pipeline Stage is marked as fail even if I catch the exception and the clean build is a success.

Any suggestions?

like image 654
Ditschi Avatar asked Jul 15 '16 09:07

Ditschi


People also ask

Can we use try catch in Jenkins pipeline?

The key is to put try... catch in a script block in declarative pipeline syntax. Then it will work. This might be useful if you want to say continue pipeline execution despite failure (eg: test failed, still you need reports..)

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.

What is cleanWs () in Jenkins?

cleanWs : Delete workspace when build is done.

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)


1 Answers

The error message is not because the try/catch but because of throwing exception in the prepareEnvironment, setupBuildEnvironment or runBuild methods, like throw new Exception("message") which is not allowed in sandbox mode. What is allowed is using the error step like so:

def runBuild() {
    // ...
    error "ERR"
}

This step will throw a hudson.AbortException with the message specified.

The catch block in you code will catch this exception and print the proper message then invoke removeOldBuildEnvironment

On the other hand with this modification done your script will still not work as expected as the exception is swallowed by the catch block, so the retry step will not execute the code again. To make it work the exception needs to be thrown again from the catch block (note that you have to use retry(2) instead of retry(1))

retry(2) {
    try {
        prepareEnvironment()
        setupBuildEnvironment() // sets up environment if it is not present yet
        runBuild()
    } catch (e) {
        echo 'Err: Incremental Build failed with Error: ' + e.toString()
        echo '     Trying to build with a clean Workspace'
        removeOldBuildEnvironment()
        throw e
    } finally {
        cleanupEnvironment()
    }
}
like image 175
Gergely Toth Avatar answered Sep 22 '22 14:09

Gergely Toth