Bitbucket cloud default environment variable BITBUCKET_BRANCH value is null while triggering a build on commit's view page

Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

Summary

There is an option available on the Bitbucket Commits page to run a pipeline build on a commit.

(Auto-migrated image: description temporarily unavailable)
(Auto-migrated image: description temporarily unavailable)

The value of the $BITBUCKET_BRANCH variable will be null in these pipeline builds.

1 - echo ${BITBUCKET_BRANCH}

You can also notice from the below pipeline that the branch name is missing in a pipeline run on a commit versus a pipeline build run on a branch.

(Auto-migrated image: description temporarily unavailable)

Cause

When you run a pipeline directly on a commit, it takes only the commit as input and performs a checkout of the commit rather than checking out a branch. The git clone, as part of the pipeline build, switches into a “detached HEAD” state.

You can notice the 'detached HEAD' state warning message under pipeline build setup logs. In this case, the HEAD is not pointing to any specified branch, hence the $BITBUCKET_BRANCH variable value will be null.

1 2 3 4 5 6 7 8 + git checkout 6765*** Note: switching to '6765***'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name>

Solution

  • Option 1: Override BITBUCKET_BRANCH Value

    Run "git branch" as a pipeline script command, which should list all the branches associated with the checked-out commit hash. You can override the default variable $BITBUCKET_BRANCH value by specifying a variable with the same name and value fetched from "git branch" command.

    1 2 3 + git branch * (no branch) master
  • Option 2: Create a New Branch

    Create a new branch from the checked-out commit using the following command:

    1 git switch -c <new-branch-name>
Updated on April 8, 2025

Still need help?

The Atlassian Community is here for you.