一、安装插件
1、安装Jenkins插件——Post build task
进入Jenkins——系统配置——插件管理——可选插件,输入Post build task搜索安装
2、重启Jenkins
安装完插件后重启Jenkins
二、飞书群中添加自定义机器人
1、添加机器人
2、复制保存机器人的webhook
三、Jenkins配置构建后脚本
1、打开jobs配置,选择“构建后操作”——Post build task
2、输入脚本内容
#!/bin/bash JOB_URL="${JENKINS_URL}job/${JOB_NAME}" getBuildState(){ buildNr=$1 result=$2 user=test passwd=test 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} changlog=$(awk 'NR==FNR {a[FNR]=$1; next} {print $0, a[FNR]}' <(echo "$pro") <(echo "$des") |tr ' ' '_' ) scm=$(echo $changlog | awk '{gsub(/ /,";")}1') 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提交记录:$scm"'"}]]} } }}' \ https://open.feishu.cn/open-apis/bot/v2/hook/ 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提交记录:$scm\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 fi
标签:插件,FNR,JOB,BUILD,getBuildState,Jenkins,jenkins,推送,飞书 From: https://www.cnblogs.com/lgxlsm/p/17449900.html