user2195411
user2195411

Reputation: 237

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.

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?

Upvotes: 13

Views: 29384

Answers (5)

Jevgenij Kononov
Jevgenij Kononov

Reputation: 1237

My approch of doing that

script{
 def COMMITTER_EMAIL = bat(
    script: "git --no-pager show -s --format='%%ae'",
    returnStdout: true).split('\r\n')[2].trim() 
    echo "COMMITTER_EMAIL: ${COMMITTER_EMAIL}" 
}

Upvotes: 1

Mohamed Amine Kharrez
Mohamed Amine Kharrez

Reputation: 144

Use this function in your jenkinsfile :

def author = ""
def changeSet = currentBuild.rawBuild.changeSets               
for (int i = 0; i < changeSet.size(); i++) 
{
   def entries = changeSet[i].items;
   for (int i = 0; i < changeSet.size(); i++) 
            {
                       def entries = changeSet[i].items;
                       def entry = entries[0]
                       author += "${entry.author}"
            } 
 }
 print author;

Upvotes: 0

Magnus B&#228;ck
Magnus B&#228;ck

Reputation: 11581

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

Upvotes: 20

Tim
Tim

Reputation: 2066

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.

Upvotes: 0

Matt Kneiser
Matt Kneiser

Reputation: 2156

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)

Upvotes: 0

Related Questions