email - 在电子邮件中发送解析的控制台输出

标签 email jenkins hudson logparser email-ext

我正在使用两个 Jenkins 插件,Email-ExtLog Parser .我有我想要的 Log Parser 插件的正则表达式,我想在构建后发送给用户的电子邮件中包含 Log Parser 插件的输出。

Email-Ext 插件可以访问控制台输出,我可以在电子邮件中为控制台输出重写我的正则表达式,但是由于 Log Parser 插件已经完成了艰苦的工作,我希望有某种方法可以拉其输出到电子邮件中。

有没有人知道有什么方法可以做到(比如Jenkins环境变量)?

最佳答案

一位同事告诉我,Jenkins 中的每个构建都有与之相关的“ Action ”,并且 Jenkins 插件通过 Action 来发挥它们的魔力。我可以用 build.getActions() 找到我的所有 Action .然后我循环执行这些操作,直到得到 LogParserAction这是 Jenkins Log Parser 插件提供的操作。

然后我查看了 LogParserAction.class 的源代码找到方法 getErrorLinksFile() .使用这种方法,我能够获取解析日志的文本。一个类似的方法叫做getWarningLinksFile()可用于警告,另一个可用于信息。

然后我循环浏览了 \n 上的文本字符并应用了一些正则表达式以使其看起来像我想要的那样。代码的重要部分如下。如果您在 Notepad++ 中将其视为 HTML 会更好看

%>
    <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>

关于email - 在电子邮件中发送解析的控制台输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8719902/

相关文章:

php - 邮件 PEAR 包不发送电子邮件

c# - AWS/SES 电子邮件发送...您的体验如何?

git - Jenkins Git 构建触发器 : build every intermediate revision, 不只是 HEAD?

jenkins - 使用 Jenkins 管道上的 Email-ext 插件在电子邮件正文中嵌入 html 报告?

c# - SonarQube 分析无法完成,因为找不到分析配置文件

parameters - 从作业仪表板参数化 Hudson 构建

linux - Hudson/Jenkins - 将文件从 linux 传输到 windows

macos - 从 Mail.app 在撰写窗口中获取消息

html - outlook 2007 忽略单元格填充

java - 在 Hudson 中检查(从 VSS)和构建 Maven 项目时出现问题