I'm using multibranch pipeline and i need to get the list of modified files.
i tried
git diff $PREVIOUS_COMMIT $COMMIT
but they have the same SHA.
I'm using multibranch pipeline and i need to get the list of modified files.
i tried
git diff $PREVIOUS_COMMIT $COMMIT
but they have the same SHA.
According to this article at CloudBees, you can access such information inside a pipeline also without white-listing (using Sandbox / script security, compared to my other answer), starting from workflow-support Plugin version 2.2:
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 "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}"
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
echo " ${file.editType.name} ${file.path}"
}
}
}
Reference link: https://support.cloudbees.com/hc/en-us/articles/217630098
FYI: Pipeline Supporting APIs Plugin 2.2 or above
You can use currentBuild.changeSets in a sandboxed build as you could see in the following example of Git:
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 "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}"
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
echo " ${file.editType.name} ${file.path}"
}
}
}
Pipeline Supporting APIs Plugin older than 2.2
You can use currentBuild.rawBuild.changeSets but this is not accessible from the sandbox. Following is an example of Git for a non-sandboxed build:
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]
echo "${entry.commitId} by ${entry.author} on ${new Date(entry.timestamp)}: ${entry.msg}"
def files = new ArrayList(entry.affectedFiles)
for (int k = 0; k < files.size(); k++) {
def file = files[k]
echo " ${file.editType.name} ${file.path}"
}
}
}
You can access such information (after white-listing the API calls) via the currentBuild
variable:
currentBuild.rawBuild.getChangeSets().each { cs ->
cs.getItems().each { item ->
item.getAffectedFiles().each { f ->
println f
}
}
}
Untested by myself (but makes sense). Source: lsjostro's Gist.
I guess this solution only list the changes in the current build, which in most of cases, is not useful in DevOps. What we need is the change set from the last successful build. I found another solution here: Jenkins pipeline - An approach to get all commits since the last successful build: https://gist.github.com/ftclausen/8c46195ee56e48e4d01cbfab19c41fc0