Chris F
Chris F

Reputation: 16673

In a Jenkins pipeline, how can I get the files that changed between the current build and last successful build?

I'm trying to expand the post How to get the changes since the last successful build in jenkins pipeline?. How can I get the files that changed between the current build and the last successful build in the change set instead of the log? I don't want the actual changes in each file, but only a list of files that changed. I want the equivalent of doing a

$ git diff commit#1 commit#2 --name-only

Note I modified this function to at least get the change sets between the current build and the last successful build. In my case, I set the currentBuild state to SUCCESS so it's counted as a passedBuild, hence the check for passedBuilds.size() < 2.

////////////////////////////////////////////////////////////////////
// Code to get change sets between the current and  last successful
// build
////////////////////////////////////////////////////////////////////
def lastSuccessfulBuild(passedBuilds, build) {
    if (build != null) {
        if (build.result != 'SUCCESS' || passedBuilds.size() < 2) {
          passedBuilds.add(build)
          lastSuccessfulBuild(passedBuilds, build.getPreviousBuild())
        }
    }
}

Thanks in advance.

Upvotes: 1

Views: 1017

Answers (1)

klubi
klubi

Reputation: 1523

This might not be exactly what you need, below method returns Set of files modified in commit that triggered current build. It will return empty list on re-run.

def getChangedFiles(passedBuilds) {
    def files = [] as Set // as Set assumes uniqueness
    passedBuilds.each {
        def changeLogSets = it.rawBuild.changeSets
        changeLogSets.each {
            it.items.each {
                it.affectedFiles.each {
                    files.add(it.path)
                }
            }
        }
    }
    echo "Found changes in files: ${files}"
    return files.toSorted()
}

Upvotes: 1

Related Questions