How to control a Bamboo build based on the branch name

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

Note that this KB was created for the Data Center version of the product. Data Center KBs for non-Data-Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

The steps outlined on this article are provided AS-IS. This means we've had reports of them working for some customers — under certain circumstances — yet are not officially supported, nor can we guarantee they'll work for your specific scenario.

You may follow through and validate them on your own non-prod environments prior to production or fall back to supported alternatives if they don't work out.

We also invite you to reach out to our Community for matters that fall beyond Atlassian's scope of support!

Currently, there is no trigger available in Bamboo that checks the Plan Branch name and decides if the build needs to be triggered. There's a feature request for that: BAM-21774 - Bamboo Plan Branches should have trigger based on Branch Name.

In this article we'll go through steps to achieve this behaviour by using a script task in Bamboo. It's not possible to control the trigger, but we can have a Script Task as the first task of the Job and write a logic based on the plan branch name variable which will decide if the build proceeds ahead or fails.

Environment

All supported versions of Bamboo.

Solution

The workaround is to have a Script Task as the first task of the plan and use the Bamboo variablebamboo.planRepository.branchDisplayName to compare this value to a pattern and pass or fail the build. Please see the below examples for Linux and Windows interpreters.

In the scripts below, we use two variables. One of the variables contains the branch name; the other contains the pattern we want to match. If the pattern matches, exit 1 is called, and the Script task fails, causing the Job not to proceed ahead.

Linux

Linux Example

1 2 3 4 5 6 7 8 9 10 11 a=${bamboo.planRepository.branchDisplayName} b='master'; echo $a echo $b if [ $a = $b ] then echo "Branch Name is matching Build will be failed" exit 1 else echo "Branch Name not matching Build will proceed ahead" fi

Script task configured in Bamboo:

(Auto-migrated image: description temporarily unavailable)

Windows

Windows Example

1 2 3 4 5 6 7 8 9 10 echo $Env:bamboo_planRepository_branchDisplayName $b='master' echo "$b" if ( $Env:bamboo_planRepository_branchDisplayName -eq $b ) { echo "Branch Name is matching Build will be failed" exit 1 } else{ echo "Branch Name not matching Build will proceed ahead" }
Updated on April 8, 2025

Still need help?

The Atlassian Community is here for you.