mmaceachran
mmaceachran

Reputation: 3348

Jenkins SCM plugin, how do I get a list of every updated file since last build?

I have a Jenkins pipeline build and I want to iterate over all of the files that were updated since the last build. I looked around and thought the changeSets variable of the current build object might work. so I tried this:

def gitUrl = "[email protected]:me/myrepo.git"
def gitResponse = dir(".") { checkout([
  $class: 'GitSCM', branches: [[name: '*/master']],
  userRemoteConfigs: [[url: gitUrl,credentialsId:'xxxkey']]
]) }
// don't do anything if nothing changed
if(gitResponse.GIT_COMMIT == gitResponse.GIT_PREVIOUS_COMMIT) {
  echo "No changes detected."
  return
}
def changeLogSets = currentBuild.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]
    echo "Change detected: ${entry}"
  }
}

But I am getting this error:

Caused: java.io.NotSerializableException: hudson.plugins.git.GitChangeSetList

Any way to list out each changed file since the last build?

Upvotes: 1

Views: 954

Answers (2)

laughedelic
laughedelic

Reputation: 6460

To paraphrase Krzysztof's answer in a more functional way, you can get the set of modified paths as follows:

def paths = currentBuild
  .changeSets.collectMany {
    it.items.collectMany {
      it.affectedPaths
    }
  }.unique()

You can check it then with echo paths.join('\n'). You can use it in a declarative pipeline inside of a script step.

Upvotes: 2

Krzysztof Błażełek
Krzysztof Błażełek

Reputation: 893

I've created scripted pipeline that prints changes introduced in current build:

node {
    checkout([$class: 'GitSCM', branches: [[name: 'YOUR_BRANCH']],
     userRemoteConfigs: [[url: 'YOUR_GIT_URL']]])      
    stage('TEST') {
        def changeLogSets = currentBuild.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]
            entry.getAffectedPaths().each {
                echo "Change detected: ${it}"   
            }
          }
        }
    }
}

Upvotes: 3

Related Questions