一、安裝插件
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