我正在使用Jenkins以监控方式运行Selenium测试。失败通常是暂时现象(超时等)。我在使用Naginator插件失败时升级项目执行,下一次构建通常会通过。
因此,我正在寻找一种使用失败计数的可能性,这种失败计数只有在连续n次测试失败时才能发送通知。你知道怎么能这样做吗?
答案 0 :(得分:5)
丹尼尔,我相信你最好的选择就是自己编写脚本。我有一个similar problem,并且在没有任何Java / Groovy经验的情况下自己想出了一个三行解决方案。
首先,您需要一种方法来确定构建失败。请参阅我的问题解决方案。
其次,您需要在某处存储失败构建的数量。项目工作区中的文件是显而易见的位置。使用此代码段作为基础:
def f = new File(manager.build.getWorkspace().getRemote() + '/GroovyFailedBuildsCount.txt')
f.createNewFile()
f.write(text)
第三,您需要发送电子邮件。在我的头脑中你可以将第一个失败的构建标记为不稳定,当达到限制时,将构建标记为失败,并使用email-ext插件仅在失败的构建上发送电子邮件通知。
Groovy getting started guide对我有很大的帮助。
答案 1 :(得分:1)
另一种实施方式:
如果有人手动禁用您的工作,还通知您,这会带来快乐的副作用。
获取标题包含“标题过滤器”的已禁用作业列表:
curl -sS http://jenkins.example.com/api/json \
| jq '.jobs[] | select(.name | contains("title filter")) | select(.color=="disabled") | .name'
答案 2 :(得分:1)
Jenkins仅在连续不稳定的情况下发出通知
使用Jenkins v.2.65和Groovy Postbuild Plugin v.2.3.1
设置Jenkins通知插件(mail / slack等),仅在构建失败时才发送消息。
将此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, 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());
}
}
答案 3 :(得分:1)
您可以使用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
答案 4 :(得分:0)
email-ext插件允许您在两个或更多版本的构建状态为“失败”时发送电子邮件。不幸的是,它会在那之后每次都给你发电子邮一个时髦的脚本可能仍然是你的