Jenkins GitLab集成 邮件通知

网友投稿 1255 2022-11-30

Jenkins GitLab集成 邮件通知

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

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

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

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

checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '7c74af64-1774-4031-bc5f-0e97de40b2cd', url: '我们这里获取到然后使用 ​​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_= webHookData["project"]["git_= env.commitIDcurrentBuild.description = "Trigger by user ${env.userName} \n branch: ${env.branchName}"pipeline { agent any stages { stage('CheckOut') { steps { println("project git url is ${env.git_") println("the user email is ${userEmail}") checkout([$class: 'GitSCM', branches: [[name: "${env.branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${credentialsId}", url: ' } } }}

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

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

安装一个Email插件

​​Email Extension | Jenkins plugin​​

#上传插件到这个目录下面,重启生效[root@jenkins-master plugins]# pwd/data/cicd/jenkins/plugins[root@jenkins-master plugins]# docker restart jenkinsjenkins

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

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

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

发送短信之后获取授权码

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

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

这块邮箱就配置成功了

使用 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,

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

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_= webHookData["project"]["git_= env.commitIDcurrentBuild.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_") println("the user email is ${env.userEmail}") checkout([$class: 'GitSCM', branches: [[name: "${env.branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${credentialsId}", url: ' } } } 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: """
构建信息

""", subject: "Jenkins-${JOB_NAME}项目构建信息 ", to: "${userEmail}"}

Generic CauseRunning in Durability level: MAX_SURVIVABILITY[Pipeline] Start of Pipeline[Pipeline] readJSON[Pipeline] nodeRunning on build-01 in /data/cicd/jenkinsagent/workspace/pipeline-pullcode-email[Pipeline] {[Pipeline] stage[Pipeline] { (CheckOut)[Pipeline] echoproject git url is [Pipeline] echothe user email is 1239683670@qq.com[Pipeline] checkoutThe recommended git tool is: NONEusing credential 11da4458-0df7-4077-8e4d-a902b0b79eb8Fetching changes from the remote Git repositoryChecking 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 # timeout=10Fetching upstream changes from > git --version # timeout=10 > git --version # 'git version 2.7.3'using GIT_ASKPASS to set credentials gitlab-devop > git fetch --tags --progress +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] echothe userEmail is: 1239683670@qq.com status: SUCCESS[Pipeline] emailextSending email to: 1239683670@qq.com[Pipeline] }[Pipeline] // script[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of PipelineFinished: SUCCESS

最后收到的邮件效果:

邮件通知封装到共享库

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

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

package org.devops//邮件通知def EmailUser(userEmail,status){ println("the userEmail is ${userEmail} status is ${status}") emailext body: """
构建信息

""", subject: "Jenkins-${JOB_NAME}项目构建信息 ", to: "${userEmail}"}

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

@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_= webHookData["project"]["git_= env.commitIDcurrentBuild.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_") println("the user email is ${env.userEmail}") checkout([$class: 'GitSCM', branches: [[name: "${env.branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${credentialsId}", url: ' } } } post{ always{ script{ //emailext body: 'hello jenkins.....', subject: 'test........', to: '1239683670@qq.com' //EmailUser(userEmail,currentBuild.currentResult) myemail.EmailUser(userEmail,currentBuild.currentResult) } } } }

配置共享库

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring启动过程源码分析及简介
下一篇:NexusAPI调试方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~