10

In a Jenkins build I see a list of changed files:

enter image description here

So which command Jenkins uses to get this list (I am using git for repository version control).

Men X
  • 153
  • 1
  • 2
  • 8

2 Answers2

16

You can use the changeSets property of the currentBuild global variable to get information relating to the detected changes of the current build.

e.g.

// returns a list of changed files
@NonCPS
String getChangedFilesList() {
    changedFiles = []
    for (changeLogSet in currentBuild.changeSets) {
        for (entry in changeLogSet.getItems()) { // for each commit in the detected changes
            for (file in entry.getAffectedFiles()) {
                changedFiles.add(file.getPath()) // add changed file to list
            }
        }
    }
    return changedFiles
}
pppery
  • 3,731
  • 22
  • 33
  • 46
  • which language that you are using? Could I write this in Linux Bash script? – Men X Jan 14 '19 at 07:13
  • 1
    This is groovy code which is typically used in a [scripted Pipeline](https://jenkins.io/doc/book/pipeline/syntax/#scripted-pipeline). It can also be executed using the 'execute Groovy script' step in the Build section of a Freestyle job. – TheDukeOfKirkcaldy Jan 14 '19 at 07:45
  • @MenX it is better to use `Groovy script` if you have to run that code on win and linux. – Daniel Jan 22 '21 at 15:22
  • 3
    Thanks for the code, it works well, but the return type should be `List`, not `String`, and since Jenkins/Groovy is silently converting to String, it took me a while to spot what was wrong. – Alessandro S. Feb 05 '21 at 15:23
  • How can I get this list of files (changedFiles) to display or into another script in a declaritive pipeline? – Zak Keirn Jul 28 '21 at 21:27
  • can currentBuild also return the git hash? – red888 Aug 05 '21 at 00:24
  • @red888 SCM-specific variables such as GIT_COMMIT are not automatically defined as environment variables; rather you can use the return value of the checkout step. – Krishnom Jul 19 '22 at 04:32
  • Saved me a day. Works flawlessly. – Krishnom Jul 19 '22 at 04:33
5

Possibly one of the two possible commands listed in "How to list all the files in a commit?":

git diff-tree --no-commit-id --name-only -r <commit-ish>

Note that the Jenkins Git plugin does expose that commit as an environment variable: GIT_COMMIT

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    if build has multiple commits? how to go for that case? – user_9090 Mar 13 '19 at 06:59
  • @user_9090 `git diff-tree` (https://git-scm.com/docs/git-diff-tree) can take *two* : simply diff between the older and newest commit displayed in Jenkins build list of commits. – VonC Mar 13 '19 at 07:16
  • in case of Jenkins first build, we will not have GIT_PREVIOUS_COMMIT, How can we approach for this? – user_9090 May 03 '19 at 06:49
  • @user_9090 then you need to fallback to a convention, like ~1: the parent commit. That is an approximation though. Or you could differ against the empty tree (https://stackoverflow.com/a/9766506/6309) to get *all* files created so far. – VonC May 03 '19 at 07:09
  • is there a way in which we can calculate total commits in a Jenkins build? – user_9090 May 03 '19 at 07:13
  • @user_9090 if you have th GIT_PROVIOUS_COMMIT, then yes, you can count commits between that reference and GIT_COMMIT: https://stackoverflow.com/a/31998123/6309 – VonC May 03 '19 at 08:11
  • yes, but for Jenkins first build it (GIT_PREVIOUS_COMMIT) won't be available. Is there any way for the first build. – user_9090 May 03 '19 at 09:36
  • @user_9090 Sure: you can get first to the first commit: https://stackoverflow.com/a/5189296/6309. Then you can count between that first commit and your current "first" Jenkins Git commit. – VonC May 03 '19 at 13:08
  • this is giving me the first commit of the repo – user_9090 May 06 '19 at 07:27
  • @user_9090 Yes, as an approximation, since the Jenkins first build has no previous build. – VonC May 06 '19 at 07:37
  • so, for the jenkins first build we cannot get the files that are being changed – user_9090 May 06 '19 at 07:42
  • @user_9090 You can, but only by listing said file since the first commit of the repo. – VonC May 06 '19 at 07:53
  • for that I have to use this command only "git rev-list --max-parents=0 HEAD" , or any other way – user_9090 May 06 '19 at 07:57
  • @user_9090 yes, that command is the recommended way, except you might replace HEAD by GIT_COMMIT, to reference the commit used by Jenkins for its (first) build. – VonC May 06 '19 at 07:59