Jenkins GitLab集成 邮件通知_html

 

邮件通知反馈  配置GitLab用户邮箱


Jenkins需要拿到用户的邮箱,才能进行邮件通知。

默认情况可能每个Gitlab用户没有配置邮箱的, 需要Gitlab用户要配置好邮箱。 点击头像进入 edit profile​ 

Jenkins GitLab集成 邮件通知_jenkins_02

在这个页面配置好邮箱地址,最好这几个email都配置上吧......最后(这里呢,不要使用admin账号,创建一个devop的账号,修改密码后再设置邮箱)

Jenkins GitLab集成 邮件通知_html_03

账号创建好之后在admin用户让devop用户加入group,这样才能对项目有权限

Jenkins GitLab集成 邮件通知_git_04

checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '7c74af64-1774-4031-bc5f-0e97de40b2cd', url: 'http://139.198.166.235:81/dev/devops-hello-service.git']]])

Jenkins解析Gitlab的POST参数来获取用户邮箱地址, 我们这里获取到然后使用 ​​currentBuild​​ 来增加描述信息。这样更加醒目直观的看到此次构建的主要信息。

此段代码没有加到Pipeline{}中, 直接写就可以运行的。 

def credentialsId = "7c74af64-1774-4031-bc5f-0e97de40b2cd"


webHookData = readJSON text: "${webHookData}"
env.userName = webHookData["user_username"]
env.userEmail = webHookData["user_email"]
env.branchName = webHookData["ref"] - "refs/heads/"
env.commitID = webHookData["checkout_sha"]
env.git_http_url = webHookData["project"]["git_http_url"]

currentBuild.displayName = env.commitID
currentBuild.description = "Trigger by user ${env.userName} \n branch: ${env.branchName}"


pipeline {
agent any

stages {
stage('CheckOut') {
steps {
println("project git url is ${env.git_http_url} ")
println("the user email is ${userEmail}")
checkout([$class: 'GitSCM',
branches: [[name: "${env.branchName}"]],
extensions: [], userRemoteConfigs:
[[credentialsId: "${credentialsId}",
url: 'http://139.198.166.235:81/dev/devops-hello-service.git']]])
}
}
}
}

Jenkins GitLab集成 邮件通知_git_05

可以看到在Jenkins里面拿到用户的邮箱了 

[Pipeline] echo (hide)
the user email is 1239683670@qq.com

安装一个Email插件 

Jenkins GitLab集成 邮件通知_json_06

​Email Extension | Jenkins plugin​

Jenkins GitLab集成 邮件通知_json_07

#上传插件到这个目录下面,重启生效

[root@jenkins-master plugins]# pwd
/data/cicd/jenkins/plugins

[root@jenkins-master plugins]# docker restart jenkins
jenkins

然后进入系统管理-> ​​系统设置​​ , 先配置下全局的admin的邮箱地址。(最后配置下不然可能会出错的)

要将管理员的邮箱改一下,也就是机器人账号,这个是Jenkins整个对外的邮箱账号。

Jenkins GitLab集成 邮件通知_git_08

 ->​​Extended E-email Notification​​。设置邮件系统配置信息。在邮件这块登入邮箱拿到授权码。

Jenkins GitLab集成 邮件通知_html_09

发送短信之后获取授权码

Jenkins GitLab集成 邮件通知_html_10

这里我使用的是QQ邮箱,填写SMTP服务器地址​​smtp.qq.com​​​ 和端口 ​​465​​注意要开启SSL,密码为授权码。这里授权码就是密码

Jenkins GitLab集成 邮件通知_json_11

换个选项注意下: 不选择 ​​HTML​​​ 就是普通的文本, ​HTML​​ 可以支持html网页,更加美观。这里选择 ​​HTML​​ 

Jenkins GitLab集成 邮件通知_jenkins_12

这块邮箱就配置成功了

使用 Pipeline Utility Steps Plugin,您可以使用readJSON函数.

java.lang.NoSuchMethodError: No such DSL method 'readJSON' found among steps [archive, bat, build, catchError, checkout, deleteDir, dir, echo, emailext, emailextrecipients,

Jenkins GitLab集成 邮件通知_git_13

pipeline as code , 进入片段生成器,生成邮件通知代码。 

Jenkins GitLab集成 邮件通知_html_14

Jenkins GitLab集成 邮件通知_json_15

 jenkins as code 将email 写成一个函数。这个通知信息是一个html格式的。 

webHookData = readJSON text: "${webHookData}"
env.userName = webHookData["user_username"]
env.userEmail = webHookData["user_email"]
env.branchName = webHookData["ref"] - "refs/heads/"
env.commitID = webHookData["checkout_sha"]
env.git_http_url = webHookData["project"]["git_http_url"]

currentBuild.displayName = env.commitID
currentBuild.description = "Trigger by user ${env.userName} \n branch: ${env.branchName}"


pipeline {

agent {
label 'build'
}

stages {
stage('CheckOut') {
steps {
println("project git url is ${env.git_http_url} ")
println("the user email is ${env.userEmail}")
checkout([$class: 'GitSCM',
branches: [[name: "${env.branchName}"]],
extensions: [], userRemoteConfigs:
[[credentialsId: "${credentialsId}",
url: 'http://139.198.166.235:81/dev/devops-hello-service.git']]])
}
}
}


post{
always{
script{
//emailext body: 'hello jenkins.....', subject: 'test........', to: '1239683670@qq.com'
EmailUser(userEmail,currentBuild.currentResult)
}
}

}

}



def EmailUser(userEmail,status){
println("the userEmail is: ${userEmail} status: ${status}")
emailext body: """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
<img src="http://192.198.166.235: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: "${userEmail}"

}
Generic Cause
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] readJSON
[Pipeline] node
Running on build-01 in /data/cicd/jenkinsagent/workspace/pipeline-pullcode-email
[Pipeline] {
[Pipeline] stage
[Pipeline] { (CheckOut)
[Pipeline] echo
project git url is http://76c03cb0fea1/dev/devops-hello-service.git
[Pipeline] echo
the user email is 1239683670@qq.com
[Pipeline] checkout
The recommended git tool is: NONE
using credential 11da4458-0df7-4077-8e4d-a902b0b79eb8
Fetching changes from the remote Git repository
Checking out Revision ef8d7d847fd1978da6218353b80718fd1d66751e (origin/master)
Commit message: "Initial commit"
> git rev-parse --resolve-git-dir /data/cicd/jenkinsagent/workspace/pipeline-pullcode-email/.git # timeout=10
> git config remote.origin.url http://139.198.166.235:81/dev/devops-hello-service.git # timeout=10
Fetching upstream changes from http://139.198.166.235:81/dev/devops-hello-service.git
> git --version # timeout=10
> git --version # 'git version 2.7.3'
using GIT_ASKPASS to set credentials gitlab-devop
> git fetch --tags --progress http://139.198.166.235:81/dev/devops-hello-service.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse origin/master^{commit} # timeout=10
> git config core.sparsecheckout # timeout=10
> git checkout -f ef8d7d847fd1978da6218353b80718fd1d66751e # timeout=10
> git rev-list --no-walk ef8d7d847fd1978da6218353b80718fd1d66751e # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
the userEmail is: 1239683670@qq.com status: SUCCESS
[Pipeline] emailext
Sending email to: 1239683670@qq.com
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

 最后收到的邮件效果: 

Jenkins GitLab集成 邮件通知_html_16

邮件通知封装到共享库


剩下的就是添加到共享库里面

Jenkins GitLab集成 邮件通知_jenkins_17

Jenkins GitLab集成 邮件通知_html_18

 

Jenkins GitLab集成 邮件通知_html_19

共享库的代码都写到这里了。把邮件通知的先到共享库里面

Jenkins GitLab集成 邮件通知_html_20

package org.devops

//邮件通知
def EmailUser(userEmail,status){
println("the userEmail is ${userEmail} status is ${status}")
emailext body: """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
<img src="http://192.198.166.235:81/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: "${userEmail}"

}

然后去把Jenkinsfile加入到项目下面,使用这个共享库 

Jenkins GitLab集成 邮件通知_html_21

Jenkins GitLab集成 邮件通知_git_22

@Library("devopslib@main") _

def myemail = new org.devops.email()


webHookData = readJSON text: "${webHookData}"
env.userName = webHookData["user_username"]
env.userEmail = webHookData["user_email"]
env.branchName = webHookData["ref"] - "refs/heads/"
env.commitID = webHookData["checkout_sha"]
env.git_http_url = webHookData["project"]["git_http_url"]

currentBuild.displayName = env.commitID
currentBuild.description = "Trigger by user ${env.userName} \n branch: ${env.branchName}"


pipeline {

agent {
label 'build'
}

stages {
stage('CheckOut') {
steps {
println("project git url is ${env.git_http_url} ")
println("the user email is ${env.userEmail}")
checkout([$class: 'GitSCM',
branches: [[name: "${env.branchName}"]],
extensions: [], userRemoteConfigs:
[[credentialsId: "${credentialsId}",
url: 'http://139.198.166.235:81/dev/devops-hello-service.git']]])
}
}
}


post{
always{
script{
//emailext body: 'hello jenkins.....', subject: 'test........', to: '1239683670@qq.com'
//EmailUser(userEmail,currentBuild.currentResult)
myemail.EmailUser(userEmail,currentBuild.currentResult)
}
}

}

}

 

Jenkins GitLab集成 邮件通知_json_23

 

 

配置共享库


Jenkins GitLab集成 邮件通知_html_24

Jenkins GitLab集成 邮件通知_jenkins_25