Jenkinsfile构建失败邮箱通知

IT
IT
IT
405
文章
1
评论
2021年3月25日00:21:13 评论 3,713 3663字阅读12分12秒

gitlab

  • 配置邮箱或者自己的邮箱可以收到邮件

Jenkinsfile构建失败邮箱通知

Jenkins

  • 安装email插件

Jenkinsfile构建失败邮箱通知

  • 添加邮箱获取参数

Jenkinsfile构建失败邮箱通知

  • 配置Jenkins发邮件,端口使用默认的25

Jenkinsfile构建失败邮箱通知

  • 系统管理员邮箱要和上面的发送邮箱一致

Jenkinsfile构建失败邮箱通知

  • 这里选择html

Jenkinsfile构建失败邮箱通知

  • 构建测试

Jenkinsfile构建失败邮箱通知 Jenkinsfile构建失败邮箱通知

  • jenkinsfile代码
#!groovy

@Library('jenkinslibrary@master') _

//func from shareibrary
def build = new org.devops.build()
def deploy = new org.devops.deploy()
def tools = new org.devops.tools()
def gitlab = new org.devops.gitlab()
def toemail = new org.devops.toemail()
def sonar = new org.devops.sonarqube()
def sonarapi = new org.devops.sonarapi()
def nexus = new org.devops.nexus()
def artifactory = new org.devops.artifactory() 


def runOpts
//env
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"
String deployHosts = "${env.deployHosts}"
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"
String artifactUrl = "${env.artifactUrl}"


if ("${runOpts}" == "GitlabPush"){
    branchName = branch - "refs/heads/"
    
    currentBuild.description = "Trigger by ${userName} ${branch}"
    gitlab.ChangeCommitStatus(projectId,commitSha,"running")
    env.runOpts = "GitlabPush"

    
} else {
   userEmail = "lucky@centoscn.vip"
}


//pipeline
pipeline{
    agent { node { label "master"}}
    
    
    stages{

        stage("CheckOut"){
            steps{
                script{
                   
                    
                    println("${branchName}")
                
                    tools.PrintMes("获取代码","green")
                    checkout([$class: 'GitSCM', branches: [[name: "${branchName}"]], 
                                      doGenerateSubmoduleConfigurations: false, 
                                      extensions: [], 
                                      submoduleCfg: [], 
                                      userRemoteConfigs: [[credentialsId: 'ef208191-527c-4630-a677-b81696446562', url: "${srcUrl}"]]])

                }
            }
        }
        stage("Build"){
            steps{
                script{
                
                    tools.PrintMes("执行打包","green")
                    //build.Build(buildType,buildShell)
                    artifactory.main(buildType,buildShell)
                    artifactory.PushArtifact()
                    
                    //上传制品
                    //nexus.main("nexus")
                    
                    //发布制品
                    //sh " wget ${artifactUrl} && ls "
                    
                    
                    


                    //deploy.SaltDeploy("${deployHosts}","test.ping")
                    //deploy.AnsibleDeploy("${deployHosts}","-m ping ")
                }
            }
       }
       
       
        stage("QA"){
            steps {
                script{
                    tools.PrintMes("搜索项目","green")
                    result = sonarapi.SerarchProject("${JOB_NAME}")
                    println(result)
                    
                    if (result == "false"){
                        println("${JOB_NAME}---项目不存在,准备创建项目---> ${JOB_NAME}!")
                        sonarapi.CreateProject("${JOB_NAME}")
                    } else {
                        println("${JOB_NAME}---项目已存在!")
                    }
                    
                    tools.PrintMes("配置项目质量规则","green")
                    qpName="${JOB_NAME}".split("-")[0]   //Sonar%20way
                    sonarapi.ConfigQualityProfiles("${JOB_NAME}","java",qpName)
                
                    tools.PrintMes("配置质量阈","green")
                    sonarapi.ConfigQualityGates("${JOB_NAME}",qpName)
                
                    tools.PrintMes("代码扫描","green")
                    sonar.SonarScan("test","${JOB_NAME}","${JOB_NAME}","src")
                    

                    sleep 30
                    tools.PrintMes("获取扫描结果","green")
                    result = sonarapi.GetProjectStatus("${JOB_NAME}")
                    
                    
                    println(result)
                    if (result.toString() == "ERROR"){
                        toemail.Email("代码质量阈错误!请及时修复!",userEmail)
                        error " 代码质量阈错误!请及时修复!"
                        
                        
                    } else {
                        println(result)
                    }
                
                

                }
           }
       }
    }
    post {
        always{
            script{
                println("always")
            }
        }
        
        success{
            script{
                println("success")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"success")
                }
                toemail.Email("流水线成功",userEmail)
            
            }
        
        }
        failure{
            script{
                println("failure")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"failed")
                }
                toemail.Email("流水线失败了!",userEmail)
            }
        }
        
        aborted{
            script{
                println("aborted")
                if ("${runOpts}" == "GitlabPush"){
                    gitlab.ChangeCommitStatus(projectId,commitSha,"canceled")
                }
               toemail.Email("流水线被取消了!",userEmail)
            }
        
        }
    
    }
    
    
}
  • 模版代码
package org.devops

//定义邮件内容
def Email(status,emailUser){
    emailext body: """
            <!DOCTYPE html> 
            <html> 
            <head> 
            <meta charset="UTF-8"> 
            </head> 
            <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0"> 
                <img src="https://192.168.90.17:8080/static/0eef74bf/images/headshot.png">
                <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">   
                    <tr> 
                        <td><br /> 
                            <b><font color="#0B610B">构建信息</font></b> 
                        </td> 
                    </tr> 
                    <tr> 
                        <td> 
                            <ul> 
                                <li>项目名称:${JOB_NAME}</li>         
                                <li>构建编号:${BUILD_ID}</li> 
                                <li>构建状态: ${status} </li>                         
                                <li>项目地址:<a href="${BUILD_URL}">${BUILD_URL}</a></li>    
                                <li>构建日志:<a href="${BUILD_URL}console">${BUILD_URL}console</a></li> 
                            </ul> 
                        </td> 
                    </tr> 
                    <tr>  
                </table> 
            </body> 
            </html>  """,
            subject: "Jenkins-${JOB_NAME}项目构建信息 ",
            to: emailUser
        
}
继续阅读
  • 我的QQ
  • QQ扫一扫
  • weinxin
  • 我的头条
  • 头条扫一扫
  • weinxin
IT
  • 本文由 发表于 2021年3月25日00:21:13
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
jenkinsfile集成阿里云镜像仓库 jenkins-ci/cd

jenkinsfile集成阿里云镜像仓库

创建Jenkins认证授权 ID也配置成aliyun-registry-admin 阿里云镜像库有个问题,在设置固定密码后,使用的是本账号名字。本账号的密码和镜像的密码也就不一样了。所以要么单独创建一...

您必须才能发表评论!