Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using Phing to do post build tasks in Hudson.

I want to generate changelog containing all commits since last successful Hudson build. But looks like neither Hudson nor Git plugin for Hudson does not provide %last_build_time% variable.

This would be satisfying solution, (but how to get the time?):

git log --pretty="%s" --since="%last_build_time%"

The only way I see for now is extracting it from the job xml file, but I do not know if it is possible with Phing.

How do you generate your change logs?

like image 937
takeshin Avatar asked May 09 '10 18:05

takeshin


2 Answers

@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

like image 191
levigroker Avatar answered Sep 19 '22 21:09

levigroker


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).

like image 36
takeshin Avatar answered Sep 20 '22 21:09

takeshin