问题
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.
The push triggers a Post-Receive hook via gitblit which triggers a jenkins job, and is using a non user specific SSH authentication.
回答1:
Jenkins does not provide an easy way to accomplish that. 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 Plugin offers two environmental variables:
GIT_COMMIT - SHA of the current
GIT_PREVIOUS_COMMIT - SHA of the previous built commit from the same branch (the current SHA on first build in branch)
which you get every time your git Web Hook triggers the Jenkins job.
On the other hand, git log
provides the means to get the author name and author email using the format
command option.
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 $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.
回答2:
I had to
- define a gitblit user and use it for pushing the changes.
- change my jenkins job to a parameterized one and define a parameter for the username (myuser).
- 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 :
<property environment="env" />
<target name="xx" description="xx">
<echo message="${env.myuser}" />
</target>
来源:https://stackoverflow.com/questions/41162674/how-to-determine-the-user-who-pushed-to-a-remote-repo-in-git-using-jenkins-git-p