如何获取管道jenkins中所有修改过的文件的列表?

时间:2018-05-20 17:33:38

标签: git jenkins jenkins-pipeline

我使用multibranch管道,我需要获取修改过的文件列表。

我试过

git diff $PREVIOUS_COMMIT $COMMIT

但他们有相同的SHA。

4 个答案:

答案 0 :(得分:3)

根据this article at CloudBees,您可以在管道内访问此类信息,无需使用白名单(使用Sandbox /脚本安全性,与我的其他答案相比),从workflow-support Plugin版本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}"
        }
    }
}

答案 1 :(得分:2)

我猜这种解决方案仅列出了当前版本中的更改,在大多数情况下,这在DevOps中没有用。我们需要的是上次成功构建后的变更集。我在这里找到了另一个解决方案: Jenkins管道-一种自上次成功构建以来获取所有提交的方法: https://gist.github.com/ftclausen/8c46195ee56e48e4d01cbfab19c41fc0

答案 2 :(得分:1)

参考链接:https://support.cloudbees.com/hc/en-us/articles/217630098

仅供参考: 管道支持API插件2.2或更高版本

您可以在沙盒构建中使用currentBuild.changeSets,如以下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}"
        }
    }
}

低于2.2的管道支持API插件

您可以使用currentBuild.rawBuild.changeSets,但是无法从沙箱中访问它。以下是用于非沙盒化构建的Git示例:

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}"
        }
    }
}

答案 3 :(得分:0)

您可以通过currentBuild变量访问此类信息(在白名单API调用之后):

currentBuild.rawBuild.getChangeSets().each { cs ->
  cs.getItems().each { item ->
    item.getAffectedFiles().each { f ->
      println f
    }
  }
}

我自己未经测试(但有道理)。来源:lsjostro's Gist