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

后端 未结 2 463
鱼传尺愫
鱼传尺愫 2020-12-24 09:27

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 Hud

2条回答
  •  孤独总比滥情好
    2020-12-24 09:44

    @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:///jenkins/job//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 "" | sed 's|.*.*\(.*\).*.*|\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=
    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 "" | sed 's|.*.*\(.*\).*.*|\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

提交回复
热议问题