How to generate changelog: git log since last Hudson build?

浪尽此生 提交于 2019-11-29 23:21:30

@takeshin's answer is fine if you have access to the build.xml file, but this may break, especially if you are building on a slave node (since the slave does not have the referenced build.xml).

Fear not, since you can access this information via Jenkins directly, using its remote access api:

https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API

For example:

http://<host>/jenkins/job/<job_name>/lastSuccessfulBuild/api/xml

(will give you the xml content... you could replace xml with json to get json content back instead of XML, for example).

NOTE that you may need to use authentication if you've setup your Jenkins instance to require it. Again, fear not: https://wiki.jenkins-ci.org/display/JENKINS/Authenticating+scripted+clients

Then it's a simple matter of parsing the XML for what you want. Something like this, perhaps:

curl --silent --user $USER:$API_TOKEN $URL | grep "<lastBuiltRevision>" | sed 's|.*<lastBuiltRevision>.*<SHA1>\(.*\)</SHA1>.*<branch>.*|\1|'

So, pulling it all together, you can end up with a (relatively) simple shell script to retrieve the last good revision hash from Jenkins:

#!/bin/sh
GIT_LOG_FORMAT="%ai %an: %s"
USER=<username>
API_TOKEN=<api_token>

LAST_SUCCESS_URL_SUFFIX="lastSuccessfulBuild/api/xml"
#JOB_URL gets populated by Jenkins as part of the build environment
URL="$JOB_URL$LAST_SUCCESS_URL_SUFFIX"

LAST_SUCCESS_REV=$(curl --silent --user $USER:$API_TOKEN $URL | grep "<lastBuiltRevision>" | sed 's|.*<lastBuiltRevision>.*<SHA1>\(.*\)</SHA1>.*<branch>.*|\1|')
# Pulls all commit comments since the last successfully built revision
LOG=$(git log --pretty="$GIT_LOG_FORMAT" $LAST_SUCCESS_REV..HEAD)
echo $LOG

Cheers,

Levi

I have extracted last successful build date using bash:

git log --pretty="%s" --since="`date -r ./../lastSuccessful/build.xml "+%F %T"`"

(In xml file I had to replace " with &quote; entity).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!