##获取钉钉审批脚本,可以接入到jenkins中,在jenkins构建过程中,先使用此脚本,其中acitonurl则可以用golang起个端口服务,解析url参数,最后判断脚本审批失败,则不触发后续构建,审批成功,则触发后续构建。
#!/bin/bash
if [ $# != 2 ];then
echo "参数不对";
exit 1
fi
title=$1
text=$2
# 钉钉机器人Webhook地址
webhook_url="https://oapi.dingtalk.com/robot/send?access_token=钉钉token"
# 请求体
data='{
"msgtype": "actionCard",
"actionCard": {
"title": "20'"$title"'",
"text": "'"$text"'",
"hideAvatar": "0",
"btnOrientation": "0",
"btns": [
{
"title": "同意",
"actionURL": "http://localhost:8080/request?action=agree&text='"$text"'"
},
{
"title": "拒绝",
"actionURL": "http://localhost:8080/request?action=reject&text='"$text"'"
}
]
}
}'
# 发送消息
curl -sS ${webhook_url} \
-H 'Content-Type: application/json' \
-d "${data}" > /dev/null
# 模拟轮询读取审批结果,最多轮询 1000 次
for i in {1..1000}; do
approve_url="http://localhost:8080/recent"
result=$(curl -s $approve_url)
if [ "$result" == "agree" ]; then
echo "审批已通过"
break
elif [ "$result" == "reject" ]; then
echo "审批已拒绝"
break
fi
sleep 5
done
if [ $i -eq 1000 ]; then
echo "轮询超时,请检查审批进度。"
fi
###go示例
# package main
# import (
# "net/http"
# "strconv"
# "github.com/gin-gonic/gin"
# )
# func main() {
# r := gin.Default()
# var recent string
# r.GET("/request", func(c *gin.Context) {
# action := c.Query("action")
# text := c.Query("text")
# recent = "action=" + action + "&text=" + text
# c.JSON(http.StatusOK, gin.H{
# "action": action,
# "text": text,
# })
# })
# r.GET("/recent", func(c *gin.Context) {
# // 转义字符串中的特殊字符
# quoted := strconv.QuoteToASCII(recent)
# var content string
# if recent == "" {
# content = "No recent visit"
# } else {
# content = quoted[1 : len(quoted)-1] // 去掉转义后字符串外围的引号
# }
# // 修改响应头使其返回纯文本
# c.Header("Content-Type", "text/plain; charset=utf-8")
# c.String(http.StatusOK, content)
# })
# r.Run()
# }