CIRCLE_BRANCH and << pipeline.git.branch >> are empty

Hello,

I’m trying to use CIRCLE_BRANCH in my pipeline. However, the variable is empty.

      - run: echo "This is pipeline ID << pipeline.id >>"
      - run: echo "Building branch $CIRCLE_BRANCH"
      - run: echo "Building branch << pipeline.git.branch >>"

What am I missing?

We’re using Bitbucket. CircleCi is triggered via tags
workflows:

  build:
    jobs:
      - build:
          filters:
            tags:
              only: /(stable|beta|alpha|dev|exp)-.*/
            branches:
              ignore: /.*/

Thanks a lot for your help!

I’m assuming that CIRCLE_BRANCH is empty when the pipeline is triggered via tags. So I have created a bit of a clumsy piece using tag and commit id to determine the branch.

      - run: 
          name: Determine commit ID & build branch
          # determine branch from tag using commit ID. 
          # Note: "git branch -a --contains <tag>" could return multiple entries
          #       however, since the pipeline runs right after the tag is comminted
          #       this should not become an issue
          command: COMMIT=$(git show-ref ${CIRCLE_TAG} | awk '{print $1}') 
                   && TMP=$(git branch -a --contains $COMMIT) 
                   && BRANCH="${TMP##*/}" 
                   && echo "Building branch $BRANCH"

Maybe this will be of use to someone else. Of course, any feedback and improvement suggestions are very welcome.

Hi @BSBoatia ,

It is indeed expected that both the CIRCLE_BRANCH built-in environment variable and pipeline.git.branch pipeline value will be empty when your build is triggered by a tag-push.

You could also use the method described in this post: Dynamically set the value of a job parameter.

For me works this solution

 - run: 
          name: Determine commit ID & build branch
          # determine branch from tag using commit ID. 
          # Note: "git branch -a --contains <tag>" could return multiple entries
          #       however, since the pipeline runs right after the tag is comminted
          #       this should not become an issue
          command: COMMIT=$(git show-ref | grep "${CIRCLE_TAG}" | awk '{print $1}') 
                   && TMP=$(git branch -a --contains $COMMIT) 
                   && BRANCH="${TMP##*/}" 
                   && echo "Building branch $BRANCH"