首页 > 解决方案 > Jenkins 声明式管道:在后期阶段发送带有测试报告的电子邮件通知

问题描述

我有一个简单的管道脚本,在后期有两个阶段(测试阶段和后期),如果构建失败或不稳定,我会在其中发送通知。但是,我可以在通知中发送包含作业名称、内部版本号和该作业的控制台输出的通知,但我还需要在通知本身中发送失败测试报告的链接(我在Jenkins 中的工作区文件夹作为 HTML 文件)

为此,我开发了一个 groovy 脚本(请参阅 test.groovy),但我不知道如何将其添加到邮件块中的 post-stage 中的管道脚本(在 post-stage 中)

流水线脚本

#!/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(我开发的后期添加的代码)

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");
}

顺便说一句,我对 Jenkins 和管道脚本很陌生,我被困在这里,有人可以在这里帮助我。

标签: javajenkinsgroovyjenkins-pipelinejenkins-declarative-pipeline

解决方案


推荐阅读