Jenkinsfile,获取PR中所有修改过的文件

ext*_*mkv 5 git continuous-integration jenkins jenkins-pipeline

我想在 PR 中有一个已修改文件的列表,我尝试了一些解决方案,但我只能访问推送到远程的最后一次提交中的修改文件。

例子:

  • 用 3 个提交创建一个 PR,一个新的 jenkins 工作开始;
  • 修改后的文件列表包括 3 次提交的所有文件;
  • 构建失败,将带有修复的新提交推送到 PR,一个新的 jenkins 作业开始;
  • 修改文件列表只包含新提交的文件;

因此,在此示例中,我希望获得 PR 中所有新提交的文件列表。

代码:

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

ext*_*mkv 3

我使用 git 命令找到了解决方案:

/**
 * Compares the current branch and target branch and extract the changed files.
 *
 * @return the PR changed files.
 */
def getPRChangelog() {
    return sh(
            script: "git --no-pager diff origin/${params.target} --name-only",
            returnStdout: true
    ).split('\n')
}
Run Code Online (Sandbox Code Playgroud)