如何获取pull请求的Jenkinsfile中已更改文件的列表?

如何获取pull请求的Jenkinsfile中已更改文件的列表?,jenkins,jenkins-pipeline,jenkins-plugins,jenkins-groovy,jenkins-cli,Jenkins,Jenkins Pipeline,Jenkins Plugins,Jenkins Groovy,Jenkins Cli,在pull请求生成期间,是否可以在Jenkins文件中获取已更改文件的列表?我现在正在做这个 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+

在pull请求生成期间,是否可以在Jenkins文件中获取已更改文件的列表?我现在正在做这个

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 
            }
        }
    }
def changeLogSets=currentBuild.rawBuild.changeSets
对于(int i=0;i
但是,如果我第一次构建一个新的分支,这个方法不会返回任何更改,因为没有以前的构建可供比较。有人找到解决办法了吗


谢谢

据我所知,没有内置的功能来执行此操作

幸运的是,您可以通过编程方式找到答案:

    def local_branch = sh (
        script: "git rev-parse --abbrev-ref HEAD",
        label: "Getting current branch name",
        returnStdout: true
    ).trim()
    println "Local branch is ${local_branch}"

    def base_branch = 'master' 
    // This is very naive.
    // In reality, you need a better way to find out what your base branch is.
    // One way is to have a file with a name of a base branch.
    // Another one is to invoke API, e.g. GitHub API, to find out base branch.
    // Use whatever works for you.
    println "Base branch is ${base_branch}"

    sh script: "git fetch origin --no-tags ${base_branch}", label: "Getting base branch"

    def git_diff = sh (
        script: "git diff --name-only origin/${base_branch}..${local_branch}",
        returnStdout: true
    ).trim()
现在,在
git_diff
变量中有一个已更改文件的列表


使用Jenkins变更集基本上不起作用,并且被保留用于其他目的。

这也可以通过插件实现:


您是否可以先手动构建,然后在下次继续?这里使用的是哪个源代码管理,也许您可以使用它的API来查找信息?感谢您的回复!如果我事先知道分支的名称,这会有什么变化吗?然后你可以将名称放入
base\u branch
local\u branch
变量中,并去掉获取它们的代码。
def changedFiles = pullRequest.files.collect {
    it.getFilename()
}