1

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.

Chris F
  • 14,337
  • 30
  • 94
  • 192

1 Answers1

1

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()
}
Chris F
  • 14,337
  • 30
  • 94
  • 192
klubi
  • 1,373
  • 1
  • 14
  • 25
  • Yeah, not quite what I was looking for, but I can work with that. I can collect all the `affectedFiles` for each build in this case, and put them through a unique and sort filters. Thanks. – Chris F Jan 01 '20 at 19:04
  • I edited @klubi's answer to do exactly what I wanted. – Chris F Jan 01 '20 at 19:30
  • 1
    I think that ```def files = [] as Set``` assures unique entries in collection, so calling ```unique()``` should not be necessary. – klubi Jan 01 '20 at 19:47