安装Post build task插件
Dashboard > 系统管理 > 插件管理,搜索Post build task安装重启Jenkins
编写通知脚本
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
评论区