Fail Jenkins Build based on build output and email failed log sections

user2094679 picture user2094679 · Mar 19, 2013 · Viewed 9.7k times · Source

I want to make the build fail by analyzing the results of the console log, for a mac project I am building. It builds each module and gives the results like this:

###################################
XX XXX XXX
##########################

It then returns BUILD FAILED

I want to show that the build failed at the end of the console output, and at the same time I want to know which module failed. It should be sent by email since I am already using the email-ext plugin.

I am unsure of what needs to be done; I am aware of text-finder, log parser and setting the run condition - but do not know what steps to follow.

Answer

CIGuy picture CIGuy · Mar 19, 2013

You will need to use the text-finder plugin and the email-ext plugin together in order to accomplish both your objectives.

First set up the text-finder plugin and provide an appropriate regex. Something like: .*(?i)failed.*|.*(?i)error.* would find the words "failed" or "error" in a case insensitive manner. You will need to specify a path to your log files and probably want to check the "Also search console output" check box.

This will cause any build which outputs "failed" or "error" to fail in Jenkins.

Your requirement to email the module which failed is a little more complex, but possible with the email-ext plugin. This plugin allows you to specify a regex which is used to gather email content using a special token which accepts arguments. The full argument list and token name is: ${BUILD_LOG_REGEX, regex, linesBefore, linesAfter, maxMatches, showTruncatedLines, substText, escapeHtml, matchedLineHtmlStyle}

Most of these arguments are optional, something like this should do the trick for you: ${BUILD_LOG_REGEX, regex=".*(?i)failed.*|.*(?i)error.*", linesBefore=10, linesAfter=10}. Put this in the "Default Content" section of the email-ext configuration. You can specify multiple tokens as well, see this answer for instructions to get the full list: Jenkins Email-ext plugin - tokens

You can of course edit the LinesBefore and LinesAfter parameters to suit your needs.