Jenkins声明性流水线:在后期阶段发送带有测试报告的电子邮件通知

huangapple 未分类评论47阅读模式
英文:

Jenkins Declarative pipeline: send email notification with test report in post stage

问题

pipeline {
agent any
stages {

    stage('Campaign Log') { 
        
        steps {
            echo "RUNNING ${env.BUILD_ID} on ${env.JENKINS_URL}"
            uiVeri5ExecuteTests script: this,
            testRepository: "https://github.wdf.sap.corp/grc-iag-acert/qa.ui.automation.wdf.git",
            gitBranch: "sg-pipeline",
            testOptions: "-v --params.user='iag.administrator.testing@sap.com' --params.pass='Iagadministrator123@\$' ./logs/campaign_log/config.js --baseUrl=https://parrot-one-flp-iag-acert-qa-hcl.cfapps.sap.hana.ondemand.com/cp.portal/site#Shell-home";
        }
    }
    
}

post {  
    failure {  
        mail bcc: '', body: "Details: ${env.JOB_NAME} Build Number: ${env.BUILD_NUMBER} Build: ${env.BUILD_URL} Console Output: ${env.BUILD_URL}/console\n${SCRIPT_RESULT}", cc: '', from: 'noreply+jaas@sap.com', replyTo: '', subject: 'Failing UIVeri5 Tests', to: 'surendra.gurram@sap.com';
    }  
    unstable {  
        mail bcc: '', body: "Details: ${env.JOB_NAME} Build Number: ${env.BUILD_NUMBER} Build: ${env.BUILD_URL} Console Output: ${env.BUILD_URL}/console\n${SCRIPT_RESULT}", cc: '', from: 'noreply+jaas@sap.com', replyTo: '', subject: 'Failing UIVeri5 Tests', to: 'surendra.gurram@sap.com';
    }  
     
}  

}

英文:

I have a simple pipeline script with two stages (test stage and post-stage) in the post-stage, where I send a notification if the build is failed or unstable. However, I am able to send a notification with job name, build number and the console output of that job in the notification but I also need to send the link of the failing test report in the notification itself (I have the test reports in the workspace folder in Jenkins as HTML file)

For this, I have a developed a groovy script (see test.groovy) but I am not sure how to add this to the pipeline script in post-stage in the mail block (in the post-stage)

pipeline script

#!/usr/bin/env groovy
@Library(['piper-lib', 'piper-lib-os']) _
 
 
pipeline {
    agent any
    stages {
        
        stage('Campaign Log') { 
            
            steps {
                echo "RUNNING ${env.BUILD_ID} on ${env.JENKINS_URL}"
                uiVeri5ExecuteTests script: this,
                testRepository: "https://github.wdf.sap.corp/grc-iag-acert/qa.ui.automation.wdf.git",
                gitBranch: "sg-pipeline",
                testOptions: "-v --params.user='iag.administrator.testing@sap.com' --params.pass='Iagadministrator123@\$' ./logs/campaign_log/config.js --baseUrl=https://parrot-one-flp-iag-acert-qa-hcl.cfapps.sap.hana.ondemand.com/cp.portal/site#Shell-home"
            }
        }
        
    }
    
    post {  
         failure {  
             //def console_output = "${env.BUILD_URL}/console" 
             mail bcc: '', body: "Details: ${env.JOB_NAME} Build Number: ${env.BUILD_NUMBER} Build: ${env.BUILD_URL} Console Output: ${env.BUILD_URL}/console", cc: '', from: 'noreply+jaas@sap.com', replyTo: '', subject: 'Failing UIVeri5 Tests', to: 'surendra.gurram@sap.com'
         }  
         unstable {  
             mail bcc: '', body: "Details: ${env.JOB_NAME} Build Number: ${env.BUILD_NUMBER} Build: ${env.BUILD_URL} Console Output: ${env.BUILD_URL}/console", cc: '', from: 'noreply+jaas@sap.com', replyTo: '', subject: 'Failing UIVeri5 Tests', to: 'surendra.gurram@sap.com'
         }  
         
     }  
}


test.groovy (The code I developed to add in post-stage)

test = new File($ {env.BUILD_URL} + '/execution/node/3/ws/logs/campaign_log/target/report/report.html').readLines()[160]

check = test.substring(18, test.length() - 7);

if (0 < check.toInteger()) {
    println($ {env.BUILD_URL} + '/execution/node/3/ws/logs/campaign_log/target/report/report.html');

} else {
    println("this test passed");
}

BTW I am pretty new to Jenkins and pipeline script, and I am stuck here, could someone help me here.

huangapple
  • 本文由 发表于 2020年7月23日 21:36:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/63055625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定