简体   繁体   English

如何确定使用jenkins git插件推送到git中的远程仓库的用户?

[英]How to determine the user who pushed to a remote repo in git using jenkins git plugin?

I need to find the git username/email of a person who is pushing changes, consisting of different commits by different users, to a remote repository. 我需要找到一个将更改(由不同用户的不同提交组成)推送到远程存储库的人员的git用户名/电子邮件。

The push triggers a Post-Receive hook via gitblit which triggers a jenkins job, and is using a non user specific SSH authentication. 推送通过gitblit触发了Post-Receive挂钩,该挂钩触发了jenkins作业,并且正在使用非用户特定的SSH身份验证。

Jenkins does not provide an easy way to accomplish that. 詹金斯(Jenkins)没有提供一种简单的方法来实现这一目标。 I had a similar problem and it took me some time to find a suitable solution. 我遇到了类似的问题,花了一些时间才找到合适的解决方案。

I ended up using a combination of the Environmental Variables of Git Plugin and git log . 我最终使用了Git插件的环境变量和git log的组合

Git Plugin offers two environmental variables: Git插件提供了两个环境变量:

GIT_COMMIT - SHA of the current GIT_COMMIT-当前的SHA

GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch) GIT_PREVIOUS_COMMIT-来自同一分支的先前构建提交的SHA(当前在第一个构建分支中的SHA)

which you get every time your git Web Hook triggers the Jenkins job. 每当您的git Web Hook触发Jenkins作业时,您都会获得此信息。

On the other hand, git log provides the means to get the author name and author email using the format command option. 另一方面, git log提供了使用format命令选项获取作者姓名和作者电子邮件的方法。

Therefore, combining those two options you can get the author name and email of each commit between your previous commit which triggered the Jenkins job : GIT_PREVIOUS_COMMIT and the current commit : GIT_COMMIT using the git log command below: 因此,结合使用这两个选项,您可以使用下面的git log命令获取触发Jenkins作业的先前提交: GIT_PREVIOUS_COMMIT和当前提交: GIT_COMMIT之间的每次提交的作者名称和电子邮件:

git log $GIT_PREVIOUS_COMMIT..$GIT_COMMIT --pretty=format:%an/%ae

in an Executed Shell in your Jenkins Job. 在您的詹金斯工作中的执行壳中。 Please make sure that you are in root of your git project. 请确保您在git项目的根目录下。

I had to 我不得不

  1. define a gitblit user and use it for pushing the changes. 定义一个gitblit用户并将其用于推送更改。
  2. change my jenkins job to a parameterized one and define a parameter for the username (myuser). 将我的jenkins作业更改为参数化的作业,并为用户名(myuser)定义一个参数。
  3. add the username in my hook 将用户名添加到我的钩子中

.

def triggerUrl = jenkinsUrl + "/job/" + jenkinsJob + "/buildWithParameters?token=" + jenkinsToken + "&myuser=" + user.getName()
new URL(triggerUrl).getText()

myuser is now an enviroment variable, here as an example in an ant script : myuser现在是一个环境变量,这里以ant脚本为例:

<property environment="env" />
<target name="xx" description="xx">
    <echo message="${env.myuser}" />
</target>

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

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