我遇到了一个问题,当我从UNSTABLE状态进入FIXED状态时,我想取消发送电子邮件 . 因此,如果一项工作失败但得到修复,我想发送消息 . 但是如果一个工作不稳定(测试没有通过)并得到修复,请不要发送电子邮件 . 可能是作业在处于FAILURE状态后标记为UNSTABLE . 当它进入SUCCESS(即它是固定的)我想在那种情况下发送电子邮件 . 您可以在下面的代码中看到这些案例 .
我的问题是,当我按照定义[1]将变量 cancel
设置为 true
时,它应该取消电子邮件,但它不会使用"Failure","Still failing"和"Fixed"的触发器 .
詹金斯版本:1.533 . Email-ext版本: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来取消发送电子邮件 . ”
2 回答
我遇到了同样的问题,并在几天内找到了解决方案 .
“取消”仅在最后一行代码中使用时才有效 .
这将取消构建:
这不会:
这也将 do 取消:
我希望这会为某人节省一些时间 .
我有个类似的问题 .
在
Pre-send script
我把:我收到附有
build.log
文件的电子邮件,其中显示"cancel = true"
和"cancel = false"
个案 .