36

We are using Hudson with git. We have a build/test server which compiles our project and then a QA server to which we need to deploy.

We want to get the current built git commit sha and use it to pull the appropriate commit for deployment on our QA server.

Unfortunately it seems that the hudon git plugin does not set the git commit sha in the environment variables (as the svn plugin does in SVN_REVISION for example). How do we get around this?

Pointer/ examples will be helpful as I am a hudson noob.

Thanks

SharePoint Newbie
  • 5,762
  • 12
  • 59
  • 102

8 Answers8

55

I added to Post build command:

env

In log I found all environment variables. Some of them are:

BUILD_TAG=jenkins-Datagen-17
JOB_URL=http://jenkins:18080/job/Datagen/
USER=jenkins
GIT_COMMIT=ce9a3c1404e8c91be604088670e93434c4253f03
JENKINS_HOME=/var/lib/jenkins
JOB_NAME=Datagen
BUILD_ID=2011-06-22_15-26-06
GIT_BRANCH=master
EXECUTOR_NUMBER=0
NODE_LABELS=master
LOGNAME=jenkins
NODE_NAME=master
BUILD_NUMBER=17
Andrzej Jozwik
  • 13,093
  • 3
  • 56
  • 65
  • Can you clarify what you mean by this command? Is this an actual post-build option? If so what version of jenkins are you using. – bearrito Jan 31 '13 at 18:52
  • env command just print all environment variables. And it's all.The questions was how to read similar variable to SVN_REVISION. So you see the variable is GIT_COMMIT. – Andrzej Jozwik Feb 01 '13 at 07:37
  • 1
    Clarifying what ajozwik stated above. His solution is not technically a Postbuild action, as this has a specific meaning in jenkins. Instead, the very last build action should be "Inject Environment Variables". In that step the the "Injected Build Properties" should be 'env'. In this case, is the collection of environment variables that Jenkins maintains. – bearrito Jan 31 '13 at 18:59
  • 1
    GIT_BRANCH actually returns `origin/master`, is there a way to get only `master`? – Zennichimaro Dec 20 '13 at 07:26
  • 2
    To see all available environment variables add to the Execute shell field: `printenv` Now console output will show all the available vars. – Pavan Gupta May 13 '15 at 05:39
  • Zennichimaro: i do: branch=$( echo $GIT_BRANCH | sed "s/origin\///") – moralejaSinCuentoNiProverbio Jun 05 '15 at 17:27
7

Jenkins Version: 2.46.2

Git Client: 2.4.5

The following GIT variables are available by running the env command from a shell.

  • GIT_BRANCH
  • GIT_COMMIT
  • GIT_PREVIOUS_COMMIT
  • GIT_PREVIOUS_SUCCESSFUL_COMMIT
  • GIT_URL

So, to inject them back into the Job's environment variables add the word env to the Script Content section...

tl;dr

Job > Configure > Build Environment > Inject environment variables to the build process > Script Content

Example

Community
  • 1
  • 1
Nick Grealy
  • 18,859
  • 9
  • 84
  • 100
5

Probably quite late, but you can do this on jenkins using the API:

https://jenkins-server/job/job-name/lastStableBuild/api/json

This gives a JSON object that you can parse. You can also use the tree option to get a more precise JSON string. Something like this:

https://jenkins-server/job/job-name/lastStableBuild/api/json?tree=actions[lastBuiltRevision[branch[*]]]

Now you can use awk to parse out the SHA1 and git branch.

Somaiah Kumbera
  • 5,876
  • 3
  • 33
  • 41
3

In the Jenkin job, you can use the command

git describe --always

This will return the first 7 characters of the SHA

Regards

3

You could add an extra step to your Hudson Job, publishing the newly created git commit to a second repo on the build/test server.
That second repo can have a post-receive hook pushing automatically said commit to the QA server.

If you don't want that extra layer of indirection, then you need to have, in your extra, step, git commands to query the SHA1 of HEAD: git describe or git rev-parse.
You have other git options in the question "Saving Git SHA1 when building with Hudson similar to the CVS_BRANCH tag for CVS."

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • Can't I just get the successful commit sha using some environment variable, like I do for svn using SVN_REVISION? We are using the standard git plugin for hudson. – SharePoint Newbie Apr 01 '11 at 09:20
  • @SharePoint Newbie: I have updated my answer with other options. – VonC Apr 01 '11 at 10:26
3

This is apparently not possible with the current version of the git plugin. We are instead writing current head to a file and saving it as an artifact. We can later curl it and get the commit id corresponding to a given build.

SharePoint Newbie
  • 5,762
  • 12
  • 59
  • 102
1

There is already a good solution within a executable shell. That solution has the advantage that you can do it as part of the build phase using scripting and not in the post-build phase.That is explained below. See https://stackoverflow.com/a/11837662/5842403

In fact, you can access to the information before the build phase finish by reading/parsing the ../builds/$BUILD_NUMBER/changelog.xml file inside the build folder. This file is created with the SVN/GIT commit triggering and not with the end of the build or post_build phase. That means, you can parse it at the start of the build phase of the same job with a script and insert the data in env variables.

Community
  • 1
  • 1
Joniale
  • 388
  • 2
  • 14
0

Instead of using SVN_REVISION you can use $GIT_COMMIT . so env $GIT_COMMIT will take latest commit hash from Git by using Jenkins.

Dilip
  • 11
  • 2