简体   繁体   English

自上次成功构建以来,如何使用Groovy获取有关Jenkins的提交信息?

[英]How to get commits information on Jenkins using Groovy since last successful build?

自上次成功构建以来,如何编写自定义的Groovy脚本以轻松地操纵所有提交中的数据?

Add new build step after gradle/maven step -> Execute system groovy script. 在gradle / maven步骤后->​​执行系统常规脚本,添加新的构建步骤。

Adaptable code: 适应性代码:

import com.tikal.jenkins.plugins.multijob.*
import hudson.*
import hudson.model.*
import hudson.plugins.git.*
import hudson.slaves.*
import hudson.tasks.*

def ln = System.getProperty('line.separator')
println "---------------Groovy Changelog script Started---------------$ln"

def lastSuccesfulBuild = build.previousNotFailedBuild
def failed = build.result != hudson.model.Result.SUCCESS

println "Last Succesful Build: ${lastSuccesfulBuild}"
println "Current Build Result, is failed?: ${failed}"


def currResult = build.result
def prevResult = build.previousBuild?.result ?: null

def consecutiveSuccess = currResult == hudson.model.Result.SUCCESS && prevResult == hudson.model.Result.SUCCESS

def builds = []
def changes = []
def count = 0



if (consecutiveSuccess) {
    println "Last Build was sucessful, getting latest changes$ln"

    builds << build
    def changeItems = build.changeSet.items
    println "Change Items: ${changeItems}$ln"

    count += changeItems.length
    changes += changeItems as List
} else {
    println "Last Build was not sucessful, getting changes from all failed build as well$ln"

    println "BUILD: $build$ln"

    println "Hudson version: $build.hudsonVersion$ln"

    println "Change set: $build.changeSet$ln"

    println "Change set items: $build.changeSet.items$ln"

    while (lastSuccesfulBuild) {
        builds << lastSuccesfulBuild
        def changeSet = lastSuccesfulBuild.changeSet
        if (!changeSet.emptySet) {
            def changeItems = lastSuccesfulBuild.changeSet.items
            count += changeItems.length
            changes += changeItems as List
        }
        lastSuccesfulBuild = lastSuccesfulBuild.nextBuild
    }
}

def file = new File(build.getEnvVars()["WORKSPACE"] + '\\changelog')
file.delete()
file = new File(build.getEnvVars()["WORKSPACE"] + '\\changelog')

if (count ==0){
    file << "No changes.$ln"
}

changes.each { item ->
    println "item: $item$ln"
    println "author: $item.authorName$ln"
    println "msg: $item.msg$ln"
    println "id: $item.id$ln"
    println "revision: $item.revision$ln"
    println "comment: $item.comment$ln"
    println "commentAnnotated: $item.commentAnnotated$ln"
    println "affectedFiles: $item.affectedFiles$ln"
    println "affectedPaths: $item.affectedPaths$ln"
    println "commitId: $item.commitId$ln"
    println "timestamp: $item.timestamp$ln"
    println "date: $item.date$ln"

    file << "Commit ID: $item.id, by $item.author on $item.date, timestamp: $item.timestamp$ln"
    file << "$item.comment$ln"

    item.affectedFiles.each { cl -> 
        println "editType: $cl.editType.description$ln"
        println "changeSet: $cl.changeSet$ln"
        println "path: $cl.path$ln"
        println "src: $cl.src$ln"
        println "dst: $cl.dst$ln"

        file << "$cl.editType.description: $cl.path$ln"
    }
    file << "$ln"
}

println "---------------Groovy Changelog script Finished---------------$ln"

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

相关问题 自上次成功构建以来,如何在jenkins中包含带有电子邮件的git changelog? - How to include git changelog w/ email with jenkins since last successful build? Jenkins:从上次成功构建中获取分支 - Jenkins: Get branch from last successful build 如何从git的最后一个标签开始合并提交 - How to get commits merged since last tag with git 自上次使用git / gerrit在Jenkins / Hudson中生成以来,如何获取已更改文件的列表 - How to get list of changed files since last build in Jenkins/Hudson with git/gerrit Jenkins:如何检查 PullRequest 文件夹中的文件自上次构建后是否更改? - Jenkins: How to check if files in a folder of the PullRequest changed since the last build? 获取自上次标记以来的所有git提交 - Get all git commits since last tag 使用Jenkins将最后成功的构建上传到Fabric / Crashlytics - Upload last successful build to Fabric/Crashlytics with Jenkins VSTS:如何获取自上一次成功发布到生产以来的所有链接工作项? - VSTS: How to get all linked work items since last successful release to production? libgit2sharp 获取自上次推送以来的所有提交 - libgit2sharp get all commits since the last push 如何查找自上次推送以来在分支上推送的最后一次提交? - How to find the last commits that were pushed on a branch since the prior push?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM