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

问题描述 投票:1回答:3

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

我试过了

git diff $PREVIOUS_COMMIT $COMMIT

但他们有相同的SHA。

git jenkins jenkins-pipeline
3个回答
3
投票

根据this article at CloudBees,从workflow-support Plugin 2.2版本开始,您可以在管道内访问此类信息,而不使用白名单(使用Sandbox /脚本安全性,与我的其他答案相比):

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
投票

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

仅供参考:Pipeline Supporting 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}"
        }
    }
}

Pipeline Supporting API插件早于2.2

您可以使用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}"
        }
    }
}

0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.