通知管理
Jenkins 通知概述
-
Jenkins通知 是将任务的执行状态、事件或信息推送给相关用户,这些通常发生在pipeline的构建后处理(post-processing)时期;
- Email是Jenkins内置支持的通知方式,它也能够通过webhook扩展支持其它的即时通信媒介,例如 Slack、钉钉等;
-
Jenkins使用的许多插件或工具都会为其支持的多种任务生成HTML报告,相关的任务有如代码分析、代码覆盖率和单元测试等;
- 其中一些工具(如SonarQube和JaCoCo等)甚至可以和Jenkins任务输出做定制集成,这些工具甚至会采用直观的图标给出相关报告的链接;
- 对于那些在报告相关的功能方面支持的不是特别好的工具来说,我们也可以借助于HTML Publisher插件进行定制;
基于内置邮件功能
Jenkins UI 配置
- Jenkins UI –> 系统配置
Jenkins Location
- 系统管理员邮件地址
- 例如:767483070@qq.com
邮件通知
- SMTP服务器
- 例如:smtp.qq.com
- 用户默认邮件后缀
- 例如:@qq.com
- 使用SMTP认证 √
- 用户名密码自行填写
- 注意:此处用户名要和 Jenkins Location 中的系统管理员邮件地址保持一致!
- 用户名密码自行填写
- 使用SSL协议 √
- SMTP端口
- 例如:465
- 最后填写接受邮箱测试能否发送。
pipeline
https://www.jenkins.io/doc/pipeline/steps/workflow-basic-steps/#mail-mail
- 在声明式pipeline的post{}里可以使用mail这一step来发送通知
- 该步骤支持有参数有如下几个:
- subject:邮件主题;
- to:收件人地址;
- body:邮件正文;
- from:发件地址(通常可省略);
- 该步骤支持有参数有如下几个:
example
pipeline {
agent any
tools {
maven 'maven-3.8.6'
}
stages {
stage('Source') {
steps {
git branch: 'main', url: 'git@gitlab.xiangzheng.com:gitlab-instance-010b0ac5/spring-boot-helloWorld.git'
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
}
post {
always {
mail to: 'sredevops@163.com',
subject: "Status of pipeline: ${currentBuild.fullDisplayName}",
body: "${env.BUILD_URL} has result ${currentBuild.result}"
}
}
}- 测试
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the project...'
// 模拟构建步骤
}
}
stage('Test') {
steps {
echo 'Running tests...'
// 模拟测试步骤
}
}
}
post {
always {
mail to: 'sredevops@163.com, 767483070@qq.com',
subject: "Status of pipeline: ${currentBuild.fullDisplayName}",
body: "${env.BUILD_URL} has result ${currentBuild.result}"
}
}
}基于邮件扩展插件
- 需事先安装 Email Extension Plugin 插件
pipeline
DingTalk
- 需事先安装 DingTalk 插件
前期准备
- 创建钉钉webhook机器人。
Jenkins UI 配置
- Jenkins UI –> 系统配置 –> 钉钉(新版本中移动到了系统管理中的未分类栏中)
- 代理
- 未设置代理则无需配置
- 机器人
- id
- 机器人id,后续可以在pipeline中调用,例如:dingtalk-robot
- 名称
- 机器人id,例如:dingtalk-robot
- webhook
- 例如:https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx
- 加密
- 例如:SEC11fee24d65dfxxxxxxxxxxxxxx
- 最后可以进行测试。
- id
pipeline
https://jenkinsci.github.io/dingtalk-plugin/guide/pipeline.html#参数说明
example
pipeline {
agent any
tools {
maven 'maven-3.8.6'
}
stages {
stage('Source') {
steps {
git branch: 'main', url: 'git@gitlab.xiangzheng.com:gitlab-instance-010b0ac5/spring-boot-helloWorld.git'
}
}
stage('Build') {
steps {
sh 'mvn clean package'
}
}
}
post {
always {
dingtalk(
robot: 'dingtalk-robot',
type: 'TEXT',
text: [
"Status of pipeline: ${currentBuild.fullDisplayName}",
"${env.BUILD_URL} has result ${currentBuild.result}."]
)
}
}
}