Avoiding job failure email notification when an old job is cancelled by a newer job when entering a throttled stage

89 views
Skip to first unread message

Meiteimacha999

unread,
Feb 3, 2016, 11:50:42 AM2/3/16
to Jenkins Users
i have my workflow set up like so. This sends an email notification when a job fails. 




node("slave1") {
  ws("e:\\jenkins\\workspace\\jobname\\${env.BUILD_NUMBER}") {
  try{
stage name: 'sync', concurrency: 2
  bat '''echo "before sync"
  set a=%RANDOM%
  echo %a%
  echo "after sync"'''
  sleep 10
  
stage name: 'build', concurrency: 1
  bat '''mkdir %cd%\\Build-%BUILD_NUMBER%
  dir %cd%
  cd %cd%\\Build-%BUILD_NUMBER%
  copy /y NUL EmptyFile.txt >NUL'''
  sleep 10

stage name: 'test', concurrency: 2
  bat '''dir %cd%
  dir %cd%\\Build-%BUILD_NUMBER%'''
  sleep 17
}

catch (err){
        stage 'Send Notification'
        def msgimg="https://localhost/images/some.gif"
        def newurl="${env.BUILD_URL}console"
        mail (to: 'adminj...@somename.com',
             subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) has had an error.",
             body: "Ran on <b>${env.NODE_NAME}</b><br>job failed <a href=${newurl}>${env.JOB_NAME}</a>. Some more message.... .<br><img src=${msgimg} />",
             mimeType:'text/html');
        currentBuild.result = 'FAILURE'
    }

}
}


But the issue, is i am also getting email notice for jobs that were cancelled due to the newer job catching up with the older job while running concurrently. I just want email notice for jobs that actually failed due to some error and not be notified for jobs that were marked failed as they were canceled by a presiding job.  
Have anyone handled this issue? 








Daniel Beck

unread,
Feb 3, 2016, 11:58:40 AM2/3/16
to jenkins...@googlegroups.com
Have you tried looking more closely at the exception 'err' to see whether they're different based on how the build was failed/aborted?
> --
> You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/dc1ef652-5f53-44a0-94d3-4ae0626b0dfc%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Meiteimacha999

unread,
Feb 3, 2016, 12:17:21 PM2/3/16
to Jenkins Users, m...@beckweb.net
how do i check the difference? is there a way to print it in Windows? 

Meiteimacha999

unread,
Feb 3, 2016, 2:39:43 PM2/3/16
to Jenkins Users, m...@beckweb.net
okay - so when a job fails, it throws the exception: hudson.AbortException: script returned exit code 1
And when its cancelled by the new job, it throws this: org.jenkinsci.plugins.workflow.steps.FlowInterruptedException

I am not a groovy man, so any help to put a condition inside the catch (like an IF statement) will be helpfull

Meiteimacha999

unread,
Feb 3, 2016, 5:01:29 PM2/3/16
to Jenkins Users, m...@beckweb.net
Almost there but still not perfect yet:

node {
try {
// some stages for the workflow
}

catch (org.jenkinsci.plugins.workflow.steps.FlowInterruptedException e){

        echo "the job was cancelled or aborted"
 
}
 catch (err){ 
>         stage 'Send Notification' 
>         def msgimg="https://localhost/images/some.gif
>         def newurl="${env.BUILD_URL}console" 
>         mail (to: 'adminj...@somename.com', 
>              subject: "Job '${env.JOB_NAME}' (${env.BUILD_NUMBER}) has had an error.", 
>              body: "Ran on <b>${env.NODE_NAME}</b><br>job failed <a href=${newurl}>${env.JOB_NAME}</a>. Some more message.... .<br><img src=${msgimg} />", 
>              mimeType:'text/html'); 
>         currentBuild.result = 'FAILURE' 
>     } 

}

I can get the catch the interrupted job and it echos the string. But for any other exceptions i was expecting it will be caught by the other. But it doesn't. 

Craig Rodrigues

unread,
Feb 3, 2016, 10:10:40 PM2/3/16
to Jenkins Users
I have a big try / catch / finally block in the script.
I explicitly set currentBuild.result in that script, and
check to see if currentBuild.result is equal to "ABORTED".

I don't know if there is a better way to do it,
but that's what I got to work.
--
Craig

On Wed, Feb 3, 2016 at 8:50 AM, Meiteimacha999 <nirish...@gmail.com> wrote:

i have my workflow set up like so. This sends an email notification when a job fails. 


Reply all
Reply to author
Forward
0 new messages