获取自Jenkins Pipeline上次成功构建以来的所有更改日志


11

在我的Jenkins管道中,由此可以获取当前构建的变更日志。

def changeLogSets = currentBuild.changeSets

自上次成功构建以来,是否有办法获取所有变更日志?

Answers:


9

您可以尝试如下操作:

node('my_kubernetes_pod') {
    passedBuilds = []

    lastSuccessfulBuild(passedBuilds, currentBuild);

    def changeLog = getChangeLog(passedBuilds)
    echo "changeLog ${changeLog}"
}

def lastSuccessfulBuild(passedBuilds, build) {
    if ((build != null) && (build.result != 'SUCCESS')) {
        passedBuilds.add(build)
        lastSuccessfulBuild(passedBuilds, build.getPreviousBuild())
    }
}

@NonCPS
def getChangeLog(passedBuilds) {
    def log = ""
    for (int x = 0; x < passedBuilds.size(); x++) {
        def currentBuild = passedBuilds[x];
        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]
                log += "* ${entry.msg} by ${entry.author} \n"
            }
        }
    }
    return log;
}

此外,您可以尝试“自上次成功插件以来的更改”:https : //wiki.jenkins.io/display/JENKINS/Changes+Since+Last+Success+Plugin

但是它不能与管道一起使用,这是一种单独的方法。


看来此解决方案提供了最后成功构建的更改日志。我真正要寻找的是自上次成功构建以来的变更日志(上次未成功构建)。但是您的回答肯定有帮助。Build.getpreviousebuuld()和递归是我所错过的。非常感谢您的回答。
蜜蜂

1
永远欢迎;-)
user54

我怀疑如果您要进行大量构建,则递归解决方案可能会遇到堆栈用尽的问题。不过,切换到迭代过程并不难。
drewish

1

我发现可能对您有帮助。这样,您可以获取使用Jenkins构建的任何项目的最新成功构建更改日志文件。

$DirectoryA = "D:\Jenkins\jobs\projectName\builds"  ####Jenkind directory
$firstfolder = Get-ChildItem -Path $DirectoryA | Where-Object {$_.PSIsContainer} | Sort-Object LastWriteTime -Descending | Select-Object -First 1
$DirectoryB = $DirectoryA + "\" + $firstfolder

$sVnLoGfIle = $DirectoryB + "\" + "changelog.xml" 

write-host $sVnLoGfIle
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.