安装插件
-
DingTalk 插件 2.4.3
-
build user vars plugin 插件可能有相关版本依赖,需要重启一下 Jenkins 才能继续安装在流水线中,使用wrap,获取BUILD_USER变量(如果不使用可以注射这个功能)
wrap([$class: 'BuildUser']) {
}
钉钉配置
package org.devops
def GetChangeString() {
MAX_MSG_LEN = 100
def changeString = ""
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length; j++) {
def entry = entries[j]
truncated_msg = entry.msg.take(MAX_MSG_LEN)
commitTime = new Date(entry.timestamp).format("yyyy-MM-dd HH:mm:ss")
changeString += "> - ${truncated_msg} [${entry.author} ${commitTime}]\n"
}
}
if (!changeString) {
changeString = "> - No new changes"
}
return changeString
}
def DingdingReq(RobotID, Status) {
// wrap([$class: 'BuildUser']) {
def changeString = GetChangeString()
dingtalk (
robot: RobotID,
type: 'MARKDOWN',
title: '你有新的消息,请注意查收',
text: [
"### 构建信息",
"> - 应用名称:**${env.JOB_NAME}**",
"> - 构建结果:**${Status}**",
"> - 当前版本:**${env.BUILD_NUMBER}**",
"> - 构建发起人:**${env.BUILD_USER}**",
"> - 持续时间:**${currentBuild.durationString}**",
"> - 构建日志:[点击查看详情](${env.BUILD_URL}console)",
"### 更新记录:",
"${changeString}"
],
at: [
'xxxxxxxxxxx'
]
)
// }
}
https://blog.csdn.net/weixin_42562106/article/details/116207010
@Library ("jr-shared-library") _
def dingding = new org.devops.dingding()
String robotId = "45d3423b-90d2-44fb-a9f2-aa4e5d2d2371"
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
post {
success {
script {
dingding.DingdingReq(robotId, "构建成功 ✅")
}
}
failure {
script {
dingding.DingdingReq(robotId, "构建失败 ❌")
}
}
}
}