Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Testing Hudson/Jenkins仅在连续故障时发出通知_Testing_Selenium_Hudson_Automated Tests_Jenkins - Fatal编程技术网

Testing Hudson/Jenkins仅在连续故障时发出通知

Testing Hudson/Jenkins仅在连续故障时发出通知,testing,selenium,hudson,automated-tests,jenkins,Testing,Selenium,Hudson,Automated Tests,Jenkins,我使用Jenkins以监控的方式运行Selenium测试。故障通常是暂时现象(超时等)。我使用Naginator插件升级项目在失败时的执行,下一个构建通常会通过 因此,我正在寻找使用失败计数的可能性,该计数只允许在测试连续n次失败时发送通知。你知道怎么做吗?丹尼尔,我相信你最好自己写剧本。我有一个很好的解决方案,并在没有任何Java/Groovy经验的情况下独自提出了一个三行解决方案 首先,您需要一种确定构建失败的方法。看看我的问题的解决办法 其次,您需要将失败构建的数量存储在某个地方。项目工作

我使用Jenkins以监控的方式运行Selenium测试。故障通常是暂时现象(超时等)。我使用Naginator插件升级项目在失败时的执行,下一个构建通常会通过


因此,我正在寻找使用失败计数的可能性,该计数只允许在测试连续n次失败时发送通知。你知道怎么做吗?

丹尼尔,我相信你最好自己写剧本。我有一个很好的解决方案,并在没有任何Java/Groovy经验的情况下独自提出了一个三行解决方案

首先,您需要一种确定构建失败的方法。看看我的问题的解决办法

其次,您需要将失败构建的数量存储在某个地方。项目工作区中的文件是明显的位置。使用此代码段作为基础:

def f = new File(manager.build.getWorkspace().getRemote() + '/GroovyFailedBuildsCount.txt')
f.createNewFile()
f.write(text)
第三,你需要发送电子邮件。在我看来,你可以将第一个失败的构建标记为不稳定,当达到限制时,将构建标记为失败,并使用email ext插件仅在失败的构建上发送电子邮件通知


这对我帮助很大。

email ext插件允许您在两个或更多版本的构建状态为“失败”时发送电子邮件。不幸的是,在那之后,它每次都会给你发电子邮件。groovy脚本可能仍然是替代实现:

  • 在出现一定数量的故障后,使用可禁用受监视的作业
  • 创建一个监视作业,检查该监视作业是否已禁用。如果作业被禁用,则监控生成失败并发送电子邮件
  • (可选)禁用监视作业以防止其向您发送电子邮件
  • 如果有人手动禁用您的工作,这也会通知您

    要获取标题包含“标题筛选器”的禁用作业列表,请执行以下操作:


    仅在连续不稳定时发出Jenkins通知

    詹金斯一案。2.65和Groovy Postbuild插件v。2.3.1

  • 将Jenkins通知插件(mail/slack等)设置为仅在构建失败时发送消息

  • 将此Groovy代码添加为作业后期生成操作

    // debugging:
    // manager.build.@result = hudson.model.Result.SUCCESS;
    
    final int threshold = 2;
    
    String jobName = manager.build.project.name;
    def f = new File('/tmp/' + jobName + '_GroovyUnstableBuildsCount.txt');
    
    // debugging:
    // manager.createSummary("info.gif").appendText('path: ' + f.toString(), false, false, false, "red")
    
    if(!f.exists()){ // file don't exists
        f.createNewFile();
    }
    
    // debugging:
    // String fileContent = f.text;
    // manager.createSummary("info.gif").appendText('fileContent: ' + fileContent, false, false, false, "red")
    
    
    if (manager.build.result.isBetterOrEqualTo(hudson.model.Result.SUCCESS)) {
        f.write("0");
    
    } else if (manager.build.result == hudson.model.Result.UNSTABLE) {
    
            int oldValue = 0;
            if(f.text != null && !f.text.empty && f.text != ''){
              oldValue = f.text.toInteger();
            }
        int newValue = oldValue + 1;
    
        if(newValue > threshold){ // trigger send message
            // set build to fail
            manager.build.setResult(hudson.model.Result.FAILURE);
            f.write("0"); // reset counter
    
        } else { // increase count
    
            f.write(newValue.toString());
        }
    }
    

  • 您可以使用jenkins可编辑电子邮件通知插件,在高级设置中,您将有一个failure-X选项,您可以在其中指定发送电子邮件的失败次数。詹金斯的旧版本可能对此没有帮助。因此,您可以在可编辑电子邮件通知中使用以下呈现脚本

    try { //used try catch because NAGINATOR_COUNT will be a undefined property for first 
    build
    cancel = true
    if ($NAGINATOR_COUNT && $NAGINATOR_COUNT == 2) { //where after 2nd retry it will 
    trigger email
     cancel = false
    }
    } catch (e) {
     cancel = true
    }
    

    对于步骤2,我应该如何创建监视作业?我没有看到一个内置的詹金斯项目可以做到这一点。这需要脚本吗?@Zac是的,需要少量脚本。您可以使用,也可以使用。谢谢!我就是这么想的。我将投票表决你的答案!
    try { //used try catch because NAGINATOR_COUNT will be a undefined property for first 
    build
    cancel = true
    if ($NAGINATOR_COUNT && $NAGINATOR_COUNT == 2) { //where after 2nd retry it will 
    trigger email
     cancel = false
    }
    } catch (e) {
     cancel = true
    }