我遇到了一个问题,我想在作业从 UNSTABLE 状态进入 FIXED 状态时取消发送电子邮件。因此,如果工作失败但得到修复,我想发送消息。但是,如果工作不稳定(测试未通过)并且得到修复,请不要发送电子邮件。可能是某个作业在处于 FAILURE 状态后被标记为 UNSTABLE。当它成功(即它已修复)时,我想要一封电子邮件。您可以在下面的代码中看到这些案例。
我的问题是,当我根据定义将变量设置为cancel
[ true
1] 时,它应该取消电子邮件,但事实并非如此。电子邮件每次都会发送。当然,我正在使用“失败”、“仍然失败”和“已修复”的触发器。
詹金斯版本:1.533。电子邮件分机版本:2.37.2.2
// The goal of this script is to block sending the 'FIXED' message
// when the status goes from 'UNSTABLE' to 'SUCCESS'
//
// These are the cases (where F=FAILURE, U=UNSTABLE, S=SUCCESS)
// S - S : no msg (previous state: S, current state: S)
// F - S : msg
// S - U ... U - S : no msg <-- this is the one we need to avoid sending an email
// F - U ... U - S : msg
logger.println("Entering pre-send script")
// variable definitions
def keepGoing= true
def cancelEmail = false
// object to current job
job = hudson.model.Hudson.instance.getItem("incr-build-master")
// current build number
buildNumber = build.getNumber()
logger.println("Current build number: " + buildNumber)
// if the build failed or is unstable don't to anything,
// the specific triggers should take care of the messages
if (build.result.toString().equals("SUCCESS"))
{
logger.println("Build is successful. Procesing...")
while( keepGoing )
{
// get the number of the next past build
pastBuild = job.getBuildByNumber(--buildNumber)
buildResult = pastBuild.result.toString()
switch ( buildResult )
{
case "SUCCESS":
// if the previous non-unstable build was successful
// don't send a 'FIXED' message
cancelEmail = true
keepGoing = false
logger.println("Cancel sending email")
break
case "FAILURE":
// here we exit, but we will send the 'FIXED' message
keepGoing = false
logger.println("Send email")
break
case "UNSTABLE":
// let us keep looking until we find a previous build
// that is either 'SUCCESS' or 'FAILURE*
logger.println("Build " + buildNumber + " is unstable")
break
default:
logger.println("Error in script: result string is wrong - " + buildResult)
return
}
}
}
logger.println("Emailed canceled?: " + cancelEmail)
cancel=cancelEmail
logger.println("Exiting pre-send script")
[1] 来自帮助图标:“您也可以通过将布尔变量“cancel”设置为 true 来取消发送电子邮件。”