开发者

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

开发者 https://www.devze.com 2023-02-21 01:00 出处:网络
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 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.

Unfortun开发者_Python百科ately 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


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


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

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


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.


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."


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.


In the Jenkin job, you can use the command

git describe --always

This will return the first 7 characters of the SHA

Regards


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号