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

本文介绍了如何在Jenkins pipeline中利用currentBuild.changeSets环境变量来获取最新变更的文件列表、Commit ID、作者名和提交消息。这些信息对于基于代码变更控制 Jenkins 作业的执行至关重要。通过示例代码展示了如何获取这些详细信息,并提醒读者changeSets只包含当前构建与前一次构建之间的差异,不一定是与上一个成功构建的对比。
摘要由CSDN通过智能技术生成

Jenkins — Get the latest changed files list, Commit ID, AuthorName, and Commit Message
7429a2b031594e4b4e60ad85e4e17f56.png

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全面!

7c862e8a314dadd45bbf1148962213d9.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值