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

2

10

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?

Polyglot answered 9/5, 2010 at 18:39 Comment(0)
P
7

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

Polyglot answered 9/5, 2010 at 19:55 Comment(0)
M
14

@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

Marathi answered 5/10, 2011 at 22:56 Comment(0)
P
7

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

Polyglot answered 9/5, 2010 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.