Jenkins Job Status Is Not What It Was Before!
What is this all about?
I had few use-cases where I needed to change the Jenkins build status to be different than the one that the build-flow produced. Here are the main 2 use cases:
- Failed maven builds using -T are showing up as Aborted (JENKINS-24832)
- A validation-step (don’t allow Jira merged issues) in the job fails the build but I was asked to set that status of the build - for this case - to be UNSTABLE (instead of FAILED) - for statistics reasons
The TextFinder (so called…) solution
I’ve asked around (including a web search) for a solution which will help me changed the status of the build for those use-cases (and for some other) and I’ve got the same answer: use the TextFinder plugin.
So I did!
But it didn’t change the status as I’ve expected. It turn out that that even that the plugin finds the requested text (I see that in the log), it can’t change the status if the produced status is different than SUCCESS.
Because that in the above use-cases the status is FAILED or ABORTED, it remains the same after the TextFinder atempt to change it. Looking at the plugin description show that indeed it (quote): downgrade a “successful” build to be unstable or a failure.
The groovy (groovy indeed!!!) solution
So I’ve decided to look for a programatic solution, and to write a groovy script that will change the build status for me. I’ve added a Groovy PostBuild step (Click the Add PostBuild Action button and select it from the list) and put the following code in it:
See GitHub Gist - if you want to copy-paste the code…
For the Maven failed-but-reported-Aborted build, I’ve put this code:
And for the failed-but-should-be-reported-as-unstable build, I’ve put this code:
We will contact you as soon as possible.