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

别来无恙 提交于 2019-12-18 11:00:37

问题


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


回答1:


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



回答2:


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.




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




回答4:


In the Jenkin job, you can use the command

git describe --always

This will return the first 7 characters of the SHA

Regards




回答5:


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.




回答6:


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




回答7:


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.




回答8:


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



来源:https://stackoverflow.com/questions/5510978/in-hudson-how-do-i-get-the-current-builds-git-commit-sha

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!