How to control a Bamboo build based on the branch name

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the Server and Data Center platforms.

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

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!

Summary

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 - Getting issue details... STATUS

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 variable bamboo.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
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:

Windows

Windows Example
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"
}





Last modified on Apr 19, 2023

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.