Jenkins: 获取文件变更列表、提交ID、提交人和提交信息

love374
发布于 2023-7-20 11:31
浏览
0收藏

Jenkins — Get the latest changed files list, Commit ID, AuthorName, and Commit Message

Jenkins: 获取文件变更列表、提交ID、提交人和提交信息-鸿蒙开发者社区

We sometimes get requirements such as “We need to run/execute this Jenkins job/stage” only based on changes from the previous build like below:
我们有时会得到诸如“我们需要运行/执行此 Jenkins 作业/阶段”之类的要求,仅基于对先前构建的更改,如下所示:

  • Files changes from the previous build to the current build. 文件从以前的版本更改为当前版本。
  • Commit message — for example, a particular word/pattern in the message.提交消息 — 例如,消息中的特定单词/模式。
  • Commit ID 提交标识
  • Author Name 作者姓名

If we use the native git commands via shell block, we always get the git differences between the last two commits, not the git changes between Jenkins builds.
如果我们通过 shell 块使用原生 git 命令,我们总是会得到最后两次提交之间的 git 差异,而不是Jenkins 构建之间的 git 更改。

In order to get expecting details, we can use the currentBuild.changeSets Jenkins environment variable like below.
为了获得预期的详细信息,我们可以使用currentBuild.changeSetsJenkins 环境变量,如下所示。

pipeline {
    agent any

    stages {
        stage('Get Last Commit Details') {
            steps {
                script{

                    List<String> changes = getChangedFilesList()
                    println ("Changed file list: " + changes)

                    String gitCommitId = getGitcommitID()
                    println("GIT CommitID: " + gitCommitID)

                    String gitCommitAuthorName = getAuthorName()
                    println("GIT CommitAuthorName: " + gitCommitAuthorName)

                    String gitCommitMessage = getCommitMessage()
                    println("GIT CommitMessage: " + gitCommitMessage)

                }
            }
        }
    }
}

@NonCPS
List<String> getChangedFilesList(){
    def changedFiles = []
    for ( changeLogSet in currentBuild.changeSets){
        for (entry in changeLogSet.getItems()){
            changedFiles.addAll(entry.affectedPaths)
        }
    }
    return changedFiles
}

@NonCPS
String getGitcommitID(){
    gitCommitID = " "
    for ( changeLogSet in currentBuild.changeSets){
        for (entry in changeLogSet.getItems()){
            gitCommitID = entry.commitId
        }
    }
    return gitCommitID
}

@NonCPS
String getAuthorName(){
    gitAuthorName = " "
    for ( changeLogSet in currentBuild.changeSets){
        for (entry in changeLogSet.getItems()){
            gitAuthorName = entry.authorName
        }
    }
    return gitAuthorName
}

@NonCPS
String getCommitMessage(){
    commitMessage = " "
    for ( changeLogSet in currentBuild.changeSets){
        for (entry in changeLogSet.getItems()){
            commitMessage = entry.msg
        }
    }
    return commitMessage
}

Once you get the required values, we can utilize them as data sources to control the flow of our Jenkins pipeline.
获得所需的值后,我们可以利用它们作为数据源来控制 Jenkins 管道的流程。

Note: People often take changeSets for what they aren’t. is the list of files that was modified between this build and the previous build only. If the previous one failed and was re-triggered, changeSet would be empty. You may want to get a list of changes for the given branch.changeSet

注意:人们经常将 changeSet 视为他们不了解的内容。是在此构建和上一个构建之间修改的文件列表。如果前一个失败并被重新触发,则 changeSet 将为空。您可能希望获取给定分支的更改列表。



DevOps云学堂,一个盛满新技术实践的学习平台。技术开放交流,技术实践应用分享。目标前课程正正在一步步覆盖DevOps全面!


文章转载自公众号:DevOps云学堂

分类
标签
已于2023-7-20 11:31:30修改
收藏
回复
举报
回复
    相关推荐