How to select the branch used by Linked Repositories in a Plan Branch in Bamboo via Java Specs

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

Code customisation, including Java Specs or YAML Specs samples, is not part of the Atlassian Support scope. This work is provided "as-is", on a best-effort basis. Some sample codes may work better or worse than expected. It is up to each customer to analyse each sample code and understand if that is enough for their specific needs. Further programming knowledge is also required and is not covered by this article.

Summary

This article demonstrates sample Java Specs code that can be used to automate the selection of the repository branches used by Linked Repositories in by Plan Branches.

Environment

  • Bamboo Server or Data Center

  • Bamboo Specs (Java), optionally configured as Repository Stored Specs (RSS)

  • Linked Repositories

Diagnosis

When using Plan Branches, Bamboo allows customers to manually select the repository branches used by repositories via the UI by following the Plan Branch Configuration >> Repositories (tab) >> Select Repository Branch path. Though this strategy works in most of cases, some customers would wish to have an automated way of doing that via Bamboo Specs code, optimally stored in a Repository.

When using Bamboo Specs, the UI interaction is disabled. This article demonstrates the equivalent settings using Java Specs.

Solution

It is necessary to use Java Specs to specify the branches used by each repository in a Plan. Currently, this feature is yet to be implemented in YAML Specs.

Sample pseudo-code

Here's an example of how to set a Plan Branch repository configuration using Java Specs:

1 2 3 4 5 6 7 8 9 import com.atlassian.bamboo.specs.api.builders.repository.VcsRepositoryBranch; // ... <add the rest of your plan code here>           .linkedRepositories("Default-Linked-Repo-Name", "Repository-A", "Repository-B") .repositoryBranches(new VcsRepositoryBranch("Repository-A", "non_default_branch") .branchDisplayName("branch_name"), new VcsRepositoryBranch("Repository-B", "yet_another_non_default_branch") .branchDisplayName("branch_name"))

A more complex example, make the repository branches used by non-default repositories follow the same repository as the one used by the default Linked Repository

In the example below, Repositories "A", "B", "C" are used. "A" is the "default" repository. The pseudo-code below will make any branches used by non-default repositories follow the same ones used by the default "A" repository.

The Specs code is hosted on a Repository as "Repository Stored Specs" and is used to evaluate the "current" branch used by the Specs repository via com.atlassian.bamboo.specs.api.context.RssRuntimeContext:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 import com.atlassian.bamboo.specs.api.builders.repository.VcsRepositoryBranch; import com.atlassian.bamboo.specs.api.context.RssRuntimeContext; import java.util.ArrayList; import java.util.Arrays; import java.util.List; // ... Additional imports @BambooSpec public class PlanSpec { /** * The current branch name is retrieved from the RssRuntimeContext. * If no branch is found, it defaults to "master". */ final String branchName = RssRuntimeContext.getCurrentRssBranch().orElse("master"); /** * A boolean flag indicating if the current branch is the default RSS branch. * If the information is not available, it defaults to false. */ final boolean isDefaultRssBranch = RssRuntimeContext.isDefaultRssBranch().orElse(false); /** * A list of Linked Repository names to be used by the plan. * The first element of the array will be set as the Default branch. */ final List<String> linkedRepositories = Arrays.asList("A", "B", "C"); /** * Sets the repository branches for the given plan based on the provided linked repositories. * If the current branch is not the default RSS branch, this method will create a list of * VcsRepositoryBranch objects for each linked repository, excluding the first (default) branch. * If the resulting list of repository branches is not empty, it will be set to the given plan. * * @param plan The plan for which the repository branches should be set. * @param linkedRepositories A list of linked repository names. */ private void setRepositoryBranches(Plan plan, List<String> linkedRepositories) { // Check if linkedRepositories has only 1 element (default repo) and exit silently if (linkedRepositories.size() <= 1) { return; } if (!isDefaultRssBranch) { List<VcsRepositoryBranch> repositoryBranches = new ArrayList<>(); for (String repo : linkedRepositories) { repositoryBranches.add(new VcsRepositoryBranch(repo, branchName).branchDisplayName(branchName)); } // Remove the first element of the list (the Default branch) repositoryBranches.remove(0); plan.repositoryBranches(repositoryBranches.toArray(new VcsRepositoryBranch[0])); } } public Plan plan() { final Plan plan = new Plan(new Project() // ... <add the rest of your plan code here> // Set linked repositories based on the Array list .linkedRepositories(linkedRepositories.toArray(new String[0])) // ... // Before returning the Plan, call the method to set their branchName setRepositoryBranches(plan, linkedRepositories); return plan; }

All Specs reference documentation can be found here:

More information on Specs:

Updated on April 1, 2025

Still need help?

The Atlassian Community is here for you.