Jenkins SCM插件,如何获得自上次构建以来每个更新文件的列表?

时间:2018-10-19 00:08:50

标签: jenkins

我有一个Jenkins管道构建,我想遍历自上次构建以来更新的所有文件。我环顾四周,认为当前构建对象的changeSets变量可能起作用。所以我尝试了这个:

def gitUrl = "git@gitlab.xxx.com:me/myrepo.git"
def gitResponse = dir(".") { checkout([
  $class: 'GitSCM', branches: [[name: '*/master']],
  userRemoteConfigs: [[url: gitUrl,credentialsId:'xxxkey']]
]) }
// don't do anything if nothing changed
if(gitResponse.GIT_COMMIT == gitResponse.GIT_PREVIOUS_COMMIT) {
  echo "No changes detected."
  return
}
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 "Change detected: ${entry}"
  }
}

但是我收到此错误:

Caused: java.io.NotSerializableException: hudson.plugins.git.GitChangeSetList

自上次构建以来,有什么办法列出每个更改的文件?

2 个答案:

答案 0 :(得分:1)

我已经创建了脚本化的管道,可以打印当前版本中引入的更改:

node {
    checkout([$class: 'GitSCM', branches: [[name: 'YOUR_BRANCH']],
     userRemoteConfigs: [[url: 'YOUR_GIT_URL']]])      
    stage('TEST') {
        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]
            entry.getAffectedPaths().each {
                echo "Change detected: ${it}"   
            }
          }
        }
    }
}

答案 1 :(得分:0)

要以更实用的方式解释Krzysztof的答案,您可以按以下方式获得一组修改后的路径:

def paths = currentBuild
  .changeSets.collectMany {
    it.items.collectMany {
      it.affectedPaths
    }
  }.unique()

然后可以使用echo paths.join('\n')进行检查。您可以在script步骤内的声明性管道中使用它。