2

I have a Jenkins pipeline using Groovy to send the status of the builds via emails when it is failure or unstable. But when i cancel the build manually in Jenkins, the email still send because it take that as a failure. How can i make Jenkins not to send emails in this situation?

Tung Do
  • 73
  • 2
  • 2
  • 12

1 Answers1

-1

You can play with the FlowInterruptedException, for example, my not graceful, but working solution:

import org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
node(){
    stage("doing things"){
        sendEmailflag = true
        try{
            echo "into try block"
            sleep 10
        }
        catch(org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e) {
            sendEmailflag = false
            echo "!!!caused error $e"
            throw e
        }
        finally{
            if(sendEmailflag == false)
                {echo "do not send e-mail"}
            else
                {echo "send e-mail"}
        }
    }
}

this is based on:

Abort current build from pipeline in Jenkins

How to catch manual UI cancel of job in Jenkinsfile

Catching mulitple errors in Jenkins workflow

Sysanin
  • 1,501
  • 20
  • 27