git branch name in shell script

961 views
Skip to first unread message

henrik

unread,
May 18, 2011, 9:23:13 AM5/18/11
to Jenkins Users
Hi.

We add the name of the current git branch into a file before bundling
our artifacts by calling a shell script from maven-exec-plugin. The
branch name is available in my shell script with e.g.

git branch | grep '^*' |awk '{print $2}'

or

git describe --contains --all HEAD

This works fine locally, but the jenkins git plugin uses tags to keep
track of things. This makes these git commands unable to print the
branch name.

Is the git branch name available as an environment variable? Are
there other ways of getting the branch name?

Cheers,
Henrik

Vojtech Juranek

unread,
May 18, 2011, 11:02:34 AM5/18/11
to jenkins...@googlegroups.com
Hi,
not sure if I catch what you need to do, but reference to currecnt branch is
in .git/HEAD so e.g.
cat .git/HEAD |awk -F'/' '{print $3}'
could do what you need

henrik

unread,
May 19, 2011, 2:51:52 AM5/19/11
to Jenkins Users
On May 18, 5:02 pm, Vojtech Juranek <vjura...@redhat.com> wrote:
> not sure if I catch what you need to do, but reference to currecnt branch is
> in .git/HEAD so e.g.
> cat .git/HEAD |awk -F'/' '{print $3}'
> could do what you need

Nope, that doesn't work:

$ cat .git/HEAD
1eac0da19080a06263129c60b9c8b7f49b591530

(You can try for yourself in /var/lib/jenkins/jobs/your_project/
workspace/ )

Manuel Doninger

unread,
May 19, 2011, 3:01:54 AM5/19/11
to jenkins...@googlegroups.com

If .git/HEAD contains a hash, then you're in detached HEAD state, not
on the tip of a branch. So that is correct.
If you're on a branch, it should look like this: "ref: refs/heads/development".
I also had that problem with the git plugin and detached HEAD, thus i
execute in every job a Maven pre build step (as batch command):

git checkout development || git checkout -b development

git reset --hard origin/development

to get the branch and not detached HEAD.

Manuel

henrik

unread,
May 19, 2011, 3:18:59 AM5/19/11
to Jenkins Users
On May 18, 3:23 pm, henrik <henrik.aron...@gmail.com> wrote:
> We add the name of the current git branch into a file before bundling
> our artifacts by calling a shell script from maven-exec-plugin.  The
> branch name is available in my shell script with e.g.
>
>  git branch | grep '^*' |awk '{print $2}'
>
> or
>
>  git describe --contains --all HEAD
>
> This works fine locally, but the jenkins git plugin uses tags to keep
> track of things.  This makes these git commands unable to print the
> branch name.

This one seems to do the trick:

git show-ref | grep $(git show-ref -s -- HEAD) | sed 's|.*[origin|
heads]/||' | grep -v HEAD | sort | uniq

A bit long, though :)

H

Mark Waite

unread,
May 19, 2011, 5:58:09 PM5/19/11
to jenkins...@googlegroups.com
The git plugin "Advanced" section allows you to specify the branch to be used for the checkout.  It defaults to checkout a specific SHA1 hash instead of a branch, but will perform the branch checkout if you specify the name of the branch.

I have generally preferred the plugin's "detached head" use model on all my working branches.  Recently I needed to use Jenkins to push changes from Jenkins back to the origin repository.  When I specify the branch for checkout, the plugin does very well at using that branch for the checkout.

Mark Waite


From: henrik <henrik....@gmail.com>
To: Jenkins Users <jenkins...@googlegroups.com>
Sent: Thursday, May 19, 2011 1:18 AM
Subject: Re: git branch name in shell script
Reply all
Reply to author
Forward
0 new messages