Jenkins Git 插件分离 HEAD

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11511390/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:07:52  来源:igfitidea点击:

Jenkins Git plugin detached HEAD

gitjenkins

提问by Michel Werren

I am new to Git and also to Jenkins. My problem is that I can't get the Jenkins Maven release plugin to work.

我是 Git 和 Jenkins 的新手。我的问题是我无法让 Jenkins Maven 发布插件工作。

When I build a common Maven build with Jenkins, it works well, but when I try to perform a release with the Maven release plugin, I get the following stack trace:

当我使用 Jenkins 构建通用 Maven 构建时,它运行良好,但是当我尝试使用 Maven 发布插件执行发布时,我得到以下堆栈跟踪:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.3.2:prepare (default-cli) on project parent: An error is occurred in the checkin process: Exception while executing SCM command.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.jvnet.hudson.maven3.launcher.Maven3Launcher.main(Maven3Launcher.java:79)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchStandard(Launcher.java:329)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:239)
    at org.jvnet.hudson.maven3.agent.Maven3Main.launch(Maven3Main.java:158)
    at hudson.maven.Maven3Builder.call(Maven3Builder.java:98)
    at hudson.maven.Maven3Builder.call(Maven3Builder.java:64)
    at hudson.remoting.UserRequest.perform(UserRequest.java:118)
    at hudson.remoting.UserRequest.perform(UserRequest.java:48)
    at hudson.remoting.Request.run(Request.java:326)
    at hudson.remoting.InterceptingExecutorService.call(InterceptingExecutorService.java:72)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: org.apache.maven.plugin.MojoExecutionException: An error is occurred in the checkin process: Exception while executing SCM command.
    at org.apache.maven.plugins.release.PrepareReleaseMojo.prepareRelease(PrepareReleaseMojo.java:295)
    at org.apache.maven.plugins.release.PrepareReleaseMojo.execute(PrepareReleaseMojo.java:247)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
    ... 27 more
Caused by: org.apache.maven.shared.release.ReleaseExecutionException: An error is occurred in the checkin process: Exception while executing SCM command.
    at org.apache.maven.shared.release.phase.AbstractScmCommitPhase.checkin(AbstractScmCommitPhase.java:160)
    at org.apache.maven.shared.release.phase.AbstractScmCommitPhase.performCheckins(AbstractScmCommitPhase.java:145)
    at org.apache.maven.shared.release.phase.ScmCommitPreparationPhase.runLogic(ScmCommitPreparationPhase.java:76)
    at org.apache.maven.shared.release.phase.AbstractScmCommitPhase.execute(AbstractScmCommitPhase.java:78)
    at org.apache.maven.shared.release.DefaultReleaseManager.prepare(DefaultReleaseManager.java:234)
    at org.apache.maven.shared.release.DefaultReleaseManager.prepare(DefaultReleaseManager.java:169)
    at org.apache.maven.shared.release.DefaultReleaseManager.prepare(DefaultReleaseManager.java:146)
    at org.apache.maven.shared.release.DefaultReleaseManager.prepare(DefaultReleaseManager.java:107)
    at org.apache.maven.plugins.release.PrepareReleaseMojo.prepareRelease(PrepareReleaseMojo.java:291)
    ... 30 more
Caused by: org.apache.maven.scm.ScmException: Exception while executing SCM command.
    at org.apache.maven.scm.command.AbstractCommand.execute(AbstractCommand.java:63)
    at org.apache.maven.scm.provider.git.AbstractGitScmProvider.executeCommand(AbstractGitScmProvider.java:291)
    at org.apache.maven.scm.provider.git.AbstractGitScmProvider.checkin(AbstractGitScmProvider.java:217)
    at org.apache.maven.scm.provider.AbstractScmProvider.checkIn(AbstractScmProvider.java:410)
    at org.apache.maven.shared.release.phase.AbstractScmCommitPhase.checkin(AbstractScmCommitPhase.java:156)
    ... 38 more
Caused by: org.apache.maven.scm.ScmException: Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref
    at org.apache.maven.scm.provider.git.gitexe.command.branch.GitBranchCommand.getCurrentBranch(GitBranchCommand.java:147)
    at org.apache.maven.scm.provider.git.gitexe.command.checkin.GitCheckInCommand.createPushCommandLine(GitCheckInCommand.java:192)
    at org.apache.maven.scm.provider.git.gitexe.command.checkin.GitCheckInCommand.executeCheckInCommand(GitCheckInCommand.java:132)
    at org.apache.maven.scm.command.checkin.AbstractCheckInCommand.executeCommand(AbstractCheckInCommand.java:54)
    at org.apache.maven.scm.command.AbstractCommand.execute(AbstractCommand.java:59)
    ... 42 more
channel stopped
Finished: FAILURE

The failing command and error message are:

失败的命令和错误消息是:

[INFO] Executing: /bin/sh -c cd
/var/lib/jenkins/workspace/test_maven/parent && git symbolic-ref HEAD
[INFO]  Working directory:
/var/lib/jenkins/workspace/test_maven/parent  mojoFailed
org.apache.maven.plugins:maven-release-plugin:2.3.2(default-cli)
projectFailed ch.apkern.achilles:parent:1.0-SNAPSHOT  sessionEnded

I have figured out that the Jenkins Git plugin creates a detached HEAD ref "(no branch)" which causes the problem, I think. But I have absolutely no idea why this ref is created or how I can solve this problem.

我已经发现 Jenkins Git 插件创建了一个分离的 HEAD 引用“(无分支)”,我认为这会导致问题。但我完全不知道为什么创建这个 ref 或者我如何解决这个问题。

I would be grateful for any help.

我将不胜感激任何帮助。

回答by Matthias Braun

The Checkout/merge to local branch (optional)field is gone in the current (2.2.1) version of the Git plugin.

结帐/合并到本地分支(可选)字段走在了Git的插件的电流(2.2.1)版本。

It has moved to Additional BehavioursCheck out to specific local branch:

它已移至附加行为查看特定的本地分支

Jenkins screenshot of setting option "Check out to specific local branch"

Jenkins 设置选项“签出特定本地分支”的屏幕截图

Setting that value to mastergot me a checked out branch instead of a detached head.

将该值设置为master 为我提供了一个已检出的分支,而不是一个独立的头。

回答by Juraj Misur

None of the other answer's Jenkins configurations worked for me without having to create manual steps. What indeed works is plain simple:

没有其他答案的 Jenkins 配置对我有用,而无需创建手动步骤。真正有效的方法很简单:

Repository URL: <repo>
Branches to build: master
Checkout/merge to local branch (optional): master

回答by Constantine

UPDATE (November 2015): Please, note that this solution was given for a specific version of Git plugin (1.1.26). In later versions, the plugin was updated to make configuration easier.

更新(2015 年 11 月):请注意,此解决方案是针对特定版本的 Git 插件 (1.1.26) 提供的。在以后的版本中,更新了插件以使配置更容易。

For Jenkins Git plugin version 1.1.26 try this:

对于 Jenkins Git 插件版本 1.1.26 试试这个:

Go to Job Configuration. Scroll down to Git section and click "Advanced..." button under "Repositories". Then set:

转到作业配置。向下滚动到 Git 部分,然后单击“存储库”下的“高级...”按钮。然后设置:

Name: origin
Refspec: +refs/heads/branch-0.1:refs/remotes/origin/localbranchname

Then click another "Advanced..." button and set:

然后单击另一个“高级...”按钮并设置:

Checkout/merge to local branch (optional): localbranchname

You can name the local branch as you like, but the destination in Refspec must match the local branch name in that optional field (in this case "localbranchname"). This will attach HEAD to localbranchname like this:

您可以随意命名本地分支,但 Refspec 中的目标必须与该可选字段中的本地分支名称匹配(在本例中为“localbranchname”)。这会将 HEAD 附加到 localbranchname ,如下所示:

HEAD -> refs/heads/localbranchname -> 7a698457751bdc043cfda631b81e3812c5361790

Maven Release should pass now in Jenkins.

Maven Release 现在应该在 Jenkins 中通过。

By the way, this works for me with Jenkins 1.492 and Jenkins Git plugin version 1.1.26.

顺便说一下,这对我来说适用于 Jenkins 1.492 和 Jenkins Git 插件版本 1.1.26。

回答by Eugene Sajine

In Git, when you have a branch checked out, like master or dev or any other local branch, your HEAD (file in .git folder) will contain a reference to the corresponding branch. Therefore it is "attached".

在 Git 中,当您检出分支时,例如 master 或 dev 或任何其他本地分支,您的 HEAD(.git 文件夹中的文件)将包含对相应分支的引用。因此它是“附加”的。

When you perform some operations like rebase, merges or when you're checking out a particular commit, i.e. anytime you see "no branch", your HEAD doesn't have a reference to any local branch but points directly to the commit, i.e. it has the actual SHA-1 inside. That means it is detached - detached from any branch. There is no new reference "no branch" created.

当你执行一些操作,比如变基、合并或者当你检查一个特定的提交时,即任何时候你看到“没有分支”,你的 HEAD 没有对任何本地分支的引用,而是直接指向提交,即它里面有实际的SHA-1。这意味着它是分离的 - 与任何分支分离。没有创建新的参考“无分支”。

The command git symbolic-ref HEADchecks if the HEAD content is a reference or a SHA-1 and prints it out.

该命令git symbolic-ref HEAD检查 HEAD 内容是引用还是 SHA-1 并将其打印出来。

You can see that by doing:

您可以通过执行以下操作来看到:

git checkout master
git symbolic-ref HEAD
git checkout HEAD~2 # going two commits back
git symbolic-ref HEAD
git checkout master # coming back

Now, most of the time the Git plugin in Jenkins works with the code in detached HEAD state. I'm not sure how the Maven release plugin works, but I'm 99% sure that it requires you to release from a specific branch. In order to fix that, I would recommend to specify something like the following as a prebuild step or shell command:

现在,大部分时间 Jenkins 中的 Git 插件与处于分离 HEAD 状态的代码一起工作。我不确定 Maven 发布插件是如何工作的,但我 99% 确定它需要您从特定分支发布。为了解决这个问题,我建议将以下内容指定为预构建步骤或 shell 命令:

git checkout master; git pull origin master

That will solve the problem, I hope ;)

这将解决问题,我希望;)

回答by R.S

(SOLVED)

(解决了)

Hi, I had the same issue when trying to do a parametrized release build from a branch with maven-release-plugin:2.5.3 and maven-scm-provider-jgit:1.9.5.

嗨,我在尝试从带有 maven-release-plugin:2.5.3 和 maven-scm-provider-jgit:1.9.5 的分支进行参数化发布构建时遇到了同样的问题。

I wanted to be able to select the branch for the "parametrized release build" this did not work and when I chose "Checkout/merge to local branch (optional)" it worked but ended up with a branch "origin/origin/mybranch" in remote (repeated origin).

我希望能够为“参数化发布版本”选择分支,这不起作用,当我选择“结帐/合并到本地分支(可选)”时,它起作用了,但最终得到了一个分支“origin/origin/mybranch”在远程(重复来源)。

So:

所以:

  • Add "Git Parameter" to "This project is parameterized"
    Name: branch
    ParameterType: Branch
    Click Advanced:
    Branch Filter:origin/(.*)
    (this was the trick!)

  • Git Repository:
    Branches to build: refs/remotes/origin/${branch}

  • Additional Behaviors: -> Check out to specific local branch
    Branch Name: ${branch}

  • 将“Git Parameter”添加到“This project is parameterized”
    Name: branch
    ParameterType: Branch
    Click Advanced:
    Branch Filter:origin/(.*)
    (这就是诀窍!)

  • Git 存储库:
    要构建的分支:refs/remotes/origin/${branch}

  • 附加行为:-> 签出到特定的本地分支
    分支名称:${branch}

Have fun :-)

玩得开心 :-)

回答by Christian Semrau

I want to build several branches, and check out each branch under its name. I am using GIT plugin 2.4.0.

我想建立几个分支,并检查其名称下的每个分支。我正在使用 GIT 插件 2.4.0。

The answer by Matthias Braungets you a named branch, but it is not named after the remote branch.

Matthias Braun答案为您提供了一个命名分支,但它不是以远程分支命名的。

Instead of setting the local branch master, set the local branch $GIT_BRANCH.

不是设置本地分支master,而是设置本地分支$GIT_BRANCH

I found that solution in https://issues.jenkins-ci.org/browse/JENKINS-6856

我在https://issues.jenkins-ci.org/browse/JENKINS-6856 中找到了该解决方案

回答by kubanczyk

Add to maven command line for the release prepare: -DpushChanges=false -DlocalCheckout=true

添加到发布准备的 Maven 命令行: -DpushChanges=false -DlocalCheckout=true

This means maven would use what jenkins got inside working directory .git, and neither clone the remote or push to remote.

这意味着 maven 将使用 jenkins 在工作目录中获得的内容.git,而不是克隆远程或推送到远程。

I recommend to configure fully qualified refs/remotes/origin/developas your Git "Branch to build". This way it seems more understandable to me.

我建议将完全限定配置refs/remotes/origin/develop为您的 Git“要构建的分支”。这样对我来说似乎更容易理解。

In such case your $GIT_BRANCH would get magically set by Jenkins to origin/develop

在这种情况下,您的 $GIT_BRANCH 会被 Jenkins 神奇地设置为 origin/develop

Then, instead of using overly complicated (but portable) GitPublisher, just add a post-build step "Execute Shell":

然后,不要使用过于复杂(但可移植)的 GitPublisher,只需添加一个构建后步骤“执行 Shell”:

echo Remote branch is $GIT_BRANCH, replacing origin with refs/heads.
git push --follow-tags "$GIT_URL" "+HEAD:${GIT_BRANCH/#origin\//refs/heads/}"

This pushes whatever maven changed, like pom.xml and tags.

这会推送任何 maven 更改的内容,例如 pom.xml 和标签。

回答by Pawel

Got same problem. @Eugene solution worked only once. There was error in second try - "can't delete HEAD from repository" or something like this.

遇到同样的问题。@Eugene 解决方案只工作一次。第二次尝试时出现错误 - “无法从存储库中删除 HEAD”或类似内容。

I've founded this (source):

我已经建立了这个(来源):

And m2 extra steps (pre-build)

git checkout master || git checkout -b master

git reset --hard origin/master

和 m2 额外步骤(预构建)

git checkout master || git checkout -b master

git reset --hard origin/master

And now i think its ok.

现在我觉得还可以。

回答by antoine

I had the same problem. I tried Constantine's solution, that worked perfectly but the tag and the commits were pushed on the "localbranchname" remote repository.

我有同样的问题。我尝试了康斯坦丁的解决方案,效果很好,但标签和提交被推送到“localbranchname”远程存储库上。

So I did the same but manually : first add a pre-steps shell script :

所以我做了同样的但手动的:首先添加一个预先步骤的shell脚本:

git branch -f localJenkins
git checkout localJenkins

Then a post-steps shell script :

然后是一个后期的 shell 脚本:

git checkout master
git rebase localJenkins
git branch -D localJenkins
git push origin master
git push --tag

This works ! This way, you don't have jenkins remote branch, commits and tag will be on the master (or other) branch.

这有效!这样,您就没有 jenkins 远程分支,提交和标记将位于主(或其他)分支上。

hope this helps !

希望这可以帮助 !