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.

The push triggers a Post-Receive hook via gitblit which triggers a jenkins job, and is using a non user specific SSH authentication.

  • Jenkins GIT include regions never builds for
  • Git create branch from range of previous commits?
  • How to mirror a github repository, while maintaining my small changes?
  • Ability to set --no-ff option when merge pull request on TFS
  • How to revert “git rm” of an untracked file
  • git group development server setup
  • Selective access to Git repository based on SSH
  • Can SourceTree show diff side-by-side
  • git repository on github
  • GIT send-mail failing to send mail
  • How should you keep a shared develop branch up to date with upstream changes on git?
  • Sparse Git checkout of files into parent directory
  • 2 Solutions collect form web for “How to determine the user who pushed to a remote repo in git using jenkins git plugin?”

    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.

    I had to

    1. define a gitblit user and use it for pushing the changes.
    2. change my jenkins job to a parameterized one and define a parameter for the username (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 :

    <property environment="env" />
    <target name="xx" description="xx">
        <echo message="${env.myuser}" />
    </target>
    
    Git Baby is a git and github fan, let's start git clone.