和釘釘一樣,也需要在群裡面添加一個機器人
2.1 添加群機器人
企業微信

文本消息
{
"text": {
"content": "DEVOPS我就是我, @kkobe 是不一樣的煙火",
"mentioned_list":["wangqing","@all"],
"mentioned_mobile_list":["13800001111","@all"]
},
"msgtype": "text"
}
圖文消息
{
"msgtype": "news",
"news": {
"articles" : [
{
"title" : "第一期CICD實踐",
"description" : "第一期CICD實踐-jenkins",
"url" : "https://www.idevops.site/detail/term_6054ad6b126c2_MTE8oq/25",
"picurl" : "http://wechatapppro-1252524126.file.myqcloud.com/app7x5jejrv6551/image/compress/640480598kmgc1v610gr1.png"
}
]
}
}
markdown格式消息
{
"msgtype": "markdown",
"markdown": {
"content": "## 《Jenkins實踐擴充》 \n ### 1.内容1 \n ### 2.内容1 \n ### 3.内容1 \n ### 4.内容1 \n> \n> ###### 更多詳細資訊點選 [課程連結](https://www.idevops.site/detail/term_6054ad6b126c2_MTE8oq/25) \n"
}
}
釘釘通知未優化前
pipeline {
agent {
label "build"
}
stages {
stage('DingDing') {
steps {
script{
DingDing()
}
}
}
}
}
def DingDing(){
sh """
curl --location --request POST 'https://oapi.dingtalk.com/robot/send?access_token=a054e493663471009fc3aad90bda43cf738903ad29a49e996ef69711957e1603' \
--header 'Content-Type: application/json' \
--data '{
"msgtype": "markdown",
"markdown": {
"title": "明天的DEVOPS課程安排",
"text": "## 建構通知 《Jenkins實踐擴充》 \n ### 1.内容1 \n ### 2.内容1 \n ### 3.内容1 \n ### 4.内容1 \n> \n> ###### 更多詳細資訊點選 [課程連結](https://www.idevops.site/detail/term_6054ad6b126c2_MTE8oq/25) \n"
},
"at": {
"atMobiles": [
"158115965723"
],
"atUserIds": [
"user123"
],
"isAtAll": true
}
}'
"""
}
rg如果和gitlab做內建的話,就簡單了,如果不是那麼需要安裝插件了。
注意: 為了擷取建構使用者的名稱, 需要安裝插件
build user vars plugin
。
擷取目前的管道運作使用者
插件:build user vars
wrap([$class: 'BuildUser']){
echo "full name is $BUILD_USER"
echo "user id is $BUILD_USER_ID"
echo "user email is $BUILD_USER_EMAIL"
}
pipeline {
agent {
label "build"
}
stages {
stage('DingDing') {
steps {
script{
wrap([$class: 'BuildUser']){
echo "full name is $BUILD_USER"
echo "user id is $BUILD_USER_ID"
echo "user email is $BUILD_USER_EMAIL"
env.BUILD_USER = "${BUILD_USER}"
}
DingDing()
}
}
}
}
}
def DingDing(){
withCredentials([string(credentialsId: 'c18c258f-9b80-4f62-b187-9ecf1955068c', variable: 'accsess_token')]) {
sh """
curl --location --request POST "https://oapi.dingtalk.com/robot/send?access_token=${accsess_token}" \
--header 'Content-Type: application/json' \
--data '{
"msgtype": "markdown",
"markdown": {
"title": "明天的DEVOPS課程安排",
"text": "## 建構通知 ${JOB_NAME} \n ### 建構人:${env.BUILD_USER} \n ### 作業狀态: ${currentBuild.currentResult} \n ### 運作時長: ${currentBuild.durationString} \n ### 更多詳細資訊點選 [建構日志](${BUILD_URL}/console \n"
},
"at": {
"atMobiles": [
"158115965723"
],
"atUserIds": [
"user123"
],
"isAtAll": true
}
}'
"""
}
}
釘釘微信通知代碼如下:
pipeline {
agent{
label "master"
}
stages{
stage("Dingding"){
steps{
script {
wrap([$class: 'BuildUser']){
echo "full name is $BUILD_USER"
echo "user id is $BUILD_USER_ID"
echo "user email is $BUILD_USER_EMAIL"
env.BUILD_USER = "${BUILD_USER}"
}
DingDing()
WeiXin()
}
}
}
}
}
/*
作業名稱:
建構人:
作業狀态:
建構日志:
運作時長:
*/
// 企業微信
def WeiXin(){
withCredentials([string(credentialsId: 'b8168f6e-3bb0-4dc1-bd01-2b2348cd089a', variable: 'ACCESS_TOKEN')]) {
sh """
curl --location --request POST 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=3979ef4b-c9c1-4192-9fbb-e5d16c1ec522' \
--header 'Content-Type: application/json' \
--data '{
"msgtype": "markdown",
"markdown": {
"content": "## ${JOB_NAME}作業建構資訊: \n ### 建構人:${env.BUILD_USER} \n ### 作業狀态: ${currentBuild.currentResult} \n ### 運作時長: ${currentBuild.durationString} \n ###### 更多詳細資訊點選 [建構日志](${BUILD_URL}/console) \n"
}
}'
"""
}
}
// 釘釘通知
def DingDing(){
withCredentials([string(credentialsId: '1fbae655-b543-4667-aa63-f48451e384b8', variable: 'ACCESS_TOKEN')]) {
// some block
sh """
curl --location --request POST "https://oapi.dingtalk.com/robot/send?access_token=${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{
"msgtype": "markdown",
"markdown": {
"title": "DEVOPS通知",
"text": "## ${JOB_NAME}作業建構資訊: \n ### 建構人:${env.BUILD_USER} \n ### 作業狀态: ${currentBuild.currentResult} \n ### 運作時長: ${currentBuild.durationString} \n ###### 更多詳細資訊點選 [建構日志](${BUILD_URL}/console) \n"
},
"at": {
"atMobiles": [
"158115965723"
],
"atUserIds": [
"user123"
],
"isAtAll": true
}
}'
"""
}
}