"取消"在Jenkins Email-ext插件中不能使用Groovy脚本中的变量

时间:2014-06-03 11:43:46

标签: groovy jenkins email-ext

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

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

詹金斯版本: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 个答案:

答案 0 :(得分:2)

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

&#34;取消&#34;仅在最后一行代码中使用时才有效。

这将取消构建:

changed = false
files = 5
cancel = true

这不会:

changed = false
cancel = true
files = 5

这也将取消:

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

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

答案 1 :(得分:0)

我遇到了类似的问题 在我提出的Pre-send script中:

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

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