前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Jenkins: 获取文件变更列表、提交ID、提交人和提交信息

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

作者头像
DevOps云学堂
发布2022-12-29 17:15:27
2.1K0
发布2022-12-29 17:15:27
举报

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

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 将为空。您可能希望获取给定分支的更改列表。


本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-11-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DevOps云学堂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档