jenkins高級篇 pipeline 系列之-—07 實現釘釘機器人插件效果


一. 知識點補充

知識點1. Jenkins Pipeline 中使用全局變量

jenkins 中可用的全局變量見如下鏈接: http://jenkinsServer/jenkins/pipeline-syntax/globals#en
使用方式:
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
  }
}

知識點3. Jenkins Pipeline 中獲取shell命令的標准輸出或者狀態

shell中獲取標准輸出或者狀態參考

//獲取標准輸出
//第一種
result = sh returnStdout: true ,script: "<shell command>"
result = result.trim()
//第二種
result = sh (script: "<shell command>", returnStdout: true).trim()
//第三種
sh "<shell command> > commandResult"
result = readFile('commandResult').trim()

//獲取執行狀態
//第一種
result = sh returnStatus: true ,script: "<shell command>"
result = result.trim()
//第二種
result = sh (script: "<shell command>", returnStatus: true).trim()
//第三種
sh '<shell command>; echo $? > status'
def r = readFile('status').trim()

在pipeline 中的應用

//如果命令執行沒報錯,exitValue為0,報錯則為非0
  def exitValue = sh(script: '"$ANT_HOME/bin/ant" -f svn/build.xml', returnStatus: true)
  if (exitValue != 0) {
    echo "return exitValue :${exitValue}"
    currentBuild.result = 'FAILURE'
  }	

二. 最終的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"
			}
        }
    }
}

效果如下:

注意:

如果在Pipeline stage 中運行shell命令,且命令執行失敗了,這時 Pipeline 的執行狀態還是成功,間接導致郵件或者釘釘消息顯示成功,誤導消息接收人。例如我通過調用shell 中 ant命令執行sql,腳本有問題執行失敗了,通知消息還是成功。
這里用知識點3,根據shell的狀態修改 currentBuild.result 的值,問題得到解決。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM