12

I am working with two Jenkins plugins, Email-Ext and Log Parser. I have the regular expressions for the Log Parser plugin how I want them, and I would like to include the output of the Log Parser plugin in the email that is sent out to users after a build.

The Email-Ext plugin has access to the console output, and I could rewrite my regular expressions for the console output in the email, but since the Log Parser plugin has already done the hard work I was hoping there was some way I could just pull its output into the email.

Does anyone know of any way (like a Jenkins environment variable) this can be done?

Josh Unger
  • 6,717
  • 6
  • 33
  • 55
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179

2 Answers2

1

A co-worker told me that every build in Jenkins has "actions" associated with it and that Jenkins plugins do their magic via actions. I was able to find all actions of my actions with build.getActions(). I then looped through the actions until I got LogParserAction which is the action supplied by the Jenkins Log Parser plugin.

I then looked through the source code of LogParserAction.class to find the method getErrorLinksFile(). With this method I was able to get the text of the parsed log. A similar method called getWarningLinksFile() is available for the warnings and another is available for info.

I then looped through the text on \n character and applied some regexs to make it look how I wanted. Important parts of the code are below. Looks better if you view it as HTML in Notepad++

%>
    <TABLE width="100%">
        <TR>
            <TD class="bg1" colspan="2">ERRORS</TD>
        </TR>
<%
    def publisher = null
    for(iter in project.getPublishersList()){
        if(iter.getDescriptor().getDisplayName().equals("Editable Email Notification")){
            publisher = iter
            break
        }
    }
    if(publisher != null){
        def logParserResult
        //Get the LogParserAction from Jenkins
        for(action in build.getActions()){
            if(action.toString().contains("LogParserAction")){
                //Get the LogParserResult from the LogParserAction
                logParserResult = action.getResult()
                break
            }
        }

        //Get the ErrorLinksFile from the LogParserResult
        def errorLinksFile = new File(logParserResult.getErrorLinksFile())

        //Rewrite the URL so it directs to something useful
        pattern = ~/<a.*?><font.*?>/
        def errorList = []
        for(line in errorLinksFile.getText().split("\n")){
            //All errors have a link, so this makes sure no superfluous text is displayed
            if(!line.contains("href")){
                continue
            }
            errorList.add(line.replaceAll(pattern, "<a href="+ rooturl + build.url + "parsed_console/?\">").minus("</font>"))
        }
%>
        <TR>
            <TD class="bg2" colspan="2">Total : ${errorList.count{it}} error(s)</TD>
        </TR>
<%
        for(error in errorList){
%>
        <TR>
            <TD class="errors" colspan="2">${error}</TD>
        </TR>
<%
        }
%>
    </TABLE>
ubiquibacon
  • 10,451
  • 28
  • 109
  • 179
  • 3
    See [this gist](https://gist.github.com/1566181) for ideas on using more canonical Groovy code; it's untested but pretty close. Just seems a shame to write Groovy like that :( – Dave Newton Jan 05 '12 at 17:14
0

If you are able to pull the logs and write into a file. You can attach that file as an attachment to your email using Email-Ext.

Amey
  • 8,470
  • 9
  • 44
  • 63
  • Is it with Email-Ext also possible to inline the logs? Couldn't find this at documentation? Perhaps somehow via scripting? – Strinder Apr 08 '16 at 08:47
  • 1
    Found the solution: Just add
    ${BUILD_LOG, maxLines=9999, escapeHtml=false}
    to content area
    – Strinder Apr 08 '16 at 08:52