Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Hudson, how do I get the current build's git commit sha?

Tags:

git

hudson

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

like image 777
SharePoint Newbie Avatar asked Apr 01 '11 08:04

SharePoint Newbie


People also ask

How do I get the latest commit of a branch?

If you need to get latest commit on a branch through the REST api, try the "/commits" call with the "until" parameter set to the branch you are interested in, and limit=1 to get the single tip commit from the branch.

What is commit SHA in git?

"SHA" stands for Simple Hashing Algorithm. The checksum is the result of combining all the changes in the commit and feeding them to an algorithm that generates these 40-character strings. A checksum uniquely identifies a commit.

How do I deploy a specific commit in Jenkins?

Pass the commit sha1 to a predefined parameter and in Build-Execute shell run git checkout <commit> before the build process starts. Some extra work is needed to make sure the check-out goes well. Check the box This project is parameterized and then you can add predefined parameters.


2 Answers

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 
like image 67
2 revs Avatar answered Oct 05 '22 07:10

2 revs


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

like image 29
Nick Grealy Avatar answered Oct 05 '22 07:10

Nick Grealy