首先需要Post Build Task插件,在构建后操作里选择它,然后写入下方shell代码

#!/bin/bash
JOB_URL="${JENKINS_URL}job/${JOB_NAME}"
getBuildState(){
  buildNr=$1
  curl -v ${JOB_URL}/${buildNr}/api/json --header 'Authorization: Basic YWRtaW46VWF0NjY2IQ==' | grep -Po '"result":\s*"\K\w+'
}
state=$(getBuildState ${BUILD_NUMBER}  )
string1=$BUILD_DISPLAY_NAME
string2=$JOB_BASE_NAME
nowTime=$(date "+%Y-%m-%d %H:%M:%S")
echo $state
if [[ "x${state}" == "xSUCCESS" ]] ; then
   curl -X POST -H "Content-Type: application/json" \
        -d '{"msg_type":"post","content": {"post": {"zh_cn": {"title": "发布结果通知","content": [[{"tag": "text","text": "'"应用:本群通知专用\n系统:$string2\n构建:$string1\n状态:成功\n日期:$nowTime"'"}]]} } }}' \
https://这里加上飞书群机器人Hook就OK!
else
   curl -X POST -H "Content-Type: application/json" \
        -d '{"msg_type":"text","content":{"text":"'"应用:本群通知专用\n系统:$string2\n构建:$string1\n状态:失败\n日期:$nowTime"'"}}' \
https://这里加上飞书群机器人Hook就OK!

fi

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐