Git branch shows current branch as "no branch"

We have a shell script that builds the Docker images after a successful build, uses the branch name in the tag name. For example if we had a branch called ab/new-login-flow, the Docker image build will be tagged as ab_new_login_flow-abc123de.

I have noticed the image tags are showing up as _no-abc123de. When ssh onto one of the builds and doing a git branch shows the current branch as no branch.

Does anyone anything changed recently introduces this issue.

We are using Circle CI v2.0, this is happening on a Ruby and Elixir project as well.

Many thanks for the help.

Edit:

This is how we create the tag name from the branch,
BRANCH=$(git branch | grep -e "^*" | cut -d' ' -f 2 | sed 's/[^a-zA-Z0-9]/_/g')