0

i have munit html report and integration html report. if in case there is any failure in integration test so report is not generated. and i want to send email notification to only munit test but not integration test. so i tried if condition but i am clueless how to fit double if condition in post action.

it will be really helpful if anyone suggest me a way to solve the above scenario. and i have defined below variables but facing issue with exit variable.

FILE = "${DEMO}/${BUILD_NUMBER}/MunitReport-${BUILD_NUMBER}.html" 
def exists = fileExists 'FILE'
REPO = "${DEMO}/${BUILD_NUMBER}/report/${BUILD_NUMBER}/htmlreport.html"
def exit = fileExists 'REPO'

error:groovy.lang.MissingPropertyException: No such property: exist for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63)

post {
      always{
             script{
        if ((exists))
         emailext attachLog: true, body: "${currentBuild.result}: <h4> MUnit test Results from Below link</h4> <h3>****/job/*****job/********/job/master/${BUILD_NUMBER}/Munit_20Report</h3> <h4>Integration test Results from Below link</h4> <h3>*********/job/********/job/*******/job/master/${BUILD_NUMBER}/Integration_20Test_20Report</h3>"", compressLog: true, replyTo: 'email@xxx.com',
       subject: "Build Notification: ${JOB_NAME}-Build# ${BUILD_NUMBER} ${currentBuild.result}", to: 'email123@xxx.com' 

thanks in advance

2 Answers2

0

If you put a def in front of the exist, it is only available in the local scope and no longer there in the post step. Remove the def and the MissingProperty exception should be resolved.

I didn't completely understand how you want to check these conditions but it sounds like it could be done with boolean logic. Something like:

if(exists && !exit){ ... }
smelm
  • 1,253
  • 7
  • 14
  • in my usecase i have 2 test report one is munit and other is newman report. in fail scenario munit is success and newman is fail and it should generate only munit report in mail not newman report. and now i am facing one more error although job is fail but still receiving both success and failure mail. can you please help me out it will be greatful. – Sandhya .sandy Aug 17 '20 at 13:14
  • my post code : post { unsuccessful { script { if (exists && !file) { emailext attachLog: true, else if (file && !exists) { emailext attachLog: true, } } success { script { if (exists && file) { emailext attachLog: true, } – Sandhya .sandy Aug 17 '20 at 13:23
  • replying to @smelm can you please look into problem ? – Sandhya .sandy Aug 17 '20 at 14:03
  • I am sorry, but that's your job. It seems like you are executing the same email step in every case. My recommendations would be to 1. rename the variables to something meaningful (integrationTestFailed etc) 2. extract the logic for email generation into on or more functions (generateEmailbody(), getAttachment() ). From there it should be easier to troubleshoot the problem – smelm Aug 17 '20 at 14:15
0

i achieved with the below code

 post {
    always {
        archiveArtifacts allowEmptyArchive: true, artifacts: 'munit.html', onlyIfSuccessful: true
        archiveArtifacts allowEmptyArchive: true, artifacts: 'htmlreport.html', onlyIfSuccessful: true
        emailext attachLog: true, attachmentsPattern: 'munit.html,htmlreport.html',         
        body: "<h4> ${currentBuild.currentResult}: </h4> Job: <h4> ${env.JOB_NAME}</h4> build: <h4>${env.BUILD_NUMBER}</h4>\n More info at: <h4>${env.BUILD_URL}</h4>", compressLog: true, subject: "Jenkins Build ${currentBuild.currentResult}", to: "example@gmail.com"
            }
    }   
}