How to avoid a clean rebuild with each new feature branch?
Asked by Dean Reading:

**My Bamboo setup**

I'm working with a very large repo. For a desired change, I make a feature branch in the repo with the desired changes. My Bamboo Plan then detects the new branch and creates a plan branch. The Bamboo job clones the branch and performs a clean build.

**My issue**

The clean build takes several hours. Many times, the feature branch might only be a small change. It might only take a minute to perform an incremental build on top of trunk.

++How can I get my feature branch to be built in Bamboo as an incremental build?++

This may require copying build files so that both trunk and the feature branch can continue incremental builds.

It may also require some smarter selection of the build files to use as a starting point (e.g. if my feature branch stems from release_3.0 instead of trunk).


Answers:
author: alok m
body: Optimizing build times in Bamboo, especially for projects with very large repositories, requires a strategic approach to handling incremental builds. Incremental builds compile or process only the changes in the codebase instead of rebuilding everything from scratch, saving a significant amount of time. Here are several strategies you might consider to implement incremental builds for feature branches in Bamboo:

**1. Utilize Bamboo's Artifact Sharing**   
Bamboo allows the sharing of artifacts between different plans or different stages of the same plan. You can configure your main build plan to publish artifacts (e.g., compiled classes, jars, or any intermediate build files). Then, your feature branch build plans can use these artifacts as a starting point for an incremental build, reducing the need to recompile unchanged code.

**2. Implement a Custom Script for Incremental Builds**   
Detect Changes: Write a script that compares the current branch with its base branch (e.g., trunk or release_3.0) to detect changes. This script can be a part of the build process in Bamboo and should determine if an incremental build is possible or necessary.

Cache and Reuse Build Artifacts: Implement caching mechanisms to store build artifacts from previous builds. This cache can be stored in a shared location accessible to Bamboo. When a feature branch build is triggered, the script checks the cache for artifacts that can be reused and only builds changed components.

Smart Selection of Base Artifacts: Enhance the script to intelligently select which version of the artifacts to use based on the branching structure. If a feature branch stems from a specific release rather than the trunk, the script should be able to identify and use artifacts from that release as a starting point.

**3. Use Bamboo's Dependency Management**   
Leverage Bamboo's built-in dependency management features to manage dependencies more efficiently. By properly managing dependencies, you can avoid unnecessary rebuilds of components that haven't changed. This approach requires a well-structured project and consistent management of dependencies.

4. Optimize the Repository  
For very large repositories, consider strategies to optimize the repository size and structure:

Splitting the Repository: If possible, split the large repository into smaller, more manageable repositories. This can significantly reduce the time required to clone the repository for a build.  
Shallow Clones: Use shallow clones to clone only the latest commits necessary for the build instead of the entire repository history.  
5. Bamboo Specs for Configuration as Code  
Use Bamboo Specs, Bamboo's configuration as code solution, to programmatically define and manage your build plans. With Bamboo Specs, you can script complex behaviors, like conditional steps for incremental builds, more efficiently and consistently across multiple plans.

Implementing incremental builds in Bamboo, particularly for projects with large repositories, requires a combination of Bamboo's features and custom scripting. A successful implementation can drastically reduce build times and improve the development workflow. Each strategy has its considerations and might require adjustments based on your specific setup and requirements.