jenkins高级篇 pipeline 系列之-—07 实现钉钉机器人效果

一. 知识点补充

知识点1. Jenkins Pipeline 中使用 全局变量

echo "${currentBuild.displayName},${currentBuild.result}"

知识点2. Jenkins 使用 Pipeline 获取当前构建任务的构建人

pipeline中的全局变量,默认不支持获取当前构建任务的构建人,要想获取构建人的信息,只能通过插件

插件:build-user-vars-plugin 下载地址

# 下载插件的源码
wget https://github.com/jenkinsci/build-user-vars-plugin/archive/build-user-vars-plugin-1.5.zip

# 进入到解压后的插件目录中, 执行mvn打包命令
mvn clean package

# 完成后,在target目录中会生成一个build-user-vars-plugin.hpi文件
# 将.hpi结尾的文件,jenkins上手动上传插件即可

#Pipeline Examples
node {
  wrap([$class: 'BuildUser']) {
    def user = env.BUILD_USER_ID
  }
}

二. 最终的pip脚本

// vars/buildAndDeploy.groovy
def call(Map config) {

    node {
......	
        try {
......
       } finally {
			wrap([$class: 'BuildUser']) {
			def user = env.BUILD_USER_ID
				if (currentBuild.result == 'SUCCESS') {
					dingtalk (
							robot: "${dingMap.accessToken}",
							type: 'ACTION_CARD',
							title: "${env.JOB_NAME} ${currentBuild.displayName}构建成功",
							text: [
								"### [${env.JOB_NAME}](${env.JOB_URL}) ",
								'---',
								"- 任务:[${currentBuild.displayName}](${env.BUILD_URL})",
								'- 状态:<font color=#00CD00 >成功</font>',
								"- 持续时间:${currentBuild.durationString}".split("and counting")[0],
								"- 执行人:${user}",
							],
						)
				} else if (currentBuild.result == 'FAILURE') {
					dingtalk (
							robot: "${dingMap.accessToken}",
							type: 'ACTION_CARD',
							title: "${env.JOB_NAME} ${currentBuild.displayName}构建失败",
							text: [
								"### [${env.JOB_NAME}](${env.JOB_URL}) ",
								'---',
								"- 任务:[${currentBuild.displayName}](${env.BUILD_URL})",
								'- 状态:<font color=#EE0000 >失败</font>',
								"- 持续时间:${currentBuild.durationString}".split("and counting")[0],
								"- 执行人:${user}",
							],
						)
				} else {
					echo "${env.JOB_NAME} ${currentBuild.displayName} ${currentBuild.result}"
				}
			}
			withEnv(["QA_EMAIL=${config.QA_EMAIL}"]) {
				emailext body: '''${DEFAULT_CONTENT}''', subject: '''${DEFAULT_SUBJECT}''', to: "$QA_EMAIL"
			}
        }
    }
}

效果如下:

【Quality】 Quality is the value to someone who matters。做测试,首先要找到这个someone是谁,以及这个 someone重视的 value是什么。
原文地址:https://www.cnblogs.com/liuyitan/p/14373452.html