'Bamboo build plan on branches but not on develop (aka conditional build)
This is an attempt to implement a conditional build across branches, impossible by default because of BAM-11257, open since 2012, still pending.
Context:
- Bamboo 5.10.3
- Gitflow branching strategy applied
- We would like to have a certain behavior on feature branches, a different behavior on release and hotfix branches, and a third different behavior on the
developbranch
Implemented (not working) approach:
- Three different build plan
- The first provides a regex in the "Branches" tab of the build plan ("Create plan branches for matching new branches) to only match feature and bugfix branches (
(?=(feature.*|bugfix.*)).*$). Then it provides the desired behavior in its stages and jobs - The second one as above configured for release and hotfix branches (
(?=(hotfix.*|release.*)).*$), then with its own behavior in stages - The third has the "Do not create plan branches" in its "Branches" control, that is, no branching, only for
develop.
This works fine, each branch has its own build plan based on the desired behavior. However, when merging any branch into develop, the three build plan start in parallel, because the three of them have develop in their "Repositories" as original branch entry.
Question: how could this side effect be avoided? How could we specify a build plan for certain branch types only and not also for the original branch (in this case develop)?
I understand Bamboo needs an origin from which cloning new build plan for its branches according to a certain strategy (the regex), but how can we block it to run the build plan for the original branch as well? Or, can we define a build plan for a branch type independently than its origin and based on a dynamic name (e.g. feature-something, feature-bob)?
(On this last point: in "Repository" we can specify a branch as origin, but that's a static value, we cannot put a regex there, which would fix the overall issue, I suppose)
Solution 1:[1]
It is possible to have a build plan in Bamboo defined for branch "types" and not linked to other branches by default. Hence, a Bamboo plan configuration which will only apply to dynamic branches, with no default branch to run upon.
The main misunderstanding comes from the following points:
- In the "Repositories" tab of a Plan Configuration, you would probably have a "BitBucket / Stash" repository, where you would select your repository and also a branch, you have to, it's a dropdown box with a mandatory default value. That's the main issue here. This option obliges you to have a build plan already defined for an existing branch (e.g.
develop), while you actually would like to have it for dynamic branches (e.g. a feature branch not yet created). - Then, in the "Branches" tab, under the "Automatic Branch Management" section, you can indeed select a branching strategy according to a regex, to suit dynamic branches (e.g. the
(?=(feature.*|bugfix.*)).*$I mentioned above), the build plan would be cloned accordingly, but the main issue is that the whole build plan will still also run on the default branch of the default repository, something you often don't want. - Still, in the "Branches" tab you can read that "Plan branches allow you to run builds across different branches in your source repository using the same plan configuration.". Note the bold: the branching strategy is applied to branches in the whole repository, not only branches which were created from the default branch you defined in the Repositories entry, but to any branch in the repository.
- That's the turning point: you can hence define a new branch, parallel to
masteranddevelop, let's call itbamboo-management-branch, you can even prevent its deletion or changes via Bitbucket, then use this branch as default branch of the default repository entry in the initial plan configuration. That's it. Now, you have a branch that nobody will use within the team, a build plan which will never run, but will be cloned according to your branch strategy on dynamic branches within the repository.
The option suggested above, to use a "Git" and not a "Bitbucket / Stash" repository is also perfect, since there the "branch" option is not mandatory, is an open text field (and not a dropdown list), hence you can type a non existing branch name there as workaround (note: if you leave it empty, it will point to master by default, as per official documentation.
Here is the forced branch name you need to provide for an existing branch in a "Bitbucket / Stash" repository entry:

As you can see, I created a dummy ci-branch to make it happy, it will not be used by the dev team, it's an additional branch on top of gitflow branches, but it only serves the CI managements.
Then, the branches strategy can be applied as usual:

Again, clarifying that it's the default repository the important part, not the default branch. Branches can be created anywhere in that repository and not only from the default branch. Builds will be cloned as long as the regex will be happy.
Finally, here is the additional option to use a pure "Git" repository entry and not the "Bitbucket / Stash":

As you can see, the branch entry here is optional, which also suits our needs, although you may miss some nice automatic Bamboo-Bitbucket integration then (that's why, although this approach is clean, a dummy branch in a bitbucket entry sounds still reasonable).
Solution 2:[2]
I wanted to leave this here and note that nowadays there's the conditional task which can be configured to run only on certain branches.
Solution 3:[3]
Could you make a change to the regex in the first two build plans to not be execute if it contains the string "develop"?
something similar to this negative lookahead: ^((?!develop).)*$
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Alex |
| Solution 3 | isaac-fisher |
