Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Git branch is "(no branch)" on hudson CI

My Ant build.xml script starts with

<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...

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

* (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.

like image 692
milkplus Avatar asked Oct 04 '10 19:10

milkplus


People also ask

What is no branch in git?

The “no branch” state is called a detached HEAD. It is called this because the HEAD ref is not attached to any branch, instead it is pointing directly at a commit. To attach HEAD to a branch that points to the current HEAD commit, use git checkout -b branchname .

How do I show a branch in Visual Studio?

Select an existing branch in Visual Studio 2019 Visual Studio displays the current branch in the selector at the top of the Git Changes window. The current branch is also available in the status bar on the bottom-right corner of the Visual Studio IDE.

Can branches have branches in git?

It is a feature available in most modern version control systems. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes.

What is git branch command?

The git branch command lets you create, list, rename, and delete branches. It doesn't let you switch between branches or put a forked history back together again. For this reason, git branch is tightly integrated with the git checkout and git merge commands.


2 Answers

RESOLVED

After seeing the comment by abayer in Hudson bug 6856, I took the following steps:

  • 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"

alt text

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.

I'm now able to push successful build tags to my git repo.

like image 53
milkplus Avatar answered Sep 23 '22 01:09

milkplus


Note: the OP milkplus's comment refers to recent Hudson bug 6856 (June 2010), which mentions:

Git builds with detached head no matter what

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.


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.

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.

Your building script could first try to find what branch the relevant commit is coming from.


As the OP milkplus realized by looking at the source code of the 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:

<property environment="env"/>
<echo>GIT_BRANCH = ${env.GIT_BRANCH}</echo>

If that is the case, it may be because of issue 7554:

GIT_BRANCH not set when multiple branches are selected for build

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.

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.

Kind of mirror the NPE described here

alt text

For some reason Git plugin started to pass null value for GIT_BRANCH environment variable.
This caused Maven plugin to fail in System.getProperties().putAll(systemProps) call.

The solution was to use "master" as default Git branch instead of "**" or empty String.

like image 29
VonC Avatar answered Sep 26 '22 01:09

VonC