1)开始通知
在Jenkins发版过程的第一步添加下面内容,调用下面脚本实现机器人发版通知(注意脚本路径和传参)
${BUILD_USER}是Jenkins内置变量,执行发布的用户名,需要安装插件-Build User Vars Plugin,在构建任务里开启Set jenkins user build variables就可以调用这个变量
bash /data/jenkins/scripts/zhyly/test/alert 1 ${env} ${JOB_BASE_NAME} ${tag} ${BUILD_USER} &
[root@public-jenkins test]# cat /data/jenkins/scripts/zhyly/test/alert
#!/bin/bash
isk8s=${1}
env=${2}
project=${3}-server
tag=${4}
now=$(date "+%y%m%d-%H:%M:%S")
user=${5}
if [ ${env} == "zhylytest" ] || [ ${env} == "zhylypre" ];then
url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d38df46a-5f86-42a3-b606-031ed9546279' #这里是钉钉机器人地址
if [ ${isk8s} == 1 ];then
while [ 1 == 1 ]
do
nodeReady=$(kubectl get pod -n ajsh-pre|grep ${project}|awk '{print $2}'|grep 0) #指定项目的容器状态,如果有没有运行的,则表示失败。变量
if [ ${nodeReady} ];then ##该写法= if [ ! -z ${nodeReady} ];then,如果变量不为空则成立
sleep 10
continue #其作用为结束本次循环,即跳出循环体中下面尚未执行的语句
fi
break #break语句的作用就是用于只要出现问题,马上强制退出循环,不执行循环中后面剩余的东西
done
fi
curl -s ${url} \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"markdown": {
"content": "<font color=\"info\">'${project}'发布开始</font>\n
> 环境:<font color=\"comment\">'${env}'</font>\n
> 版本:<font color=\"comment\">'${tag}'</font>\n
> 执行人:<font color=\"comment\">'${user}'</font>\n
> 时间:<font color=\"comment\">'${now}'</font>"
}
}'
fi
这个脚本有问题:不管容器状态是否就绪,nodeReady变量都会存在。都会执行下面的发送通知,应对变量值进行判断,如果没有未就绪的容器则发送通知
[root@ajsh-dev001 bin]# a=`kubectl get pod -n ajsh-pre|grep ajsh-major |awk '{print $2}'|grep 2`
[root@ajsh-dev001 bin]# echo $a #这里的变量不为空
[root@ajsh-dev001 bin]#
2)结束通知
在Jenkins发版过程的最后一步添加下面内容(如果有调用python接口,就在最后一个python接口对应的脚本最后一行添加,也就是整个发版过程的最后一部)。调用下面脚本实现机器人发版通知
alert 1 ${ns} ${server_name} ${tag} ${BUILD_USER} &
[root@zhyly-master .bin]# cat alert
#!/bin/bash
isk8s=${1}
env=${2}
project=${3}-server
tag=${4}
now=$(date "+%y%m%d-%H:%M:%S")
user=${5}
if [ ${env} == "zhylytest" ] || [ ${env} == "zhylypre" ];then
url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d38df46a-5f86-42a3-b606-031ed9546279' #这里是钉钉机器人地址
if [ ${isk8s} == 1 ];then
while [ 1 == 1 ]
do
nodeReady=$(kubectl get pod -n ajsh-pre|grep ${project}|awk '{print $2}'|grep 0)
if [ ${nodeReady} ];then
sleep 10
continue
fi
break
done
fi
curl -s ${url} \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"markdown": {
"content": "<font color=\"info\">'${project}'发布结束</font>\n
> 环境:<font color=\"comment\">'${env}'</font>\n
> 版本:<font color=\"comment\">'${tag}'</font>\n
> 执行人:<font color=\"comment\">'${user}'</font>\n
> 时间:<font color=\"comment\">'${now}'</font>"
}
}'
fi
标签:bin,grep,微信,project,ajsh,env,发版,Jenkins
From: https://www.cnblogs.com/anslinux/p/16850106.html