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

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

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.

  • understanding git fetch then merge
  • post-receive hook — if incoming file type *.cgi, then set permission to 755
  • Check response of git push from shell script
  • Managing PDFs/binaries with Git or other VCS
  • Jenkins Multibranch Pipelines - Configuring properties in branches?
  • Managing git across mutliple sites
  • 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?

    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?

    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.

    Question 3: is the envInject my only option?

  • Is it rude to commit design documents to github?
  • Migration from StarTeam to distributed source control
  • Merge dev into master and take all code from dev branch
  • Is it a bad idea to put a jar file in the vendor/ directory and add it to the git repo?
  • How to work around updating polymer-cli on Ubuntu on Windows which fails due to .git: Permission denied
  • How to protect a file in the git repository from commits after ther first commit?
  • 3 Solutions collect form web for “Jenkins and Git – how to grab a committer's name?”

    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?).

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

    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,

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

    and later source that file in the other shell:

    . $WORKSPACE/envvars
    

    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.

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

    foo=$(git show --name-only)
    
    Git Baby is a git and github fan, let's start git clone.