简体   繁体   English

如何在 Jenkins 管道内使用 GitHub 提交消息和提交 ID?

[英]How to use GitHub commit message and commit id inside Jenkins pipeline?

I am using DevOps model, where i have built a pipeline for code build and deploy.我正在使用 DevOps 模型,我在其中构建了一个用于代码构建和部署的管道。 In the entire process i want to log the Git commit id and commit message for that specific change commits.在整个过程中,我想记录该特定更改提交的 Git 提交 ID 和提交消息。

@shruthibhaskar
shruthibhaskar committed just now 
1 parent 51132c4 commit aedd3dc56ab253419194762d72f2376aede66a19

and commit message and description as below并提交消息和描述如下

test commit 3

test commit desc 3

how do i access these values of commit inside my jenkins pipeline, where i have configured a webhook for SCM polling?我如何在我的 jenkins 管道中访问这些提交值,在那里我为 SCM 轮询配置了一个 webhook?

git log --format=format:%s -1 (latest commit) git log --format=format:%s -1 (最新提交)

git log --format=format:%s -1 ${GIT_COMMIT} (specific commit) git log --format=format:%s -1 ${GIT_COMMIT} (具体提交)

git --no-pager show -s --format='%s' ${GIT_COMMIT}

Jenkins git plugin set some environment variables for every build. Jenkins git 插件为每个构建设置了一些环境变量。 You can find the list of them in git plugin site .你可以在git plugin site 中找到它们的列表。 In which it gives the SHA of the current commit in ${GIT_COMMIT} environment variable.它在${GIT_COMMIT}环境变量中给出了当前提交的 SHA。

You can use the SHA along with git log to print commit message and any other details you required with --pretty option.您可以使用 SHA 和 git log 来打印提交消息和您需要使用 --pretty 选项的任何其他详细信息。

git log --oneline -1 ${GIT_COMMIT} # prints SHA and title line
git log --format="medium" -1 ${GIT_COMMIT} # print commit, author, date, title & commit message

You can make a message based on currentBuild.changeSets , for example:您可以根据currentBuild.changeSets制作消息,例如:

@NonCPS
def sendChangeLogs() {
    def commitMessages = ""
    def formatter = new SimpleDateFormat('yyyy-MM-dd HH:mm')
    def changeLogSets = currentBuild.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]
            commitMessages = commitMessages + "${entry.author} ${entry.commitId}:\n${formatter.format(new Date(entry.timestamp))}: *${entry.msg}*\n" 
        }
    }
    slackSend color: "good", message: "Job: `${env.JOB_NAME}`. Starting build with changes:\n${commitMessages}"
}

And call it:并称之为:

stage('Clone repository') {
    steps {
        checkout scm

        script {
            sendChangeLogs()
        }
    }
}

PS聚苯乙烯

@NonCPS needs to except serialization error. @NonCPS需要@NonCPS序列化错误。 I've found it here我在这里找到

you can curl the api with personal token for gitlab scm to get the commit message & numerous other information.您可以使用 gitlab scm 的个人令牌卷曲 api 以获取提交消息和许多其他信息。 Here you need create personal token and save it in jenkins via secret text & then use it in pipeline stages.在这里,您需要创建个人令牌并通过秘密文本将其保存在 jenkins 中,然后在管道阶段使用它。

curl --header "Private-Token: asdfasdfasdf" https://gitlab.com/api/v4/projects/2445653/repository/commits/master

you can check out the documentation for more information.您可以查看文档以获取更多信息。 https://docs.gitlab.com/ee/api/commits.html https://docs.gitlab.com/ee/api/commits.html

enter image description here在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM