One strategy that helps a team stay in sync with changes to the codebase is known as trunk-based development. Learn more about this strategy in this post.
Join the DZone community and get the full member experience.Join For Free
When coding an application, it is important to remain in sync with the other engineers working on the project. One strategy that helps a team stay in sync with codebase changes is trunk-based development. When employing trunk-based development, the developers working on a project make all their code changes in a common branch known as "trunk". There are numerous benefits to developing with this approach, which we will discuss in this article.
What Is Trunk-Based Development?
Trunk-based development is a version control management practice where developers merge small, frequent updates to a core trunk or main branch. It’s a common practice among DevOps teams and part of the DevOps lifecycle, as it streamlines merging and integration phases. In fact, trunk-based development is a required practice of CI/CD. Developers can create short-lived branches with a few small commits compared to other long-lived feature branching strategies. As codebase complexity and team size grow, trunk-based development helps keep production releases flowing.
Trunk-based development (TBD) is a branching model for software development where developers merge every new feature, bug fix, or other code change to one central branch in the version control system. This branch is called “trunk”. Trunk-based development enables continuous integration and continuous delivery by creating an environment where commits to the trunk naturally occur multiple times daily for each programmer. This makes it easy to meet the requirement of continuous integration, and have codebase to be releasable at any time, as is necessary for continuous delivery and continuous deployment. In trunk-based development, developers push code directly into the trunk. Changes made in the release branches, or snapshots of the code when it’s ready to be released, are usually merged back to the trunk as soon as possible. In this approach, there are cases where bug fixes must be merged into releases, but these cases are not as frequent as the development of new features in the trunk. In cases where releases happen multiple times a day, release branches are not required at all, because changes can be pushed directly into the trunk and deployed from there. One key benefit of the trunk-based approach is that it reduces the complexity of merging events and keeps code current by having fewer development lines and by doing small and frequent merges.
Trunk-Based Development and Feature Flags
In other words, trunk-based development is a methodology for releasing new features and small changes quickly while helping to avoid lengthy bug fixes and “merge hell”. It is a growing popular DevOps practice among Agile development teams and is often paired with feature flags or feature toggles to ensure that any new features can be rolled back quickly and easily if any bugs are discovered. The practice of feature flagging or wrapping new features in code that can be remotely toggled on and off is one common development process that software engineers employ to help to implement trunk-based development while reducing the risk of introducing bugs into the code. Because trunk-based development involves working in a single branch in the production environment, feature flags provide a way to introduce new features and changes into the code in a controlled fashion and quickly allow them to be turned off if any bugs are discovered.
Benefits of Trunk-Based Development
1. Allows Continuous Code Integration
Trunk-based development is a required practice for continuous integration. If build and test processes are automated but developers work on isolated, lengthy feature branches that are infrequently integrated into a shared branch, continuous integration is not living up to its potential. In the trunk-based development model, there is a repository with a steady stream of commits flowing into the main branch. Adding an automated test suite and code coverage monitoring for this stream of commits enables continuous integration. When new code is merged into the trunk, automated integration and code coverage tests run to validate the code quality.
2. Ensures Fast and Continuous Code Review
The rapid, small commits of trunk-based development make code review a more efficient process. With small branches, developers can quickly see and review small changes.
3. Enables Consecutive Production Code Releases
Teams should make frequent, daily merges to the main branch. Trunk-based development strives to keep the trunk branch “green”, meaning it’s ready to deploy at any commit. Automated tests, code converge, and code reviews provide a trunk-based development project with the assurances it’s ready to deploy to production at any time. This gives team agility to frequently deploy to production and set further goals of daily production releases.
4. Quicker Releases
Trunk-based development enables teams to ship new changes to production much faster and is a prerequisite for Continuous Integration and Continuous Delivery (CI/CD).
Trunk-Based Development Best Practices
Develop in Small Batches
Keeping commits and branches small allows for a more rapid tempo of merges and deployments.
Feature flags nicely compliment trunk-based development by enabling developers to wrap new changes in an inactive code path and activate it at a later time. Feature flags directly encourage small batch updates.
Implement Comprehensive Automated Testing
Automated testing is necessary for any modern software project intending to achieve CI/CD. Automated tests help trunk-based development by maintaining a small batch rhythm as developers merge new commits. The automated test suite reviews the code for any issues and automatically approves or denies it. This helps developers rapidly create commits and run them through automated tests to see if they introduce any new issues.
Merge Branches to the Trunk at Least Once a Day
High-performing, trunk-based development teams should close out and merge any open and merge-ready branches at least on a daily basis.
I hope you found this post useful and if you need any help with trunk-based development, let us know.
Published at DZone with permission of Ekaterina Novoseltseva. See the original article here.
Opinions expressed by DZone contributors are their own.