groovy postbuild plugin image

I was working on an interesting project in my company to create an automation suite for our product. Our Jenkins node is hosted in the windows server while the actual automation pipeline should run in a linux box.

One of our engineers has already written a lot of nice power shell scripts to create automation pipeline to run in windows. So my job was to create a similar pipeline but to run in Linux.

I started customizing the powershell scripts to be compatible to linux environment. After I finished my work and gladly handed it over to QA to test, and it came straight back to me with an issue. The issue reported was a genuine problem. The Jenkins did not mark the build to be unstable when powershell threw an exception. Note that my powershell was running inside an ssh enclave in jenkins. An example would be:

powershell script called using shell script.

Now if that pwsh script throws an exception, there is no way jenkins can capture it and mark the build as “UNSTABLE”.

After thinking this through, I thought the best option would be to use a “post build” action to look at the console output and determine if it the build was successful or not. So, I went ahead and found a great plugin called “GROOVY POSTBUILD”

groovy post build plugin

I went ahead to install the plugin and restarted my Jenkins node. I wrote a small little groovy script to find for the text “UNSTABLE BUILD” which I have written to standard output from my powershell scripts whenever an exception is thrown.

powershell script snippet

Here is my Groovy postbuild script:

Groovy script to find text form console output

Now, I thought I had everything setup to run build my jenkins job, well the groovy script failed immediately, with this error when I ran my jenkins job.

Groovy script failure

So there was one more thing I had to do, I had to “Approve” the script in Jenkins. If you navigate to “Manage Jenkins” > “In Process Script Approval” > Approve the method with signature “method org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder$BadgeManager createSummary java.lang.String”.

That is it, You are all SET!!!

I finally ran my Jenkins job, and it marked the build to be “UNSTABLE” exactly how I expected it to behave:

Build step 'Groovy Postbuild' changed build result to UNSTABLE
Finished: UNSTABLE

I hope this helps you if you find yourself in a similar situation.

Thanks for taking time to read my blog and leave your comments or feedback if you liked it.

Leave a Reply

Your email address will not be published.