jenkins(二十一):Jenkins-pipeline邮件管理

Jenkins-pipeline邮件管理

 

Jenkins-pipeline也可以发邮件啦,网上很多都是简单的通知,这里提供一个Extended E-mail Notification 方式的

注意:Git系统变量不知道是HTML还是pipeline没有解析,这个花了一天时间没解决,无奈直接写明。自己太菜了,也不想接着花时间,有缘人解决的告诉我啊,314144952@qq.com

 

效果如下

 

 

script脚本方式

node ("master"){
    parameters {string(name:'Jenkins',defaultValue:'Hello',description:'How should I greet the world')}
    echo  "${params.nane} 你好!"

    gitlabCommitStatus {
        stage('第1步拉代码'){
            echo "拉代码"
            git credentialsId: '03fd8295-c536-4871-9794-1c37394676e0', url: 'git@gitlab.corp.XXX.com:wangxu/ops.git'
        }
        stage('第2步编译'){
            echo "编译"
           // sh "source /etc/profile && /usr/local/maven/bin/mvn clean compile"
        }
        stage('第3步打包'){
            echo "打包,有一个mail模块是系统级别的,需要sudo"
            // sh "sudo /usr/local/maven/bin/mvn package"
            // echo "完成后 修改一下权限,否则下一次麻烦"
            // sh "sudo chown -R  jenkins: ."
            // sh "find -name '*SNAPSHOT.jar' "
        }
        stage('第四步单元测试'){
            echo "单元测试"
            //sh "sudo /usr/local/maven/bin/mvn test"
        }
        stage("放到下载服务器上"){
            echo "发送文件"
          //  sh "sudo cp ./account-email/target/account-email-1.0.0-SNAPSHOT.jar /home/admin/webserver/html/download && sudo chown  -R admin: /home/admin/webserver/html/download"
        }
    }
        stage("发送邮件"){
        //部分解析不出的环境变量,我在此设置
            def git_url = 'git@gitlab.corp.XXX.com:wangxu/ops.git'
            def branch = 'dev'
            def username = 'Jenkins'
            echo "Hello Mr.${username}"
            echo "GIT路径: ${git_url}"
  
            echo "发送邮件"
            emailext body: '''
            <!DOCTYPE html>
            <html>
            <head>
            <meta charset="UTF-8">
            <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
            </head>
             
             
            <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                    <tr>
                        <td>(本邮件由程序自动下发,请勿回复!)</td>
                    </tr>
                    <tr>
                        <td>
                            <h2><font color="#FF0000">构建结果 - ${BUILD_STATUS}</font></h2>
                        </td>
                    </tr>
                    <tr>
                        <td><br />
                            <b><font color="#0B610B">构建信息</font></b>
                            <hr size="2" width="100%" align="center" />
                        </td>
                    </tr>
                    <tr><a href="${PROJECT_URL}">${PROJECT_URL}</a>
                        <td>
                            <ul>
                                <li>项目名称:${PROJECT_NAME}</li>
                                <li>GIT路径:<a href="git@gitlab.corp.XXX.com:wangxu/ops.git">git@gitlab.corp.XXX.com:wangxu/ops.git</a></li>   
                                <li>GIT分支: master</li>
                                <li>构建编号:${BUILD_NUMBER}</li>                    
                                <li>触发原因:${CAUSE}</li>   
                                <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                            </ul>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b><font color="#0B610B">变更信息:</font></b>
                           <hr size="2" width="100%" align="center" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <ul>
                                <li>上次构建成功后变化 :  ${CHANGES_SINCE_LAST_SUCCESS}</a></li>
                            </ul>    
                        </td>
                    </tr>
             <tr>
                        <td>
                            <ul>
                                <li>上次构建不稳定后变化 :  ${CHANGES_SINCE_LAST_UNSTABLE}</a></li>
                            </ul>    
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <ul>
                                <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                            </ul>    
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <ul>
                                <li>变更集:${JELLY_SCRIPT,template="html"}</a></li>
                            </ul>    
                        </td>
                    </tr>
                    <!--
                    <tr>
                        <td>
                            <b><font color="#0B610B">Failed Test Results</font></b>
                            <hr size="2" width="100%" align="center" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
                            <br />
                        </td>
                    </tr>
                    
                    <tr>
                        <td>
                            <b><font color="#0B610B">构建日志 (最后 100行):</font></b>
                            <hr size="2" width="100%" align="center" />
                        </td>
                    </tr>-->
                    <!-- <tr>
                        <td>Test Logs (if test has ran): <a
                            href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a>
                            <br />
                        <br />
                        </td>
                    </tr> -->
                    <!--
                    <tr>
                        <td>
                            <textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG, maxLines=100,escapeHtml=true}</textarea>
                        </td>
                    </tr>-->
                    <hr size="2" width="100%" align="center" />
             
                </table>
             
             
            </body>
            </html>


    ''', subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', to: '314144952@qq.com'
    }

}
View Code

 

Declarative陈述式

pipeline{
    agent{label 'master'}
    environment {
        git_url = 'git@gitlab.corp.XXX.com:wangxu/ops.git'
        git_key = '03fd8295-c536-4871-9794-1c37394676e0'
        git_branch = 'master'
    }
// triggers { pollSCM('* * * * *') }
    stages {
   
        stage('下载代码') {
            steps {
                echo "拉代码"
                git credentialsId: '03fd8295-c536-4871-9794-1c37394676e0', url: 'git@gitlab.corp.XXX.com:wangxu/ops.git'
            }
        }
    }
    
    post { 
        always{
        //always部分 pipeline运行结果为任何状态都运行

            script{
                emailext body: 
                    '''  <!DOCTYPE html>
            <html>
            <head>
            <meta charset="UTF-8">
            <title>${ENV, var="JOB_NAME"}-第${BUILD_NUMBER}次构建日志</title>
            </head>
             
             
            <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                    <tr>
                        <td>(本邮件由程序自动下发,请勿回复!)</td>
                    </tr>
                    <tr>
                        <td>
                            <h2><font color="#FF0000">构建结果 - ${BUILD_STATUS}</font></h2>
                        </td>
                    </tr>
                    <tr>
                        <td><br />
                            <b><font color="#0B610B">构建信息</font></b>
                            <hr size="2" width="100%" align="center" />
                        </td>
                    </tr>
                    <tr><a href="${PROJECT_URL}">${PROJECT_URL}</a>
                        <td>
                            <ul>
                                <li>项目名称:${PROJECT_NAME}</li>
                                <li>GIT路径:<a href="git@gitlab.corp.XXX.com:wangxu/ops.git">git@gitlab.corp.XXX.com:wangxu/ops.git</a></li>   
                                <li>GIT分支: master</li>
                                <li>构建编号:${BUILD_NUMBER}</li>                    
                                <li>触发原因:${CAUSE}</li>   
                                <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                            </ul>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b><font color="#0B610B">变更信息:</font></b>
                           <hr size="2" width="100%" align="center" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <ul>
                                <li>上次构建成功后变化 :  ${CHANGES_SINCE_LAST_SUCCESS}</a></li>
                            </ul>    
                        </td>
                    </tr>
             <tr>
                        <td>
                            <ul>
                                <li>上次构建不稳定后变化 :  ${CHANGES_SINCE_LAST_UNSTABLE}</a></li>
                            </ul>    
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <ul>
                                <li>历史变更记录 : <a href="${PROJECT_URL}changes">${PROJECT_URL}changes</a></li>
                            </ul>    
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <ul>
                                <li>变更集:${JELLY_SCRIPT,template="html"}</a></li>
                            </ul>    
                        </td>
                    </tr>
                    <!--
                    <tr>
                        <td>
                            <b><font color="#0B610B">Failed Test Results</font></b>
                            <hr size="2" width="100%" align="center" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <pre style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">$FAILED_TESTS</pre>
                            <br />
                        </td>
                    </tr>
                    
                    <tr>
                        <td>
                            <b><font color="#0B610B">构建日志 (最后 100行):</font></b>
                            <hr size="2" width="100%" align="center" />
                        </td>
                    </tr>-->
                    <!-- <tr>
                        <td>Test Logs (if test has ran): <a
                            href="${PROJECT_URL}ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip">${PROJECT_URL}/ws/TestResult/archive_logs/Log-Build-${BUILD_NUMBER}.zip</a>
                            <br />
                        <br />
                        </td>
                    </tr> -->
                    <!--
                    <tr>
                        <td>
                            <textarea cols="80" rows="30" readonly="readonly" style="font-family: Courier New">${BUILD_LOG, maxLines=100,escapeHtml=true}</textarea>
                        </td>
                    </tr>-->
                    <hr size="2" width="100%" align="center" />
             
                </table>
             
             
            </body>
            </html>
''', 
                subject: '$PROJECT_NAME - Build # $BUILD_NUMBER - $BUILD_STATUS!', to: '314144952@qq.com'
            }
            }

            success {
                //当此Pipeline成功时打印消息
                echo 'success'
            }
            failure {
                //当此Pipeline失败时打印消息
                echo 'failure'
            }
            unstable {
                //当此Pipeline 为不稳定时打印消息
                echo 'unstable'     
            }
            aborted {
                //当此Pipeline 终止时打印消息
                echo 'aborted'  
            }
            changed {
                //当pipeline的状态与上一次build状态不同时打印消息
                echo 'changed'          
            }        
        
    }
    
}
View Code

 

 

.

posted on 2019-07-09 13:19  光阴8023  阅读(1290)  评论(0编辑  收藏  举报