How to catch hand-drawn UI in Jenkinsfile
I tried to find documentation on how the Jenkinsfile pipeline gets an error thrown when a user cancels a job in the jenkins web interface.
I have no approaches post
or try/catch/finally
for work, they only work when something fails in the assembly.
This results in resources not being freed when someone cancels the job.
Today I have a script in a declarative pipeline , eg:
pipeline {
stage("test") {
steps {
parallell (
unit: {
node("main-builder") {
script {
try { sh "<build stuff>" } catch (ex) { report } finally { cleanup }
}
}
}
)
}
}
}
So everything is in blocks catch(ex)
and is finally
ignored when the job is manually canceled from the UI.
source to share
Non-declarative approach:
When you interrupt the pipeline build script, a type exception is thrown org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
. Release resources in catch
and throw the exception.
import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
def releaseResources() {
echo "Releasing resources"
sleep 10
}
node {
try {
echo "Doing steps..."
sleep 20
} catch (FlowInterruptedException interruptEx) {
releaseResources()
throw interruptEx
}
}
Declarative approach:
The same, but inside a block script {}
in steps
stage
. Not the neat solution, but the one I've tested and worked.
source to share