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 changes to the codebase is known as 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.
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 since 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 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—snapshots of the code when it’s ready to be released are usually merged back to 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 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.
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 allows them to be turned off if any bugs are discovered.
- 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.
- 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.
- 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 provides 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.
- 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).
- Develop in small batches
Keeping commits and branches small allows for a more rapid tempo of merges and deployments.
- Feature flags
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, we are software development experts.