当我们通过Jenkins构建job的时候,是可以获取到git Change Log 的信息, 即本次上线修改了什么功能,我们将这个信息发送到微信群相关人员可直接获取到上线变更信息,
这样就不需要人为的去通告,以下是效果图:
主要用到的这个插件: https://github.com/daniel-beck/changelog-environment-plugin
核心配置:
我这里使用的是 项目构建完成后 使用Post build task 调用一个发送的脚本,这个脚本会将信息发送到对应微信群,
你也可以通过其它的方式,只要能将消息发出去:
微信发送脚本代码:
#!/usr/bin/python2.7 #_*_coding:utf-8 _*_ import requests,sys,json import urllib3 urllib3.disable_warnings() reload(sys) sys.setdefaultencoding(‘utf-8‘) def GetToken(Corpid,Secret): Url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken" Data = { "corpid":Corpid, "corpsecret":Secret } r = requests.get(url=Url,params=Data,verify=False) Token = r.json()[‘access_token‘] return Token def SendMessage(Token,Subject,Content, ProName): Url = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=%s" % Token Data = { "chatid": "JenkinsAlarm", # 此处不明白请参考企业微信官网 "msgtype": "text", "text": { "content": "[项目名称] : " + ProName + ‘\n‘ + "[项目地址] : " + Subject + ‘\n‘ + Content + ‘\n‘ }, "safe": "0" } r = requests.post(url=Url,data=json.dumps(Data),verify=False) return r.text def action_from_file(filename): try: str1 = ‘[变更日志] : ‘ with open(filename, ‘r‘) as f: for i in f.readlines(): str1 += i if len(str1) == 17: str1 += " 无变更" return str1 except Exception as e: print(‘[ERROR] {0}‘.format(e)) if __name__ == ‘__main__‘: Corpid = "xxxx" Secret = "xxxxxxxxx" Subject = sys.argv[1] Content = action_from_file(sys.argv[2]) ProName = sys.argv[3] Token = GetToken(Corpid, Secret) Status = SendMessage(Token,Subject,Content,ProName) print Status
以上是正常的通过每个Job内配置的git地址获取的方式。
但是我们这里有个k8s的pipeline配置,pipeline里面无法使用上面的插件,我通过编写脚本实现了如上一样的功能:
大体思路如下:
1. 获取当前 commit_id , 获取上次 commit_id , 通过git命令取到两次commit_id之间的日志并发送
2. 我是将commit_id 存到redis,每次更新commit_id , 项目第一次构建的话会取前两行发送,之后则正常
代码如下
#!/bin/bash BaseDir=$1 Project=$2 Joburl=$3 Branch=$4 if [[ $Branch != master ]]; then echo "Not master, exit..." exit 0 fi last_commit_id=$(/usr/bin/redis-cli -h 192.168.111.152 get $Project) cd $BaseDir && curr_commit_id=$(git log HEAD -1 --pretty=format:‘%H‘) if [[ $last_commit_id == "" ]] ; then cd $BaseDir msg=$(git log --date=format:‘%Y-%m-%d %H:%M:%S‘ --pretty=format:‘%s (at %cd via %cn)‘|head -2) echo "$msg" > /tmp/build_msg elif [[ $last_commit_id == ${curr_commit_id} ]] ; then msg="(无变更)" echo "(无变更)" > /tmp/build_msg else msg=`git log --date=format:"%Y-%m-%d %H:%M:%S" --pretty=format:"%s (at %cd via %cn)" ${last_commit_id}..${curr_commit_id}` echo "$msg" > /tmp/build_msg fi /usr/bin/redis-cli -h 192.168.111.152 set $Project $curr_commit_id #python /root/auto_falcon/jenkins_notify.py $Project /tmp/build_msg $Joburl grep -v Merge /tmp/build_msg|cat -n > /tmp/send_msg curr_date=$(date "+%Y/%m/%d %H:%M:%S") change_log=$(cat /tmp/send_msg) Content="[构建时间] : ${curr_date} \n[项目名称] : ${Project} \n[项目地址] : ${Joburl}\n[变更日志] :${change_log}" CropID="xxxx" Secret="xxxxx" GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" ‘{print $10}‘) PURL="https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=$Gtoken" /usr/bin/curl --data-ascii ‘{ "chatid": "jenkinsAlarm", "msgtype": "text","text": {"content": "‘"${Content}"‘"},"safe":"0"}‘ $PURL
参考网址:
1. https://www.jianshu.com/p/f03fc1bf5783