在 Hudson 中,如何获取当前构建的 git commit sha?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5510978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Hudson, how do I get the current build's git commit sha?
提问by SharePoint Newbie
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.
我们正在使用带有 git 的 Hudson。我们有一个构建/测试服务器来编译我们的项目,然后是一个我们需要部署的 QA 服务器。
We want to get the current built git commit sha and use it to pull the appropriate commit for deployment on our QA server.
我们想要获取当前构建的 git commit sha 并使用它来拉取适当的提交以在我们的 QA 服务器上进行部署。
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?
不幸的是,hudon git 插件似乎没有在环境变量中设置 git commit sha(例如 svn 插件在 SVN_REVISION 中所做的)。我们如何解决这个问题?
Pointer/ examples will be helpful as I am a hudson noob.
指针/示例会有所帮助,因为我是一个哈德逊菜鸟。
Thanks
谢谢
采纳答案by SharePoint Newbie
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.
对于当前版本的 git 插件,这显然是不可能的。相反,我们将当前头部写入文件并将其保存为工件。我们稍后可以卷曲它并获取与给定构建相对应的提交 ID。
回答by Andrzej Jozwik
I added to Post build command:
我添加到 Post build 命令:
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
回答by Somaiah Kumbera
Probably quite late, but you can do this on jenkins using the API:
可能很晚了,但是您可以使用 API 在 jenkins 上执行此操作:
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:
这提供了一个您可以解析的 JSON 对象。您还可以使用 tree 选项来获取更精确的 JSON 字符串。像这样的东西:
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.
现在您可以使用 awk 解析出 SHA1 和 git 分支。
回答by Nick Grealy
Jenkins Version:2.46.2
詹金斯版本:2.46.2
Git Client:2.4.5
Git 客户端:2.4.5
The following GIT
variables are available by running the env
command from a shell
.
下面的GIT
变量可以通过运行env
从命令shell
。
- GIT_BRANCH
- GIT_COMMIT
- GIT_PREVIOUS_COMMIT
- GIT_PREVIOUS_SUCCESSFUL_COMMIT
- GIT_URL
- 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...
因此,要将它们注入到 Job 的环境变量中,请将单词添加env
到该Script Content
部分...
tl;dr
tl;博士
Job > Configure > Build Environment > Inject environment variables to the build process > Script Content
Job > Configure > Build Environment > Inject environment variables to the build process > Script Content
回答by Daniel Labonté
In the Jenkin job, you can use the command
在 Jenkins 作业中,可以使用命令
git describe --always
This will return the first 7 characters of the SHA
这将返回 SHA 的前 7 个字符
Regards
问候
回答by VonC
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.
您可以向您的 Hudson 作业添加一个额外的步骤,将新创建的 git commit 发布到构建/测试服务器上的第二个存储库。
第二个 repo 可以有一个 post-receive hook 自动推送到 QA 服务器的提交。
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."
如果您不想要额外的间接层,那么您需要在额外的步骤中使用 git 命令来查询 HEAD:git describe
或git rev-parse
.
“你必须在这个问题其他的git选项哈德森类似于CVS的CVS_BRANCH标签时,建设节约混帐SHA1。”
回答by Joniale
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
在可执行 shell 中已经有一个很好的解决方案。该解决方案的优点是您可以在使用脚本的构建阶段而不是在构建后阶段执行此操作。这将在下面解释。见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.
实际上,您可以在构建阶段完成之前通过读取/解析构建文件夹中的 ../builds/$BUILD_NUMBER/changelog.xml 文件来访问信息。该文件是在 SVN/GIT 提交触发时创建的,而不是在构建或 post_build 阶段结束时创建的。这意味着,您可以在同一作业的构建阶段开始时使用脚本对其进行解析,并将数据插入到 env 变量中。
回答by Dilip
Instead of using SVN_REVISION you can use $GIT_COMMIT . so env $GIT_COMMIT will take latest commit hash from Git by using Jenkins.
您可以使用 $GIT_COMMIT 而不是使用 SVN_REVISION 。所以 env $GIT_COMMIT 将使用 Jenkins 从 Git 获取最新的提交哈希。