[JIRA] [git-plugin] (JENKINS-6856) Git builds with detached head no matter what

7 views
Skip to first unread message

petteyg359@gmail.com (JIRA)

unread,
Jun 23, 2015, 2:51:01 PM6/23/15
to jenkinsc...@googlegroups.com
Gordon Pettey commented on Bug JENKINS-6856
 
Re: Git builds with detached head no matter what

The detached state breaks build tools that attempt to find the current branch name by calling git branch. When running a project that has multiple branches, the workaround in the previous comment requires n jobs for n branches instead of 1 job.

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v6.4.2#64017-sha1:e244265)
Atlassian logo

raphsoft@gmail.com (JIRA)

unread,
Aug 18, 2015, 11:15:05 AM8/18/15
to jenkinsc...@googlegroups.com

I have the same problem. Is there any way to avoid the plugin using detached state? I will suggest this to be reopen.

tanzinul.islam@gmail.com (JIRA)

unread,
Aug 18, 2015, 4:11:03 PM8/18/15
to jenkinsc...@googlegroups.com

I have this workaround, this depends on the plugin setting the GIT_BRANCH environment variable with the remote ref (including the leading "origin/") that it checked out. So have an Execute Shell build step before your commands:

#!bash
git checkout ${GIT_BRANCH#origin/}

The above will check out the branch properly, before running your command. Also, have the following commands in another Execute Shell build step after your commands to do the necessary cleanup:

#!bash
git checkout $GIT_BRANCH
git branch -D ${GIT_BRANCH#origin/}

ceddlyburge@gmail.com (JIRA)

unread,
Sep 30, 2015, 7:39:02 AM9/30/15
to jenkinsc...@googlegroups.com

I agree, this doesn't seem to have been fixed, and it would help me if it was. It seems like an easy change too. I wonder if there is a complication that we aren't aware of?

mark.earl.waite@gmail.com (JIRA)

unread,
Oct 9, 2015, 5:19:03 PM10/9/15
to jenkinsc...@googlegroups.com

Can't you add the "Additional Behaviour" of "Checkout to a specific branch"? That will give you a named branch.

Changing the plugin from using a detached head to using a branch is non-trivial if the goal is to not break a significant portion of the 70 000+ installations of the git plugin. There are too many use models and too many cases where silently switching to use a branch (instead of a detached head) would cause compatibility problems.

ceddlyburge@gmail.com (JIRA)

unread,
Oct 12, 2015, 4:11:02 AM10/12/15
to jenkinsc...@googlegroups.com

If its difficult then fair enough. I am running git commands in a batch script to get round this issue, which is fine, although I do end up wondering what I am getting out of the plug in in this case.

The checkout to specific branch does work, but has complications when building multiple branches (which is a great feature of the plug in).

Maybe adding another environment variable for the branch name would be useful (eg without the "origin/" bit at the start).

I do think that this shouldn't be marked as "Fixed", when it definitely hasn't been. It's misleading and means people will spend time trying to work out why it isn't working.

mark.earl.waite@gmail.com (JIRA)

unread,
Oct 12, 2015, 12:14:02 PM10/12/15
to jenkinsc...@googlegroups.com

I think the bug is fixed. I just ran a series of tests which confirmed that I can checkout to the branch

${GIT_BRANCH}

. It expands that variable and performs a checkout of the correct branch.

Steps I took

  1. Create a new job JENKINS-6856 -checkout-to-GIT_BRANCH
  2. Use git as the SCM
  3. Use git://github.com/MarkEWaite/git-client-plugin.git as the repository to checkout
  4. Add "Additional Behavior" to checkout to a specific branch, with the value
    $GIT_BRANCH
  5. Check the "Poll SCM" check box (no polling schedule needed)
  6. Save the job
  7. Click the "Poll Now" link on the left side of the job (assuming you have the "Poll Now" plugin installed

That series of steps resulted in immediately running 19 jobs, one for each branch in that repository. Each of those 19 jobs used a branch named precisely for the branch in the origin repository.

What it does not provide is a way to checkout a named branch which is a substring of

GIT_BRANCH

. I think that is what you need, and what is requested in pull request 347

There is some facility to allow substrings of GIT_BRANCH, but I was unable to make it work in the cases I was testing. The GIT_BRANCH variable is implemented inside the git plugin as a "token macro" which takes two optional arguments, fullName and all. If the fullName parameter is false, then the GIT_BRANCH value is to be returned with everything to the right of the leftmost slash character. Unfortunately, I was unable to see the token macro expanded when used in the context of "Branch to build". If I wrote it as

${GIT_BRANCH,fullName=false}

, then the GIT_BRANCH variable was not expanded.

The token macro facility needs more attention inside the git plugin (as far as I can tell), before I'd be confident saying that it works as intended.

mark.earl.waite@gmail.com (JIRA)

unread,
Oct 12, 2015, 12:17:04 PM10/12/15
to jenkinsc...@googlegroups.com

Earlier comments in the bug report allude to other ways to checkout the exact desired branch name, so long as you have a bash compatible interpreter available (most Unix variants).

I used this (combined with the earlier checkout to a specific branch name) as a shell script step to checkout a tracking branch

HEAD=$(git rev-parse --abbrev-ref HEAD)
CURRENT_BRANCH=remotes/origin/${HEAD##*origin/}
WORKING_BRANCH=${CURRENT_BRANCH##*origin/}
git checkout $WORKING_BRANCH || git checkout -b $WORKING_BRANCH -t $CURRENT_BRANCH

scm_issue_link@java.net (JIRA)

unread,
Oct 12, 2015, 2:39:02 PM10/12/15
to jenkinsc...@googlegroups.com

Code changed in jenkins
User: Mark Waite
Path:
src/test/java/hudson/plugins/git/TestBranchSpec.java
http://jenkins-ci.org/commit/git-plugin/91a1ffc3a6030e8d74fe463edff13d7f1f08fd41
Log:
Add tests exploring

JENKINS-6856 - token macro expansion

BranchSpec does not seem to honor token macros. For example,
$GIT_BRANCH matches as expected
$

{GIT_BRANCH}

matches as expected
$

{GIT_BRANCH,fullName=False}

does not match
$

{GIT_BRANCH,fullName=True}

does not match

I expected either all of them to match as expected (token macro expansion
was being applied), or none of them to match (no variable expansion and
no token macro expansion). As far as I can tell, variable expansion is
being applied, but token macro expansion is not being applied.

daniel@bengtssons.info (JIRA)

unread,
Apr 6, 2016, 9:00:02 AM4/6/16
to jenkinsc...@googlegroups.com

This confused me too, my scripts rely on finding the name of the current branch which was not possible. It did work however using $GIT_BRANCH as mentioned above, but I did not find that out before finding this issue. Couldn't that be documented for the "Check out to specific local branch" field ?

mark.earl.waite@gmail.com (JIRA)

unread,
Apr 6, 2016, 10:22:03 AM4/6/16
to jenkinsc...@googlegroups.com

Daniel Bengtsson you could also use the "Local Branch" extension that was added to git plugin 2.4.4. Refer to the online help for the local branch extension.

In the Local Branch extension, you set the branch name to "**" or the empty string, and that then causes the plugin to checkout to a branch name derived from the branch that was the origin of the checkout. That allows a branch specifier to select multiple branches, then the branch which is checked out is based on the specific branch selected for that run of the job.

rausalinas@gmail.com (JIRA)

unread,
Sep 12, 2019, 11:00:08 AM9/12/19
to jenkinsc...@googlegroups.com

Isn't it enough to do the following to get a proper branch head at some point after the checkout?  It seems to work for me. 

git checkout -b ${GIT_BRANCH} origin/${GIT_BRANCH}

This message was sent by Atlassian Jira (v7.13.6#713006-sha1:cc4451f)
Atlassian logo

mark.earl.waite@gmail.com (JIRA)

unread,
Sep 12, 2019, 11:12:03 AM9/12/19
to jenkinsc...@googlegroups.com

Yes, the `git checkout` command you used is a very good choice, especially if you're not using authenticated submodules or authenticated access to large file storage (git LFS).

mark.earl.waite@gmail.com (JIRA)

unread,
Sep 12, 2019, 11:13:02 AM9/12/19
to jenkinsc...@googlegroups.com
Mark Waite edited a comment on Bug JENKINS-6856

mark.earl.waite@gmail.com (JIRA)

unread,
Oct 22, 2019, 9:31:24 PM10/22/19
to jenkinsc...@googlegroups.com
Mark Waite closed an issue as Fixed
 
Jenkins / Bug JENKINS-6856
Change By: Mark Waite
Status: Resolved Closed
Reply all
Reply to author
Forward
0 new messages