How to get list of changed files since last build in Jenkins/Hudson

后端 未结 11 1941
时光取名叫无心
时光取名叫无心 2020-11-28 09:35

I have set up Jenkins, but I would like to find out what files were added/changed between the current build and the previous build. I\'d like to run some long running tests

相关标签:
11条回答
  • 2020-11-28 10:30

    The CI server will show you the list of changes, if you are polling for changes and using SVN update. However, you seem to want to be changing the behaviour of the build depending on which files were modified. I don't think there is any out-of-the-box way to do that with Jenkins alone.

    A post-commit hook is a reasonable idea. You could parameterize the job, and have your hook script launch the build with the parameter value set according to the changes committed. I'm not sure how difficult that might be for you.

    However, you may want to consider splitting this into two separate jobs - one that runs on every commit, and a separate one for the long-running tests that you don't always need. Personally I prefer to keep job behaviour consistent between executions. Otherwise traceability suffers.

    0 讨论(0)
  • 2020-11-28 10:31

    I have done it the following way. I am not sure if that is the right way, but it seems to be working. You need to get the Jenkins Groovy plugin installed and do the following script.

    import hudson.model.*;
    import hudson.util.*;
    import hudson.scm.*;
    import hudson.plugins.accurev.*
    
    def thr = Thread.currentThread();
    def build = thr?.executable;
    
    def changeSet= build.getChangeSet();
    
    changeSet.getItems();
    

    ChangeSet.getItems() gives you the changes. Since I use accurev, I did List<AccurevTransaction> accurevTransList = changeSet.getItems();.

    Here, the modified list contains duplicate files/names if it has been committed more than once during the current build window.

    0 讨论(0)
  • 2020-11-28 10:31

    Using the Build Flow plugin and Git:

    final changeSet = build.getChangeSet()
    final changeSetIterator = changeSet.iterator()
    while (changeSetIterator.hasNext()) {
      final gitChangeSet = changeSetIterator.next()
      for (final path : gitChangeSet.getPaths()) {
        println path.getPath()
      }
    }
    
    0 讨论(0)
  • 2020-11-28 10:31

    With Jenkins pipelines (pipeline supporting APIs plugin 2.2 or above), this solution is working for me:

    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]
        def files = new ArrayList(entry.affectedFiles)
        for (int k = 0; k < files.size(); k++) {
          def file = files[k]
          println file.path
        }
      }
    }
    

    See How to access changelogs in a pipeline job.

    0 讨论(0)
  • 2020-11-28 10:34

    Note: You have to use Jenkins' own SVN client to get a change list. Doing it through a shell build step won't list the changes in the build.

    0 讨论(0)
提交回复
热议问题