diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 27175c413b78c125f104648e74629663e4f1495b..2398148db301ede4118de3d6e951728450fd2553 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -11,6 +11,9 @@ variables:
 before_script:
   - set -e
   - export GRADLE_USER_HOME=$PWD/.gradle
+  # make sure the CI doesn't run in detached state so that we can
+  # extract the branch name during build
+  - git checkout "$CI_COMMIT_REF_NAME"
 
 after_script:
   # these file change every time and should not be cached