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?
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..)
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.
cleanWs : Delete workspace when build is done.
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)
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()
}
}
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