11

In my Jenkins pipeline, I can get change logs of the current build by this.

def changeLogSets = currentBuild.changeSets

Is there a way to get all change-logs since last successful build?

9

You may try something like this:

node('my_kubernetes_pod') {
    passedBuilds = []

    lastSuccessfulBuild(passedBuilds, currentBuild);

    def changeLog = getChangeLog(passedBuilds)
    echo "changeLog ${changeLog}"
}

def lastSuccessfulBuild(passedBuilds, build) {
    if ((build != null) && (build.result != 'SUCCESS')) {
        passedBuilds.add(build)
        lastSuccessfulBuild(passedBuilds, build.getPreviousBuild())
    }
}

@NonCPS
def getChangeLog(passedBuilds) {
    def log = ""
    for (int x = 0; x < passedBuilds.size(); x++) {
        def currentBuild = passedBuilds[x];
        def changeLogSets = currentBuild.rawBuild.changeSets
        for (int i = 0; i < changeLogSets.size(); i++) {
            def entries = changeLogSets[i].items
            for (int j = 0; j < entries.length; j++) {
                def entry = entries[j]
                log += "* ${entry.msg} by ${entry.author} \n"
            }
        }
    }
    return log;
}

Additionally, you may try out "Changes Since Last Success Plugin": https://wiki.jenkins.io/display/JENKINS/Changes+Since+Last+Success+Plugin

But it will not work together with pipeline, this is a separate approach.

3
  • It seems this solution gives the change logs of last succesaful builds. What I actually was looking for was chnage logs (of last unsuccessful builds) since last successful build. But your answr is definitely helpful. Build.getpreviousebuuld() and recursion was what i had missed. Thank you very much for the answer. – Bee Oct 14 '17 at 5:58
  • 1
    Always welcome ;-) – user54 Oct 14 '17 at 6:33
  • I suspect that the recursive solution might run into problems running out of stack if you had it going through a lot of builds. Wouldn't be too hard to switch to an iterative process though. – drewish Oct 28 '17 at 18:46
1

As I found may be this will helps you. With this you can fetch latest successful build change log file of any project build with Jenkins.

$DirectoryA = "D:\Jenkins\jobs\projectName\builds"  ####Jenkind directory
$firstfolder = Get-ChildItem -Path $DirectoryA | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1
$DirectoryB = $DirectoryA + "\" + $firstfolder

$sVnLoGfIle = $DirectoryB + "\" + "changelog.xml" 

write-host $sVnLoGfIle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.