zl程序教程

您现在的位置是:首页 >  工具

当前栏目

使用Jenkins扩展共享库进行钉钉消息推送

Jenkins扩展消息 使用 进行 共享 推送
2023-06-13 09:15:42 时间

起因:执行完流水线后进行一定程度的消息推送,所以选择钉钉进行jenkins构建结构的消息推送

下载配置相关依赖插件

相关环境:

Jenkins2.277.3,安装文档见上篇 DingTalk 插件 2.4.3 build user vars plugin 插件 1.7

找到 Jenkins 首页 系统管理 钉钉,我的配置如下图

image-20210426164341273

:warning: ​注意手动输入机器人的id,不要自动生成,否则重启后,robot的id将发生变化


安装build user vars plugin插件,插件可能有相关版本依赖,需要重启一下 Jenkins 才能继续安装

在流水线中,使用wrap,获取BUILD_USER变量

wrap([$class: BuildUser]) {

创建配置共享库 编写Groovy脚本

项目目录结构如下:

$ tree jenkinslibrary

jenkinslibrary

├── README.md

└── src

 └── org

 └── devops

 └── dingmes.groovy

3 directories, 2 files

dingme.groovy文件内容如下

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

文本中xxxxxxxxxxx请根据实际情况更换为钉钉群组里面,具体人的手机号,可以添加多个

在 Jenkins 中配置将共享库

找到 Jenkins 首页 系统管理 Global Pipeline Libraries,我的配置如下图

image-20210426171913350

在流水线中导入共享库

导入方法@Library(pipeline-library-demo)_,这样就可以使用共享库中的代码了

具体的 pipeline 脚本如下:

#!groovy

@Library(pipeline-library-demo)_

//func from shareibrary

def dingmes = new org.devops.dingmes()

//env

String branchName = "master"

String gitlabCredentialsId = "xxx"

String gitUrl = "http://xxx/xxx/jenkinslibrary.git"

String robotId = "2e0e11c4-2211-4687-b317-cacf58197288"

pipeline {

 agent any

 stages {

 stage(Git Clone) {

 steps {

 git branch: "{branchName}",

 credentialsId: "{gitlabCredentialsId}",

 url: "${gitUrl}"

 post {

 success {

 script {

 dingmes.DingdingReq(robotId, "构建成功 ✅")

 failure {

 script {

 dingmes.DingdingReq(robotId, "构建失败 ❌")

至此完成,构建效果如下图:

image-20210426172713667

遇到的问题 currentBuild.durationString的值传递不进去,一开始先用withEnv包裹一下

后来找到原因 Groovy 在单引号的字符串里面是不支持插值的,所以要用双引号

单引号中的env.JOB_NAME会引用失败,双引号则引用成功

单、双引号引用JOB_NAME都引用成功

推荐所有变量都用 双引号

三引号也是一样, 三单引号不支持插值, 三双引号支持插值

参考链接:https://www.ssgeek.com/post/jenkinssharelibrary-shi-jian-zhi-zi-ding-yi-tong-zhi-qi/

本文链接:http://www.yunweipai.com/39879.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52855.html

jenkins