51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Jenkins构建任务后飞书机器人告警通知

安装Post build task插件 {#%E5%AE%89%E8%A3%85post-build-task%E6%8F%92%E4%BB%B6}

Dashboard > 系统管理 > 插件管理,搜索Post build task安装重启Jenkins

编写通知脚本 {#%E7%BC%96%E5%86%99%E9%80%9A%E7%9F%A5%E8%84%9A%E6%9C%AC}

Jenkins没有像钉钉那样开发好的插件

流水线不支持飞书通知

进入前面建立的自由风格mytest任务添加构建后操作,选择Post build task

在Script框中编写通知脚本

输入下面脚本,替换你的Jenkins账号密码和飞书机器人的Webhook地址

#!/bin/bash
JOB_URL="${JENKINS_URL}job/${JOB_NAME}"

getBuildState(){
buildNr=$1
result=$2
user=monster
passwd=123456
curl -u $user:$passwd ${JOB_URL}/${buildNr}/api/json \|grep -Po $result
}


state=$(getBuildState $BUILD_NUMBER '"result":\\s\*"\\K\\w+')
des=$(getBuildState $BUILD_NUMBER 'msg\[":\]+\\K\[\^"\]+')
pro=$(getBuildState $BUILD_NUMBER 'fullName\[":\]+\\K\[\^"\]+')
string1=$BUILD_DISPLAY_NAME
string2=$JOB_BASE_NAME
nowTime=$(date "+%Y-%m-%d %H:%M:%S")


echo ${state}
echo ${des}
echo ${pro}

`if [[ "${state}" == "SUCCESS" ]] ; then
curl -X POST -H "Content-Type: application/json" `
`
-d '{"msg_type":"post","content": {"post": {"zh_cn": {"title": "编译结果通知","content": [[{"tag": "text","text": "'"项目名称:$string2\n构建编号:第$BUILD_NUMBER次构建\n远程分支:$GIT_BRANCH\n构建状态:成功\n构建日期:$nowTime\n构建版本:$version"'"}]]} } }}' `
`
https://open.feishu.cn/open-apis/bot/v2/hook/e347b1fd-b49b-45d8-87df-e311ac9941bf
else
curl -X POST -H "Content-Type: application/json" `
`
-d '{"msg_type": "interactive","card": {"elements": [{"tag": "div","text": {"content": "'"项目名称: $string2\n构建编号: 第$BUILD_NUMBER次构建\n远程分支: $GIT_BRANCH\n构建状态: 失败\n构建日期: $nowTime\n构建版本:$version\n"'","tag": "lark_md"}}, {"actions": [{"tag": "button","text": {"content": "点击查看错误日志","tag": "lark_md"},"url": "'"$JOB_URL/$BUILD_NUMBER/consoleText"'","type": "default","value": {}}],"tag": "action"}],"header": {"title": {"content": " 编译结果通知","tag": "plain_text"}}}}' `
`
https://open.feishu.cn/open-apis/bot/v2/hook/e347b1fd-b49b-45d8-87df-e311ac9941bf
fi`

告警效果 {#%E5%91%8A%E8%AD%A6%E6%95%88%E6%9E%9C}


赞(0)
未经允许不得转载:工具盒子 » Jenkins构建任务后飞书机器人告警通知