问题描述
||
我正在使用Jenkins以监视方式运行Selenium测试。故障通常是暂时的现象(超时等)。当Naginator插件发生故障时,我会升级项目的执行,通常会通过下一个构建。
因此,我正在寻找一种使用失败计数的可能性,该计数只能在连续n次测试失败时才发送通知。您知道怎么做吗?
解决方法
丹尼尔,我相信您最好的选择就是自己编写脚本。我遇到了类似的问题,没有使用Java / Groovy的经验就自己提出了三行解决方案。
首先,您需要一种确定构建失败的方法。看到我的问题的解决方案。
其次,您需要将失败的构建数量存储在某个地方。项目工作区中的文件是显而易见的位置。使用此代码段作为基础:
def f = new File(manager.build.getWorkspace().getRemote() + \'/GroovyFailedBuildsCount.txt\')
f.createNewFile()
f.write(text)
第三,您需要发送电子邮件。在我的头顶上,您可以将第一个失败的构建标记为不稳定,并且在达到限制时,将构建标记为失败,并让email-ext插件仅在失败的构建上发送电子邮件通知。
Groovy入门指南对我有很大帮助。
, 替代实现:
在一定数量的故障后,使用“禁用作业”插件来禁用受监视的作业。
创建一个监视作业,以检查是否禁用了监视的作业。如果作业被禁用,则监视构建失败并发送电子邮件。
(可选)禁用监视作业,以防止其向您发送电子邮件。
如果有人手动禁用了您的工作,这也会带来令人高兴的副作用。
要获取标题包含“标题过滤器”的禁用作业的列表,请执行以下操作:
curl -sS http://jenkins.example.com/api/json \\
| jq \'.jobs[] | select(.name | contains(\"title filter\")) | select(.color==\"disabled\") | .name\'
, Jenkins仅在连续的不稳定对象上通知
使用Jenkins 2.65版和Groovy Postbuild插件2.3.1版
将您的Jenkins通知插件(邮件/松弛等)设置为仅在构建失败时发送消息。
将此Groovy代码添加为Job Postbuild Action
// 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,\"red\")
if(!f.exists()){ // file don\'t exists
f.createNewFile();
}
// debugging:
// String fileContent = f.text;
// manager.createSummary(\"info.gif\").appendText(\'fileContent: \' + fileContent,\"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 Editable Email Notification插件,在高级设置中,您将有一个failure-X选项,您可以在其中指定发送电子邮件需要多少次失败。詹金斯的旧版本可能对此无济于事。因此,您可以在可编辑的电子邮件通知中使用以下PRESEND SCRIPT
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
}
http://jenkins-ci.361315.n4.nabble.com/Email-only-after-all-retries-fail-td4713630.html
, 当两个或多个构建的构建状态为“失败”时,email-ext插件使您可以通过电子邮件发送。不幸的是,此后每次都会给您发送电子邮件。一个普通的脚本可能仍然是