需求
项目需要在jenkins自动发布中加入version.html来跟踪项目发布的版本信息,需要获取到jira中当前sprint的名字,和一个sprint中的发布次数(我这里用文件把次数存起来的傻方法来计数的)。
shell初学者脚本写的很烂,还望大神们指教。 - -!
方法
直接使用curl 向jira发送get请求,将返回的json通过jq过滤出来存入currentSprint.txt文件中。
curl -u JiraUserName:Password -X GET -H "Content-Type: application/json"
'http://jira.jintelhealth.com/rest/greenhopper/1.0/sprintquery/13?includeFutureSprints=true&includeHistoricSprints=false'
| /usr/bin/jq '.sprints[] | select(.name | contains("PD")) | select(.state == "ACTIVE") | .name'|sed 's/"//g'> currentSprint.txt
注意:
这里在linux下需要安装 jq linux json格式化工具
添加epel源
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
yum repolist
yum install jq
总结
读取文件 $(cat file.txt)
除去name中的双引号 name'|sed 's/"//g'
shell中数字求和 echo $(( 1 + 2 )) => 3
代码
############PD-2375#############################
cd $PWD
if [ ! -f "${PWD}/LastBuildSprint.txt" ];then
echo 0 > ${PWD}/LastBuildSprint.txt
fi
curl -u JiraUserName:Password -X GET -H "Content-Type: application/json"
'http://jira.jintelhealth.com/rest/greenhopper/1.0/sprintquery/13?includeFutureSprints=true&includeHistoricSprints=false'
| /usr/bin/jq '.sprints[] | select(.name | contains("PD")) | select(.state == "ACTIVE") | .name'|sed 's/"//g'> currentSprint.txt
if [ "$(cat ./currentSprint.txt)" == "$(cat ./LastBuildSprint.txt)" ];
then
echo $(($(cat ./SprintBuildNumber.txt)+1)) > ./SprintBuildNumber.txt
else
echo 1 > ${PWD}/SprintBuildNumber.txt
fi
cp -rf ${PWD}/currentSprint.txt ${PWD}/LastBuildSprint.txt
###########generate version.html#################
rm -f ${PWD}/version.html
echo "remove successfully!"
{
echo "<html>"
echo "<head>"
echo "<title>Analytics: Version</title>"
echo "</head>"
echo "<body bgcolor="#ffffff">"
echo "<span id="analytics_version">Version: $(cat ./currentSprint.txt) B$(cat ./SprintBuildNumber.txt)</span>"
echo "<br /><br />"
echo "<span id="tag">BUILD INFO: Jenkins Build ${BUILD_NUMBER} `date`</span>"
echo "<br /><br />"
echo "<span id="commit">COMMIT SHA: ${GIT_COMMIT}</span>"
echo "<br /><br />"
echo "</body></html>"
}>> version.html
############PD-2375#############################
脚本参考自 http://kagarlickij.com/get-active-jira-sprint-name-cli/