-
Bug
-
Resolution: Not A Defect
-
Minor
-
None
-
LTS 2.46.3
I have a project that now needs to know what branch it's currently operating under when it builds. The nature of our build process prevents us from using multibranch pipelines or similar freestyle jobs. I have discovered the related issue JENKINS-43104 which describes the behaviour I'm experiencing. BRANCH_NAME is not set at all for non-multibranch jobs.
I have a sample Maven project that tries to commit changes from the build back to the repository but fails due to missing branch information. This can be found at https://github.com/kenrachynski/pipeline-branch-error.git
Running the maven build in a freestyle job exhibits the exact same behaviour. The error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-scm-plugin:1.9.5:checkin (04-commit) on project pipeline-branch-error: Cannot run checkin command : Exception while executing SCM command. Detecting the current branch failed: fatal: ref HEAD is not a symbolic ref -> [Help 1]
If you're using the git plugin, then there is an "Additional Behaviour" called "Check out to specific local branch". If that is configured with an empty string, or with "**", then the plugin will create a local branch based on the name of the remote branch.
The default is to checkout a "detached HEAD", but with "Check out to specific local branch", it uses a named branch for the checkout instead of a detached HEAD.