0

我遇到了一个问题,我想在作业从 UNSTABLE 状态进入 FIXED 状态时取消发送电子邮件。因此,如果工作失败但得到修复,我想发送消息。但是,如果工作不稳定(测试未通过)并且得到修复,请不要发送电子邮件。可能是某个作业在处于 FAILURE 状态后被标记为 UNSTABLE。当它成功(即它已修复)时,我想要一封电子邮件。您可以在下面的代码中看到这些案例。

我的问题是,当我根据定义将变量设置为cancel[ true1] 时,它应该取消电子邮件,但事实并非如此。电子邮件每次都会发送。当然,我正在使用“失败”、“仍然失败”和“已修复”的触发器。

詹金斯版本: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 来取消发送电子邮件。”

4

2 回答 2

2

我遇到了同样的问题,并在几天内找到了解决方案。

“取消”仅在最后一行代码中使用时才有效。

这将取消构建:

changed = false
files = 5
cancel = true

这不会:

changed = false
cancel = true
files = 5

这也将取消

changed = false
files = 5
if (files > 2) {
    cancel = true
}

我希望这会为某人节省一些时间。

于 2015-02-06T10:44:41.640 回答
0

我有个类似的问题。
Pre-send script我放:

if ((build.getNumber() % 2) == 0)  {
    cancel=true;
} else {
    cancel=false;
}
logger.println("cancel = " + cancel);

我收到一封电子邮件,附有build.log文件,其中显示了"cancel = true"案例"cancel = false"

于 2014-07-01T23:51:39.630 回答