Jenkins 和 Git - 如何获取提交者的名字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28311341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:44:24  来源:igfitidea点击:

Jenkins and Git - how to grab a committer's name?

gitjenkins

提问by user2195411

I am trying to get more GIT commit information into a HipChat room.

我正在尝试将更多 GIT 提交信息放入 HipChat 房间。

I see there are a number of GIT variables that can be used in jenkins. I am working in the Execute Shell step of a job.

我看到有许多可以在 jenkins 中使用的 GIT 变量。我正在工作的执行 Shell 步骤中工作。

These work:

这些工作:

echo "${GIT_BRANCH}"

echo "${GIT_URL}"

echo "${GIT_COMMIT}"

These do not:

这些不会:

echo "${GIT_COMMITTER_EMAIL}"

echo "${GIT_COMMITTER_NAME}"

echo "${GIT_AUTHOR_EMAIL}"

echo "${GIT_AUTHOR_NAME}"   

echo "${GIT_USER}"

Question 1: how come the vars above don't work?

问题 1:为什么上面的变量不起作用?

This works:

这有效:

git show --name-only

Question 2: How come I cant do Foo = "git show --name-only" And use Foo else where in the job, ie- send to HipChat?

问题 2:为什么我不能做 Foo = "git show --name-only" 并在工作中的其他地方使用 Foo,即发送到 HipChat?

I see there is a plugin envInject. But this is to write to a file in the workspace doing the execute shell step, then read from that file. This seems to be a bit overkill for what I am trying to do.

我看到有一个插件 envInject。但这是在执行执行 shell 步骤时写入工作区中的文件,然后从该文件中读取。对于我正在尝试做的事情,这似乎有点矫枉过正。

Question 3: is the envInject my only option?

问题 3: envInject 是我唯一的选择吗?

回答by Magnus B?ck

I don't know why some of the variables are available and some aren't but it seems you're not the only one with that problem (see e.g. Git plugin for Jenkins: How do I set env variables GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL?).

我不知道为什么有些变量可用而有些变量不可用,但似乎您不是唯一遇到该问题的变量(参见例如Jenkins 的 Git 插件:如何设置 env 变量 GIT_AUTHOR_EMAIL 和 GIT_COMMITTER_EMAIL?)。

Use e.g. git show -s --pretty=%anto obtain the author name and store it in a variable via command substitution as explained by @MattKneiser:

git show -s --pretty=%an@MattKneiser 所述,使用 eg获取作者姓名并通过命令替换将其存储在变量中:

foo=$(git show -s --pretty=%an)

This variable won't be available in other shell steps in your Jenkins job, but you could save it to a file in your workspace,

此变量在 Jenkins 作业的其他 shell 步骤中不可用,但您可以将其保存到工作区中的文件中,

echo "foo=\"$foo\"" > $WORKSPACE/envvars

and later source that file in the other shell:

然后在另一个 shell 中获取该文件:

. $WORKSPACE/envvars

回答by Matt Kneiser

You need to explicitly execute that git command and put the output of it into a variable. In bash, this is called command substitution.

您需要显式执行该 git 命令并将其输出放入变量中。在 bash 中,这称为命令替换

foo=$(git show --name-only)

回答by Tim

If you are using gerrit trigger plugin, you can get some info like: ${GERRIT_CHANGE_OWNER} or ${GERRIT_CHANGE_OWNER_NAME}, otherwise, try use the way @Matt mentioned, just create a simple shell script in your jenkins before you want to use this variable, and read it, inject it inot a file, later you can use it as a normal parameter.

如果您正在使用 gerrit 触发器插件,您可以获得一些信息,例如:${GERRIT_CHANGE_OWNER} 或 ${GERRIT_CHANGE_OWNER_NAME},否则,请尝试使用@Matt 提到的方式,在您想使用它之前,只需在您的 jenkins 中创建一个简单的 shell 脚本变量,并读取它,将其注入到文件中,稍后您可以将其用作普通参数。

回答by Mohamed Amine Kharrez

Use this function in your jenkinsfile :

在您的 jenkinsfile 中使用此函数:

def author = ""
def changeSet = currentBuild.rawBuild.changeSets               
for (int i = 0; i < changeSet.size(); i++) 
{
   def entries = changeSet[i].items;
   for (int i = 0; i < changeSet.size(); i++) 
            {
                       def entries = changeSet[i].items;
                       def entry = entries[0]
                       author += "${entry.author}"
            } 
 }
 print author;

回答by Jevgenij Kononov

My approch of doing that

我这样做的方法

script{
 def COMMITTER_EMAIL = bat(
    script: "git --no-pager show -s --format='%%ae'",
    returnStdout: true).split('\r\n')[2].trim() 
    echo "COMMITTER_EMAIL: ${COMMITTER_EMAIL}" 
}