如果詹金斯管道中的电子邮件分机条件

时间:2019-08-29 09:09:55

标签: jenkins jenkins-pipeline jenkins-groovy

如何基于多个项目在电子邮件正文中发送html文件? 使用下面的简单代码能够发送邮件,但是如果我使用“ if else condition”,则它失败了,是否可以使用if条件为格式?

直接代码:

always {
        emailext mimeType: 'text/html',
        body: '${FILE,path="./logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/email_dashboard.html"}',
        subject: '${JOB_NAME} Report',
        to: 'xyz.com'
        }

我正在努力实现以下目标:

post {
    success {
        archiveArtifacts 'logs/${JOB_BASE_NAME}_Build_${BUILD_NUMBER}/**/*, logs/static_results/*'
        script {
            load "logs/${JOB_BASE_NAME}_Build_${BUILD_NUMBER}/display/pipeline_vars.groovy"
        }
    }
    if ((${NODE_NAME}.contains("KG"))&&(${JOB_NAME}.contains("Nightly"))) {
        always {
            emailext mimeType: 'text/html',
            body: '${FILE,path="./logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/email_dashboard.html"}',
            subject: '${JOB_NAME} Report',
            to: 'xyz.com'
        }
    }
    else if ((${NODE_NAME}.contains("MT")) &&(${JOB_NAME}.contains("Feature"))) {
        always {
            emailext mimeType: 'text/html',
            body: '${FILE,path="./logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/email_dashboard.html"}',
            subject: '${JOB_NAME} Report',
            to: 'xyz.com'
        }
    }
}

2 个答案:

答案 0 :(得分:2)

这里的问题是,CreateStyle块内不能always 。使用if-else语法,您将必须在Declarative pipeline块内使用if-else,如下所示:

script

输出:

enter image description here

答案 1 :(得分:0)

以下代码解决了我的问题:

always {
                    script {
                       if (env.JOB_NAME.contains('Nightly'))
                       {
                            emailext (
                                to: '${DEFAULT_RECIPIENTS}',
                                subject: "${env.JOB_NAME}-Report",
                                body: '${FILE,path="./logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/xyz.html"}',
                                attachLog: true,
                                attachmentsPattern: 'logs/${JOB_NAME}_Build_${BUILD_NUMBER}/misc/xyz.png',
                            )
                        }
                    }
                }