What is Trunk-Based Development? How Does it Work?
There are two core patterns for the developers to create a team and work on the same project together using version control. The first pattern involves using feature branches where the team of developers creates a feature from the trunk or machine and isolates the branch until the feature is entirely ready. Then the developers link the branch back to the trunk. In trunk-based development, the team of developers separates their tasks into small branches and merges those branches into the trunk multiple times or at least once in 24 hours. In this article, we will discuss trunk-based development and how it benefits an application’s life cycle.
What is Trunk-Based Development? How Does it Work?
What is Trunk-based Development?
Trunk-based development (TBD) is a software development practice and is a part of version control. Here, the developers link small and periodic updates to the main branch that we call trunk. Trunk-based development is a popular concept in the DevOps lifecycle because it helps merge and integrate the phases of the software lifecycle. To perform trunk-based development, the developers should have experience with CI/CD. The developers have to produce small branches that have a smaller life span. This type of development is most suitable for organizations with a lot of potential for growth in both team size and production environment.
When to Use Trunk-Based Development?
Before implementing TBD, there are two main features that you need to consider. The first thing is TBD can move rapidly and the second one is that no matter what the developers do, they won’t lose their configurations. Both these features are also known to be the main benefits of trunk-based development. But still, TBD is not for everyone. For instance, if your company is a start-up and you are planning on creating the .01 version of your product and you also have a team of relatively experienced developers, then you can implement TBD. You have to keep in mind that to use TBD, you need trusted developers and speed.
But when you are creating an open-sourced project and speed is not that important for you, and you cannot trust just everyone with a Git request, then you can’t work with TBD. In that case, a fault-tolerant process such as GitFlow is more suitable for you.
Benefits of Using Trunk-Based Development
Trunk-based development is mandatory for continuous integration. But if the build and test processes are automated and the software programmers are working with isolated feature branches that are long and rarely merged with the shared branch, then continuous integration will fail. With trunk-based development, you can reduce the chances of friction in code integration. After finishing the work, the developers need to link the code with the core branch. However, they have to be careful not to merge the modifications to the trunk prior to verifying that the build was successful. Also, in this phase of software development, developers might experience some complex conflicts. But trunk-based development reduces the chances of those conflicts. Here are the benefits of Trunk-based Development.
1. Enables Continuous Code Integration
In the repository inside TBD, there is a stable series of commits that are entering the main branch. When you add an automated test suite and coverage monitoring for the code in the series, it will allow continuous code integration. Whenever a new code enters the trunk, code coverage monitoring and automated integration will run together to verify the quality of the code.
2. Provides Continuous Reviews for the Codes
There are small and quick commits in trunk-based development which helps in code review. Since there are smaller branches in the development environment, the developers can easily find and review even the tiniest changes made. Trunk-based development is far more convenient than feature branch development where the branches are larger than trunk-based branches. In the feature-branch development, the reviewer has to read several pages of the codes or have to examine the codes manually for a minor or major change.
3. Allows Successive Exhibit Code Releases
In trunk-based development, the developers have to keep the main branch updated with frequent and daily merges. Trunk-based development always tries to keep the trunk in-branch ready for the developers to perform commits. There are automated tests, code reviews, and code coverage performed on trunk-based development that ensure that the branches are ready to be deployed in the production environment. This allows the developers the confidence that they can deploy the branches on the production cycle.
To avail of the benefits of trunk-based development, you will have to learn how to implement it in your software practice correctly.
How to Execute Trunk-Based Development?
Trunk-based development is needed for continuous integration which we call CI. CI contains trunk-based development and a suite that maintains quick automated tests. These automated tests run after the commits in the trunk to ensure that the development is properly working. The best use of continuous integration is to reduce lengthy integration and stabilization phases by connecting small branches of codes repeatedly in the trunk. This is the best way for the developers to ensure that they are intercommunicating their tasks correctly and that the integration that they are performing can reduce lengthy merges that will create additional work for the other team members and testers.
In the continuous integration method, the developers are always working to keep the build process in the green format which means that the build process is always ready and running. This means that when the continuous integration process fails, the testers or team of developers will have to stop every task at hand and fix the cause of the failure instantly, or if they can fix the failure soon enough, they will have to revert the changes. As mentioned before, to practice or implement trunk-based development, the developers need to understand how to divide their work into small branches. According to the analysis of DevOps research and assessment, the best practices for trunk-based development are:
- The developers need to have access to three or fewer active branches in the application’s code repositories.
- The branches need to be merged into the trunk at least once within 24 hours.
- There should not be any freezes in the codes and integration.
However, even when the developers implement the trunk-based development strategy carefully, they still encounter some common pitfalls.
Hindrances in Trunk-Based Development
Some of the most common barriers that block the successful adoption of trunk-based development are:
- The code review process is heavy and needs various approvals before the changes can be linked to the trunk. The code review process can take hours or days for the developers and that’s why they stop working on small branches and combine the branches for changes. This can reduce productivity and also impact the system hugely.
- The code review process might not always be performed synchronously. If your developers’ team reviews a code by two developers at the same time, then there is a possibility that the code has been reviewed already by the other developer. If another review is needed then, the other developer will have to be ready to commit the code. But the team should be careful about an asynchronous review which means once they submit a review request to the team, then they can start on a new assignment whilst waiting for the reviewer to submit their approval. If the margin is delayed, it can create conflicts in the production environment.
Conclusion
Another mistake that the developers commit is not executing an automated test before executing a code. The developers need to ensure that the trunk is in a proper state and for that, they have to run the test against the code modifications before they execute the code. Trunk-based development is one of the highest standards for high-performing developers teams because it helps them maintain the application using simple Git branches. Moreover, there is more flexibility and control over the software environment when the developers are using a trunk-based development strategy.
Simran works as a technical writer. The graduate in MS Computer Science from the well known CS hub, aka Silicon Valley, is also an editor of the website. She enjoys writing about any tech topic, including programming, algorithms, cloud, data science, and AI. Traveling, sketching, and gardening are the hobbies that interest her.