4

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.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
Hinde
  • 69
  • 1
  • 2
  • 8
  • Possible duplicate of [How to get list of changed files since last build in Jenkins/Hudson](https://stackoverflow.com/questions/6260383/how-to-get-list-of-changed-files-since-last-build-in-jenkins-hudson) – Mahmoud May 20 '18 at 18:47
  • @Mahmoud this is not necessary a duplicate. There are additional possibilities with Jenkins Pipelines. – StephenKing May 20 '18 at 18:58
  • @Mahmoud, no this is not a duplicate. I already check the proposed solutions on the link you provide. – Hinde May 21 '18 at 09:32
  • You're right, sorry for the bad intervention – Mahmoud May 21 '18 at 11:16

4 Answers4

11

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}"
        }
    }
}
StephenKing
  • 36,187
  • 11
  • 83
  • 112
3

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}"
        }
    }
}
xin
  • 161
  • 4
2

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.

StephenKing
  • 36,187
  • 11
  • 83
  • 112
2

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

Alex
  • 180
  • 2
  • 3
  • 10