當咱們經過Jenkins構建job的時候,是能夠獲取到git Change Log 的信息, 即本次上線修改了什麼功能,咱們將這個信息發送到微信羣相關人員可直接獲取到上線變動信息,python
這樣就不須要人爲的去通告,如下是效果圖:git
主要用到的這個插件: https://github.com/daniel-beck/changelog-environment-plugingithub
核心配置: redis
我這裏使用的是 項目構建完成後 使用Post build task 調用一個發送的腳本,這個腳本會將信息發送到對應微信羣,json
你也能夠經過其它的方式,只要能將消息發出去:api
微信發送腳本代碼:bash
#!/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裏面沒法使用上面的插件,我經過編寫腳本實現瞭如上同樣的功能:併發
大致思路以下:app
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
參考網址: