Jenkins: Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref

Introduction

In this post I will show you how to resolve error on Jenkins: detecting the current branch failed: fatal: ref HEAD is not a symbolic ref. Note that I am using Jenkins version 1.651. If you have different version of Jenkins then this solution may now work.

Error Trace in Jenkins

I’ve moved some of my projects from SVN to GIT. In Jenkins, builds are working fine but when I try to release, I get the following stack trace of error in Jenkins workspace’s console:

Waiting for Jenkins to finish collecting data
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.0:prepare (default-cli) on project hello_world_app: An error is occurred in the checkin process: Exception while executing SCM command. Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Scenario

I think this is a common issue that occurs when you try to release a project in Jenkins though the project itself gets successfully built when you run the Jenkins job using link Build Now option or if you have configured parameterized build then you will see option Build with Parameters.

For example, you have a project in GIT repository and there you have branches like develop and master. When you want to release the project for deploying on Dev, SIT and UAT, you would like to build and release from develop branch, and when you want the project for production release then you would like to build and release from master branch. Therefore, you want to click on Perform maven release option in Jenkins to release an artifact for the project either from develop or master branch and you may face the error similar to the following message:

Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref

Error in Jenkins during job release

So I was also trying to release the project in Jenkins from develop branch and spontaneously I was getting the above error but I did not find any satisfactory solution in Internet and thereafter I was trying to resolve the issue in different approaches and finally after few hours by doing some trial/error I got a solution that helped me to release the project from develop branch successfully.

The whole stack trace could be similar to the below:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.4.1:prepare (default-cli) on project test-release-plugin: 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:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	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:100)
	at hudson.maven.Maven3Builder.call(Maven3Builder.java:66)
	at hudson.remoting.UserRequest.perform(UserRequest.java:118)
	at hudson.remoting.UserRequest.perform(UserRequest.java:48)
	at hudson.remoting.Request$2.run(Request.java:326)
	at hudson.remoting.InterceptingExecutorService$1.call(InterceptingExecutorService.java:72)
	at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
	at java.util.concurrent.FutureTask.run(FutureTask.java:166)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:724)
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:281)
	at org.apache.maven.plugins.release.PrepareReleaseMojo.execute(PrepareReleaseMojo.java:232)
	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:277)
	... 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

I think, by default, Jenkins does the checkout without naming the branch which produces this error. By default, Jenkins GIT plugin builds on a detached HEAD, not on a branch. You can use the option called Checkout to specific local branch (optional) under Additional Behaviors option to let Jenkins create a local branch and do your release work.

Solution

  1. In Jenkins job, click on Configuration link, then search for Additional Behaviors. Then click on Add and select Checkout to specific local branch and put develop in the input box (if you are releasing from master branch then put master, which is mainly for production release). Then click on Save at the bottom.
  2. Please do not use your username/password or credentials, letting SSH take over while you are releasing the project in Jenkins using the link Perform maven release.

Please note that my Jenkins version was 1.651.

Thanks for reading.

3 thoughts on “Jenkins: Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref

Leave a Reply

Your email address will not be published. Required fields are marked *