首页 文章

在Jenkins Email-ext插件中不能使用groovy脚本中的“取消”变量

提问于
浏览
0

我遇到了一个问题,当我从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 回答

  • 0

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

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

    这将取消构建:

    changed = false
    files = 5
    cancel = true
    

    这不会:

    changed = false
    cancel = true
    files = 5
    

    这也将 do 取消:

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

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

  • 2

    我有个类似的问题 .
    Pre-send script 我把:

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

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

相关问题