简体   繁体   English

在 Jenkins 管道中,如何获取在当前构建和上次成功构建之间更改的文件?

[英]In a Jenkins pipeline, how can I get the files that changed between the current build and last successful build?

I'm trying to expand the post How to get the changes since the last successful build in jenkins pipeline?我正在尝试扩展自上次在 jenkins 管道中成功构建以来如何获得更改的帖子? . . How can I get the files that changed between the current build and the last successful build in the change set instead of the log?如何获取更改集中当前构建和上次成功构建之间更改的文件而不是日志? I don't want the actual changes in each file, but only a list of files that changed.我不想要每个文件中的实际更改,而只想要更改的文件列表。 I want the equivalent of doing a我想要相当于做一个

$ git diff commit#1 commit#2 --name-only

Note I modified this function to at least get the change sets between the current build and the last successful build.注意我修改了这个函数以至少获取当前构建和上次成功构建之间的更改集。 In my case, I set the currentBuild state to SUCCESS so it's counted as a passedBuild , hence the check for passedBuilds.size() < 2 .在我的例子中,我将currentBuild状态设置为SUCCESS所以它被算作passedBuild ,因此检查passedBuilds.size() < 2

////////////////////////////////////////////////////////////////////
// Code to get change sets between the current and  last successful
// build
////////////////////////////////////////////////////////////////////
def lastSuccessfulBuild(passedBuilds, build) {
    if (build != null) {
        if (build.result != 'SUCCESS' || passedBuilds.size() < 2) {
          passedBuilds.add(build)
          lastSuccessfulBuild(passedBuilds, build.getPreviousBuild())
        }
    }
}

Thanks in advance.提前致谢。

This might not be exactly what you need, below method returns Set of files modified in commit that triggered current build.这可能不是您所需要的,下面的方法返回触发当前构建的提交中修改的文件集。 It will return empty list on re-run.它将在重新运行时返回空列表。

def getChangedFiles(passedBuilds) {
    def files = [] as Set // as Set assumes uniqueness
    passedBuilds.each {
        def changeLogSets = it.rawBuild.changeSets
        changeLogSets.each {
            it.items.each {
                it.affectedFiles.each {
                    files.add(it.path)
                }
            }
        }
    }
    echo "Found changes in files: ${files}"
    return files.toSorted()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM