Reputation: 843
I have a git repository which contains multiple projects, and I want to trigger specific builds depending on which directories have changed. I'm using the following code to get the list of files and trigger further builds:
echo("Number of change sets: " + currentBuild.changeSets.size()) //Always prints 1?
def changedFiles = currentBuild.changeSets
.collect{ it.getItems() }
.collect{ it.getAffectedPaths() }
.flatten()
echo("Changed files: ${changedFiles}")
def changedDirectories = changedFiles.collect {
if(it.contains("$interestingDirectoryName")) { //Code cut for simplicity
return it.split('/')[0]
}
return ""
}
.unique()
echo("Changed directories: ${changedDirectories}")
changedDirectories.each {
if(!it.isEmpty()) {
def buildTrigger = "${it}/${env.BRANCH_NAME}"
echo("Triggering build for ${buildTrigger}")
build(job: buildTrigger, wait: false)
}
}
This code works mostly fine, but I've noticed that when making multiple commits, only the files changed in the first commit are shown (and triggered).. I added the first echo line to the script to help me spot the bug in my script, but was surprised to see that it always tells me that there is only 1 changeset, no matter how many commits I make. Is this expected behaviour, and if so, is there another way to get the full list of commits, rather than just the first one?
Upvotes: 0
Views: 1086
Reputation: 843
I was missing a flatten()
after the first collect{}
.
Altering the first bit of code to the following fixed it:
def changedFiles = currentBuild.changeSets
.collect{ it.getItems() }
.flatten() //Ensures that we look through each commit, not just the first.
.collect{ it.getAffectedPaths() }
.flatten()
.toSet() //Ensures uniqueness.
echo("Changed files: ${changedFiles}")
Upvotes: 1