Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

jenkins

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.

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?

like image 664
user2195411 Avatar asked Feb 04 '15 00:02

user2195411


1 Answers

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
like image 59
Magnus Bäck Avatar answered Oct 06 '22 15:10

Magnus Bäck