sending email for unstable builds

274 views
Skip to first unread message

Faad Sayaou

unread,
Feb 28, 2019, 3:41:50 PM2/28/19
to Jenkins Users
Hi everyone
I am using the extended email plugin for notification when the build fails by using try catch. I will also like to send email when the build is unstable. Below is the structure of my pipeline 
node {
       
   
try
   
{
 
    stage
('Checkout') {
            cleanWs
()
            checkout scm

       
}


        stage
('Restore') {

            sh
"dotnet restore  $proj"

       
}

        stage
('Build') {
            sh
"dotnet restore  $proj"

       
}
        stage
('Unit test') {

           sh
"dotnet test  $test"
       
}
   
}
} catch (err) {
     emailext body
:
            ' ${JOB_NAME} ${BUILD_NUMBER} is failing! Somebody should do something about that. https://jenkins-ma.com/job/Test/${BUILD_NUMBER}/console', subject: 'FAILURE', to: 'someEmail..'
   }

I will like to send not only when the pipeline fails but when the build is unstable. thanks

Sagar Utekar

unread,
Feb 28, 2019, 9:06:53 PM2/28/19
to jenkins...@googlegroups.com
You can check the status of build by using BUILD_STATUS, if it is unstable then send a mail

--
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/2c7db49d-68ef-4c41-8cd2-39ff6855ad83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon Bayer

unread,
Mar 1, 2019, 12:32:14 AM3/1/19
to jenkins...@googlegroups.com
Dear Faad, dear Sagar,
U could check the global variable  'currentBuild.currentResult' in e.g. post build actions. Maybe it suits your needs. Doks: https://opensource.triology.de/jenkins/pipeline-syntax/globals#currentBuild

Best regards


-------- Ursprüngliche Nachricht --------
Von: Sagar Utekar <sagar....@gslab.com>
Datum: 01.03.19 03:06 (GMT+01:00)
Betreff: Re: sending email for unstable builds

Simon Bayer

unread,
Mar 1, 2019, 12:37:04 AM3/1/19
to jenkins...@googlegroups.com
Check This if u need post build actions for different build status: https://jenkins.io/doc/pipeline/tour/post/


-------- Ursprüngliche Nachricht --------
Von: Simon Bayer <sch...@msn.com>
Datum: 01.03.19 06:32 (GMT+01:00)

Faad Sayaou

unread,
Mar 1, 2019, 2:34:58 AM3/1/19
to jenkins...@googlegroups.com
thanks @Simon and @Sagar for your responses.  'currentBuild.currentResult' is definitely what i needed and works as intended. 


For more options, visit https://groups.google.com/d/optout.


--

Best Regards / Mit freundlichen Grüßen,

Faad Sayaou,

Embedded Systems for Mechatronics Masters student | FH Dortmund | Germany

Fachhochschule Dortmund

University of Applied Sciences and Arts

Emailfaad...@gmail.com


Simon Bayer

unread,
Mar 1, 2019, 11:07:33 AM3/1/19
to jenkins...@googlegroups.com
Vers Nice, appreciate your feedback. Glad that We could help you :)


-------- Ursprüngliche Nachricht --------
Von: Faad Sayaou <faa...@gmail.com>
Datum: 01.03.19 08:34 (GMT+01:00)

Mark Lübbehüsen

unread,
Mar 11, 2019, 9:14:40 AM3/11/19
to Jenkins Users
Hi,

its better you send emails in the post action

post {
      always {
           
}
        success {
        }
        unstable {

        
}
        failure {
           
}
aborted {
}
    }

Faad Sayaou

unread,
Mar 12, 2019, 6:31:43 PM3/12/19
to Jenkins Users
thanks for your contribution. Is there any other way of failing of failing the pipeline when unit test stage is unstable? I have something like the following in my code to ignore the failed test but I will like to stop the pipeline and publish unit test result.

I am currently experiencing a strange effect when using the MStest plugin. My test report is only generated when the test is successful but when it fails, no test report is generated. 

node {

        stage('Checkout') {
            cleanWs()
            checkout scm

            return
            skipRemainingStages = true
        }


        stage('Restore') {

            sh "dotnet restore  $proj"

        }

        stage('Build') {
            sh "dotnet publish $proj --output $outputFolder --configuration Release -p:Version=$buildVersion -p:FileVersion=$buildVersion"

       }
        stage ('Unit test') {

               sh  "dotnet restore $UnitTest"
               sh returnStdout: true, script: "dotnet test $UnitTest --logger \'trx;LogFileName=unit_tests.xml\' || true"

               step ([$class: 'MSTestPublisher', testResultsFile:"**/*.xml", failOnError: true, keepLongStdio: true])

        } 
         stage ('publish') {
        }
}

I do not want to publish when the build is unstable. Like I mentioned earlier. I want to stop the build and publish test result. I tried using answers suggested above but could not help in my use-case.any idea?

Mark Lübbehüsen

unread,
Mar 19, 2019, 11:07:16 AM3/19/19
to Jenkins Users
script {
                currentBuild.result = currentBuild.result ?: 'FAILED'
       }

Mark Lübbehüsen

unread,
Mar 19, 2019, 11:07:16 AM3/19/19
to Jenkins Users
HI,

you can fail your pipeline if you put the following code in the unit test stage

script {
                currentBuild.result = currentBuild.result ?: 'FAILED'
          } 

Am Dienstag, 12. März 2019 23:31:43 UTC+1 schrieb Faad Sayaou:

Aaron Digulla

unread,
Mar 19, 2019, 12:12:49 PM3/19/19
to Jenkins Users
Hi,

I'm using this code:

    node(...) {
       
try {
           
...add stages here...
       
} catch(Exception e) {
            currentBuild
.result = 'FAILURE' // Let the post step know that the build failed with an error.
           
throw e
       
} finally {
            builder
.post(currentBuild)
       
}
     
} // node

The code for post() looks like so:

    def post(def currentBuild) {
       
String currentResult = currentBuild.result ?: 'SUCCESS'
        echo
"post ${currentResult}"
       
       
try {
            postAlways
(currentBuild)
       
} catch(Exception e) {
           
def converted = toString(e)
            echo
"postAlways failed (ignored):\n${converted}"
       
}
       
       
if (currentResult == 'SUCCESS') {
            success
(currentBuild)
       
} else {
            failed
(currentBuild, currentResult)
       
}
   
}

Hope this helps,

Aaron Digulla
Reply all
Reply to author
Forward
0 new messages