others-how to solve Cannot get the branch information from the git repository error when using buildnumber maven plugin to get git branch name?

1. Purpose

In this post, I will show you how to solve cannot get the branch information from the git repository error when using buildNumber maven plugin to get git branch name.

When trying to build a maven package using buildnumber-maven-plugin, which will try to get the current git branch name , sometimes, especially in Jenkins CI pipeline, you got this error:

[INFO] **--- buildnumber-maven-plugin:3.1.0:create (default) @ bswen-testproject-parent ---**
[INFO] ShortRevision tag detected. The value is '8'.
[INFO] Executing: /bin/sh -c cd '/MyTools/jenkins_data/workspace/testpackage1' && 'git' 'rev-parse' '--verify' '--short=8' 'HEAD'
[INFO] Working directory: /MyTools/jenkins_data/workspace/testpackage1
[INFO] Storing buildNumber: dfefab49 at timestamp: 1691993785756
[WARNING] Cannot get the branch information from the git repository: 
Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref

[INFO] ShortRevision tag detected. The value is '8'.
[INFO] Executing: /bin/sh -c cd '/MyTools/jenkins_data/workspace/testpackage1' && 'git' 'rev-parse' '--verify' '--short=8' 'HEAD'
[INFO] Working directory: /MyTools/jenkins_data/workspace/testpackage1
[INFO] Storing scmBranch: UNKNOWN
[INFO]

The key problem is:

[INFO] Storing scmBranch: UNKNOWN

But the same code run on local laptop is fine:

[INFO] --- buildnumber-maven-plugin:3.1.0:create (default) @ module1 ---
[INFO] ShortRevision tag detected. The value is '8'.
[INFO] Executing: /bin/sh -c cd '/Users/bswen/JavaProjects/bswen-testdabao-project-root/bswen-testdabao-project/module1' && 'git' 'rev-parse' '--verify' '--short=8' 'HEAD'
[INFO] Working directory: /Users/bswen/JavaProjects/bswen-testdabao-project-root/bswen-testdabao-project/module1
[INFO] Storing buildNumber: dfefab49 at timestamp: 1691994328722
[INFO] Storing scmBranch: master
[INFO] 

You can see that the plugin got the branch name master, why it failed on jenkins?

The plugin configuration:

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>buildnumber-maven-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <revisionOnScmFailure>true</revisionOnScmFailure>
                        <shortRevisionLength>8</shortRevisionLength>
                        <doCheck>false</doCheck>
                        <doUpdate>false</doUpdate>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>create</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>



2. Solution

The reason:

When the Jenkins Git plugin clones a repo, it checks out a specific commit, rather than HEAD of the repo. This puts the repo in a “detached” state, so if you want to perform further git operations on the repo you need to attach to a branch with a checkout command in a shell step.

The solution:

Just configure your jenkins project, in the Source Code Management, Choose Additional Behaviours, choose Check out to specific local branch, then input the branch name you want to build.

Run the project again, you will get this:

[INFO] **--- buildnumber-maven-plugin:3.1.0:create (default) @ bswen-testproject-parent ---**
[INFO] ShortRevision tag detected. The value is '8'.
[INFO] Executing: /bin/sh -c cd '/MyTools/jenkins_data/workspace/testpackage1' && 'git' 'rev-parse' '--verify' '--short=8' 'HEAD'
[INFO] Working directory: /MyTools/jenkins_data/workspace/testpackage1
[INFO] Storing buildNumber: dfefab49 at timestamp: 1691994687755
[INFO] Storing scmBranch: master
[INFO]

You can see that the plugin successfully got the branch name:

[INFO] Storing scmBranch: master

It works!



3. Summary

In this post, I demonstrated how to solve the problem when using buildNumber plugin to get current code’s branch name in Jenkins, the key point to solve this problem is to make sure that your Jenkins code contains the branch info. That’s it, thanks for your reading.