Skip to content

Commit 581d12c

Browse files
committed
Allow Ansible to obtain the Git branch name when building on Jenkins (with a detached head).
1 parent 2972fd4 commit 581d12c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

jobs/hook_container.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,23 @@
2727
executable: /bin/bash
2828
register: version
2929

30-
- name: Obtain current branch of this repository.
30+
- name: Obtain current branch of this repository. Does not work in detached-head state.
3131
shell: git symbolic-ref HEAD | grep -o [^/]*$
3232
args:
3333
chdir: "{{ container_dir }}"
3434
executable: /bin/bash
3535
register: branch
36+
when: ("{{ lookup('env','GIT_BRANCH') }}" == "")
37+
38+
# Alternatively, we can obtain the branch in detached-head state using the
39+
# environment variable GIT_BRANCH if it is set. This helps out Jenkins.
40+
- name: Obtain current branch of this repository using GIT_BRANCH environment variable.
41+
shell: echo $GIT_BRANCH
42+
args:
43+
chdir: "{{ container_dir }}"
44+
executable: /bin/bash
45+
register: branch
46+
when: ("{{ lookup('env','GIT_BRANCH') }}" == "")
3647

3748
# Exporting container filesystem as tarball.
3849

0 commit comments

Comments
 (0)