Hunter的大杂烩 技术学习笔记

2018-03-30

jenkins通过外部脚本执行git shortlog命令无输出的问题

Filed under: 技术话题 — hunter @ 7:56 pm

最近尝试用jenkins在定时构建环节统计开发人员的代码变更情况,统计方法参考http://qa.blog.163.com/blog/static/19014700220142131203191/。结果实际运行时遇到一个奇怪的问题,在命令行中明明运行很好的git shortlog命令,放到jenkins里面运行,总是无输出,在度娘上搜了一个下午,一开始以为是环境变量问题(参考https://blog.csdn.net/zzusimon/article/details/57080337),加上”#!/bin/bash -ilex”后,仍然不行,最后还是bing搜索给力,在stackoverflow上找到解决方案(https://stackoverflow.com/questions/43041659/git-shortlog-does-not-show-output-in-jenkins-shell),最后成型的脚本如下,供参考(btw:博客黏贴代码时缩进没了,请自行处理吧~~):

[code language=”python”]
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import sys
import os
import time
import datetime
import smtplib
import commands # 不要用os.system

if len(sys.argv) < 3:
print "input argument not enough"
sys.exit()

os.chdir(sys.argv[1]) # PATH不存在,脚本会立即终止
projectPATH = sys.argv[1]
pathArray = projectPATH.split(‘/’)
projectName = pathArray[len(pathArray)-1]

daily=1
if sys.argv[2] == "weekly":
daily = 0

todayStr = "%04d-%02d-%02d" % (datetime.date.today().year,datetime.date.today().month,datetime.date.today().day)
calTime = time.strftime("%Y-%m-%d %H:%M")
untilDate = datetime.date.today() + datetime.timedelta(days=-1)
if daily == 0:
untilDate = datetime.date.today() + datetime.timedelta(weeks=-1)
untilDayStr = "%04d-%02d-%02d" % (untilDate.year, untilDate.month,untilDate.day)

authorList=[]

resultTxt=""
try:
S, R = commands.getstatusoutput("/usr/local/git/bin/git shortlog –numbered –summary HEAD")
except Exception as e:
resultTxt="get git shortlog error:\n"
resultTxt=resultTxt + e.strerror + "\n"
else:
if S == 0:
authorSummaryList=R.split(‘\n’)
count=0
for authorLine in authorSummaryList:
authorArray=authorLine.split(‘\t’)
if len(authorArray) < 2:
break
authorName=authorArray[1]
#authorList.append(authorName)
try:
commandStr="/usr/local/bin/git log –since={0} –until={1} –author={2} –pretty=tformat: –numstat ".format(untilDayStr,todayStr,authorName)
commandStr= commandStr + "| awk ‘{add += $1; subs +=$2; loc += $1-$2} END {printf \"<td width=100>++: %s </td><td width=100>–: %s </td><td valign=center>total: %s </td>\", add, subs, loc}’"
S, R1 = commands.getstatusoutput(commandStr)
except Exception as e:
resultTxt=resultTxt + "get git shortlog error:" + "\n"
resultTxt=resultTxt + e.strerror + "\n"
else:
if S == 0:
if R1.find("–: ")<0 and R1.find("++: ")<0 :
resultTxt=resultTxt + "<tr><td width=160>" + authorName + " result:</td>" + R1 + "</tr><br>\n"
count=count+1
else:
resultTxt = resultTxt + "<tr>excute command error?:%d|%s" % (S ,commandStr) + "</tr><br>\n"
if count == 0:
resultTxt = "null output:[" + R + "]"
else:
resultTxt=resultTxt + "get git shortlog error2:%d" % S + "\n"

if daily == 1:
mailContent = "<h3>[" + projectName + "]代码量统计</h3><br>\n<br>\n"
else:
mailContent = "<h3>[" + projectName + "]周代码量统计</h3><br>\n<br>\n"
mailContent = mailContent + "<p>since:" + untilDayStr + "</p>\n"
mailContent = mailContent + "<p>until:" + todayStr+ "</p>\n"
mailContent = mailContent + "<p>caltime:" + calTime+ "</p>\n"
mailContent = mailContent + "<br>\n<br>\n<table border=1 width=\"500\">" + resultTxt + "</table>"

from email.mime.text import MIMEText
from email.header import Header

sender = ‘noreply@corp.xxx.com’
receivers = [‘hunter@corp.xxx.com’]

message = MIMEText(mailContent, ‘html’, ‘utf-8’)
if daily == 1:
message[‘From’] = Header("daily statistical", ‘utf-8’)
else:
message[‘From’] = Header("weekly statistical", ‘utf-8’)

message[‘To’] = Header("开发组", ‘utf-8’)

subject = "%s代码量统计" % todayStr
message[‘Subject’] = Header(subject, ‘utf-8’)

try:
smtpObj = smtplib.SMTP(‘192.168.1.1’)
smtpObj.sendmail(sender, receivers, message.as_string())
print "send success"
except smtplib.SMTPException:
print "Error: 无法发送邮件"
[/code]

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress