0
votes

I'm having an issue where I want to cancel sending an email when a job gets into the FIXED state from the UNSTABLE state. So if a job was failing but got fixed, I want to send the message. But if a job is unstable (tests not passing) and gets fixed, don't send the email. It might be that a job is marked UNSTABLE after it has been in FAILURE state. When it goes to SUCCESS (i.e. it is fixed) I want an email in that case. You can see the cases in the code below.

My problem is that when I set the variable cancel to true by definition[1] it should cancel the email, but it doesn't. The email gets sent every time. Of course I'm using triggers for "Failure", "Still failing" and "Fixed".

Jenkins version: 1.533. Email-ext version: 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] from the Help icon: "You may also cancel sending the email by setting the boolean variable "cancel" to true."

2
Strangely it's working now. I had to reboot Jenkins for another issue, and now it's following the logic. I'll leave this open in case someone has a good explanation of why this happened.perrocontodo

2 Answers

2
votes

I encountered the same problem and found solution in a couple of days.

"cancel" only works when it is used in the last line of code.

This will cancel the build:

changed = false
files = 5
cancel = true

This will not:

changed = false
cancel = true
files = 5

And this will also do cancel:

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

I hope this will save some time for somebody.

0
votes

I am having a similar problem.
In the Pre-send script I put:

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

I get e-mail, with the build.log file attached, that shows the "cancel = true" and "cancel = false" cases.