活动 Git 分支在 hudson CI 上是“(无分支)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3858563/
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
Active Git branch is "(no branch)" on hudson CI
提问by milkplus
My Ant build.xml script starts with
我的 Ant build.xml 脚本以
<property environment="env"/>
<echo>GIT_BRANCH = ${env.GIT_BRANCH}</echo>
<echo>PWD = ${env.PWD}</echo>
Hudson CI is setup to build when any branch changes. Console output is...
Hudson CI 设置为在任何分支更改时构建。控制台输出是...
Commencing build of Revision 90906a63929e9074035eb5b10c71ee055ad3e13c (origin/DPM-48)
GitAPI created
Checking out Revision 90906a63929e9074035eb5b10c71ee055ad3e13c (origin/DPM-48)
[workspace] $ git.exe checkout -f 90906a63929e9074035eb5b10c71ee055ad3e13c
[workspace] $ cmd.exe /C '"C:\Program Files\WinAnt\bin\ant.bat" -file build.xml ...'
[echo] GIT_BRANCH = ${env.GIT_BRANCH}
[echo] PWD = /cygdrive/d/.hudson
From the console output, Hudson knows it is building topic branch DPM-48 but environment variable GIT_BRANCH is not set and 'git branch' returns that git is at a 'detached HEAD' state
从控制台输出中,Hudson 知道它正在构建主题分支 DPM-48,但未设置环境变量 GIT_BRANCH 并且“git branch”返回 git 处于“分离的 HEAD”状态
* (no branch)
master
DPM-48
What I want to know is which branch I'm building on hudson. There must be a way to do this.
我想知道的是我在 hudson 上建立哪个分支。必须有办法做到这一点。
回答by milkplus
RESOLVED
解决
After seeing the comment by abayer in Hudson bug 6856, I took the following steps:
在看到 abayer 在Hudson bug 6856 中的评论后,我采取了以下步骤:
- Using the Hudson Plugin Manager, I updated my Hudson Git Plugin version 1.1) to get abayer's fix.
- Using the Hudson Job Configuration, I clicked the "Advanced" button under "Source Code Management" -> "Branches to Build". I then changed the "Local branch to use" to something to use for a name. It doesn't matter what.
- Then I clicked the "Save" button and started a build with "Build Now"
- 使用 Hudson 插件管理器,我更新了我的 Hudson Git 插件版本 1.1) 以获得 abayer 的修复。
- 使用 Hudson 作业配置,我单击“源代码管理”->“要构建的分支”下的“高级”按钮。然后我将“要使用的本地分支”更改为用于名称的内容。什么都无所谓。
- 然后我单击“保存”按钮并使用“立即构建”开始构建
The build log shows
构建日志显示
[workspace] $ git.exe checkout -b ChangeTester d6caef27759495c5714923c1ddf19551a70d6083
Instead of
代替
[workspace] $ git.exe checkout -f d6caef27759495c5714923c1ddf19551a70d6083
Which means that I am not in a 'detached head' state and can therefore do commits, create tags, and push.
这意味着我不处于“分离头”状态,因此可以进行提交、创建标签和推送。
I can use 'git rev-parse HEAD' to get the commit hash and find that in the output of 'git show-ref' to find the real name of the branch if I need it.
如果需要,我可以使用“git rev-parse HEAD”获取提交哈希并在“git show-ref”的输出中找到它以查找分支的真实名称。
I'm now able to push successful build tags to my git repo.
我现在可以将成功的构建标签推送到我的 git 存储库。
回答by VonC
Note: the OP milkplus's comment refers to recent Hudson bug 6856(June 2010), which mentions:
注意:OP Milkplus的评论指的是最近的Hudson bug 6856(2010 年 6 月),其中提到:
Git builds with detached head no matter what
无论如何,Git 都会用分离的头构建
While it is unclear if that particular issue will be solved (answers suggest it might actually "work as designed"!), it also refers to this version of hudson Git Plugin, allowing to checkout a local branch.
虽然不清楚这个特定问题是否会得到解决(答案表明它实际上可能“按设计工作”!),但它也指的是这个版本的 hudson Git Plugin,允许签出本地分支。
You are in a DETACHED HEAD
because, as the git plugin is working right now, it did checkout directly a commit SHA1, and not a branch HEAD
.
您在 a 中是DETACHED HEAD
因为,由于 git 插件现在正在运行,它确实直接检出提交 SHA1,而不是 branch HEAD
。
The state you are in while your
HEAD
is detached is not recorded by any branch (which is natural --- you are not on any branch). What this means is that you can discard your temporary commits and merges by switching back to an existing branch.
您在
HEAD
分离时所处的状态不会被任何分支记录(这是自然的——您不在任何分支上)。这意味着您可以通过切换回现有分支来放弃临时提交和合并。
Your building script could first try to find what branch the relevant commit is coming from.
您的构建脚本可以首先尝试查找相关提交来自哪个分支。
As the OP milkplusrealized by looking at the source code of the Hudson Git Plugin:
正如 OP Milkplus通过查看Hudson Git Plugin的源代码实现的:
public void buildEnvVars(AbstractBuild build, java.util.Map<String, String> env) {
super.buildEnvVars(build, env);
String branch = getSingleBranch(build);
if(branch != null){
env.put(GIT_BRANCH, branch);
}
}
An environment variable GIT_BRANCH
is set, but it doesn't appear to have any value in the xml build script:
GIT_BRANCH
设置了环境变量,但它在 xml 构建脚本中似乎没有任何值:
<property environment="env"/>
<echo>GIT_BRANCH = ${env.GIT_BRANCH}</echo>
If that is the case, it may be because of issue 7554:
如果是这种情况,可能是因为问题 7554:
GIT_BRANCH
not set when multiple branches are selected for buildWhen trying to identify what branch the current build are on, I found that the
GIT_BRANCH
environment variable is not set when more then a single branch is selected to be built.This isn't really a bug so much as a feature request, I think - the
GIT_BRANCH
env var is only set if there's a single branch, so as such, it's not relevant if/when there are multiple branches. I'm not sure how we'd format an env var for multiple branches in this context.I thought that
GIT_BRANCH
shall be set to the branch that is currently building. Like if the build is on master it will contain the master.That would help to for example push to another remote that branch that was built during this build. Or Triggering another Build with the correct branch set to be built.
GIT_BRANCH
选择多个分支进行构建时未设置When trying to identify what branch the current build are on, I found that the
GIT_BRANCH
environment variable is not set when more then a single branch is selected to be built.我认为这并不是一个真正的错误,而是一个功能请求 -
GIT_BRANCH
只有在有一个分支时才会设置 env var,因此,如果/当有多个分支时,它不相关。我不确定在这种情况下我们将如何为多个分支设置 env var 的格式。我认为
GIT_BRANCH
应该设置为当前正在构建的分支。就像构建在 master 上一样,它将包含 master。这将有助于例如推送到在此构建期间构建的另一个远程分支。或者使用要构建的正确分支集触发另一个构建。
Kind of mirror the NPE described here
For some reason Git plugin started to pass null value for
GIT_BRANCH
environment variable.
This caused Maven plugin to fail inSystem.getProperties().putAll(systemProps)
call.The solution was to use "
master
" as default Git branch instead of "**
" or empty String.
由于某种原因,Git 插件开始为
GIT_BRANCH
环境变量传递空值。
这导致 Maven 插件System.getProperties().putAll(systemProps)
调用失败。解决方案是使用“
master
”作为默认的 Git 分支,而不是“**
”或空字符串。
回答by Tobias Hochgürtel
Thank you, very much for sharing this solution!
非常感谢您分享此解决方案!
I had some trouble to find this optional dialog to set the local branch - and to show others that this is also still the current approach also in year 2015 I would to attach some screenshot of the current gui dialogs of jenkins.
我在找到这个可选对话框来设置本地分支时遇到了一些麻烦 - 并向其他人展示这仍然是 2015 年的当前方法,我将附上 jenkins 当前 gui 对话框的一些屏幕截图。