11

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?

user2195411
  • 215
  • 2
  • 5
  • 20
  • Not sure what you're asking here. `git show --name-only` doesn't give you the name of the author or committer but the names of the files modified by the head commit. – Magnus Bäck Feb 04 '15 at 06:54
  • @MagnusBäck idk if our settings differ but - "git show --name-only" gives me the Author/Committer; I just tried it from terminal. Either way, I am considering "git show -s --pretty=%an" to get the Author/Committer now. This works great. In execute shell it works "foo=$(git show --name-only)" However I can't send this variable {$foo} to hipchat. Any ideas? – user2195411 Feb 04 '15 at 16:30
  • `--name-only` does show the author name, but not _only_ the author name. The purpose of that option is to show the names of the changes files. I have no idea how you're trying to send this to HipChat so I can't have any input. – Magnus Bäck Feb 04 '15 at 20:16
  • @MagnusBäck There is a hipchat post build plugin. You can send in variables like `${JOB_NAME}` . However, custom defined variables like `test=$(git show -s --pretty=%an)` won't send into hip chat – user2195411 Feb 04 '15 at 21:59
  • I can't comment since I don't understand _exactly_ what "send in variables" means. Please get concrete. The devil's in the details. – Magnus Bäck Feb 05 '15 at 04:56
  • @MagnusBäck Thank you for the response. In a job, I have a `Execute shell` step. Inside this step is have `test=$(git show -s --pretty=%an)` and `echo ${foo}` when I build the job, the `console` shows the git data ($foo) I need ie- `committer`. We can agree `${foo}` is a define variable now. In the same job, I have a `Post-build actions` step. This step is `HipChat Notifier`. You plug in the Room, Token, and Message. For message, I can 'send in' native variables like `${JOB_NAME}` to HC. I can send in GIT vars like `${GIT_URL}` to HC. But when I try `${foo}` it will not appear in HC. – user2195411 Feb 05 '15 at 14:37
  • That's expected. $GIT_URL and the other ones you mentioned come from the Jenkins job and are inherited by the post build action shell. The same isn't true for variables that you set in a previous build step. It was a while since I did any Jenkins work, but I think the best way out is to write the information to a file in the workspace and read that file in the next post build step. – Magnus Bäck Feb 05 '15 at 15:43
  • Okay, thanks. That is what I figured. Back to my original first post above, it stinks that the 3 GIT variables above work but the five GIT variables do not. Anyway, care to provide some help in regards to writing to a file and reading it in the next post build step? – user2195411 Feb 05 '15 at 16:11

5 Answers5

18

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
Community
  • 1
  • 1
Magnus Bäck
  • 11,381
  • 3
  • 47
  • 59
0

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)
Matt Kneiser
  • 1,982
  • 18
  • 23
  • I did this. For hipchat message, I have ${JOB_NAME} ${foo} . However, only job name appears in hipcat. echo foo in job's console works though. Any ideas? – user2195411 Feb 04 '15 at 16:21
0

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.

Tim
  • 2,006
  • 2
  • 18
  • 25
0

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;
0

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}" 
}
Jevgenij Kononov
  • 1,210
  • 16
  • 11