After discussions come to the following: use GIT_BRANCH env. variable if set by Jenkins Git plugin, otherwise call git branch and take only current branch (starred) name. If branch name is detached, then assign string DETACHED as branch name.