5 October 2022

Software Development

Trunk-based development: everything you need to know

11 minutes reading

Trunk-based development: everything you need to know

All development teams strive for an agile, flexible, and efficient software development process. There are many ways to achieve it. Trunk-based development is one of the more modern approaches that is gaining attention. Let’s take a closer look at what it is, how it works, and what benefits it can bring your team. 

What is trunk-based development?

Trunk-based development is a management practice. It is a source-control branching model focusing on a single branch of code called the trunk. Trunk-based development aims to keep the trunk ready for deployment at all times. Developers collaborate on small parts of code, which are frequently updated and merged into the core branch. 

software outsourcing services

It is an agile, rapid process - the code review is efficient, and developing and merging the branches shouldn’t take more than a few days. Trunk-based development is close to the ideas of the DevOps methodology, and it also takes advantage of automated testing and integration. 

a diagram showing trunk-based development

Fig. 1 Trunk-based development diagram

This model aims to achieve CI/CD (continuous integration/continuous delivery) and increase the team's efficiency and the product's quality. When properly deployed, it can streamline the merging process and help avoid integration hell. It is useful in a situation where merging is problematic, and new parts of code must be fixed, debugged, and sometimes completely rewritten to be integrated into the existing code. 

Trunk based-development stands in opposition to traditional branching models, which require developing long-lived branches. This process takes much longer, as the branches are more complex. Also, merging difficulties can set the team back and slow down progress. 

diagram of feature branch development

Fig. 2 Feature branch development diagram 

Benefits of trunk-based development

The trunk-based approach makes developers’ lives easier in many ways. Most importantly, merging runs as smoothly as possible. Let’s take a closer look at the trunk-based model's benefits.

Reduced merging complexity

Complex, time-consuming, complicated merging is precisely the issue that trunk-based development aims to solve. Reducing the size of new branches helps to keep integration more manageable, saving time, labor, and money.

Continuous integration

Trunk-based development is one of the ways to achieve the CI/CD pipeline. A slower, less flexible approach would make continuous integration much harder. There is a constant stream of new parts of code flowing into the main branch. Therefore automated testing and integration are a must. 

If you’re interested in the topic of CI/CD, check out our other articles:

Continuous code review

The smaller size of newly developed branches makes it possible to acquire instant feedback. Branches are also less complex than in the traditional model, so finding the source of a problem and making necessary changes is quicker and easier. 

No duplicated work

Because the teams deploy their branches frequently, there is less possibility for duplication of work, even if various developers are working on similar issues. As a result, teams stay on the same page, staying up-to-date with all the changes happening in the trunk

Uninterrupted code releases

According to the rudiments of the trunk-based model, the core branch should always be ready for deployment. It is possible thanks to instant feedback and the constant fixing of new parts of the code. Automated testing also plays a significant role here, making the process less time-consuming.

All the above benefits come together to provide a smooth, efficient software development process. Using this model saves time and labor and, therefore, results in a more cost-efficient development.

Trunk-based development vs Gitflow

It’s important to note that trunk-based development is a successor to other practices that initially disrupted the scene of software development management. One of them is Gitflow. It was the workflow that provided a modern strategy for managing branches. However, it is much less popular than it used to be, as it is challenging to manage while maintaining the CI/CD pipeline. 

Gitflow is a branching model based on feature branches and multiple primary branches. It uses longer-lived branches than trunk-based development, and merging them is delayed until a feature is complete. 

This approach requires more collaboration during the merging process, making it more time-consuming. There is also a higher risk of issues compared to trunk-based development, and the core branch does not stay ready for deployment.

Gitflow vs Trunk - when to use them?

Trunk-based development is now a common practice among software development teams, but Gitflow can still prove helpful in some instances. For example, Gitflow can be used in projects with a scheduled release cycle

Working on a single feature at once for an extended time also can have its benefits. For example, you can conduct isolated experiments. The stricter rules of Gitflow also ensure code quality - the branch can be integrated into the code only once specific individuals accept it. 

When is trunk-based development helpful?

Trunk-based development is a specific management style that will not work for every software project. When deciding whether to use this approach or not, you should consider two factors.

First, it’s important to remember that this development style means rapid progress. The team should be ready for that, so there should be a transparent communication strategy. That way, the developers are on the same page and continuously updated about changes in the trunk. Rapid progress also means that the team needs to move quickly. The trunk-based approach requires a motivated, competent development crew.

The other thing to keep in mind is that trunk-based development puts a lot of trust in developers. There is a short code review process, so each developer needs to be careful about the changes they commit to the trunk. That means that this style of software development management probably won't work for open-source projects where anyone can open pull requests.

That said, trunk-based development is still a good choice for many situations. For example, it works great for startups and other teams that are good at collaborating and keeping high development speed. If you're curious about implementing this approach in your project, check out our custom software development services.

How to make trunk-based development work?

Once you know what trunk-based development is and when to use it, you should learn how to make it work for your team. Here we share some best practices that make trunk-based development run smoothly and effectively.

  • Three or less

That’s the number of branches that should usually be active on an application repositories’ version control systems. Sometimes it can differ due to a project’s specifics, but it’s important to set up a clear number. The number should be easy to manage and to make sure it's visible to everyone on the team.

  • No code freezes

Avoiding code freezes and pauses for integration phases is crucial for successful trunk-based development. In the CI/CD pipeline, there should be a constant flow of updates. Therefore, merging conflicts and code freezes that stop developers from submitting code should happen as rarely as possible.

  • Merging once a day

The frequency of merging is an important measurement for trunk-based development. The environment should encourage every developer to commit to the trunk at least once every 24 hours. It is a requirement for continuous integration.

  • Synchronous code review

An efficient code review that doesn’t take hours or days to get final approval is key for performing daily merging. Developers shouldn’t start new parts of code until the previous one is merged - that helps keep the team focused. The shorter the merging latency is, the less possibility there is for merging problems.

Measuring the success of trunk-based development

In order to track whether your team meets the trunk-based development standards, you need some measuring methods. These metrics are similar to the above-mentioned best practices, and they make it easy to recognize room for improvement.

The factors to test include:

  • the number of active branches on the application’s code repository (usually three or fewer)
  • the number of code freeze periods (should be zero)
  • frequency of merging branches (at least once a day)
  • time of approving code changes (should be as short as possible).

These four checkpoints can keep your team on track with the requirements of trunk-based development.

Conclusion

Trunk-based development is a good tool for keeping software development running smoothly. This practice aims to save the time and labor of developers, minimize the possibility of merging difficulties and utilize test automation. It has certain requirements and won’t work for every project, but it can significantly improve a team when deployed correctly. It is also a cost and time-effective version control management approach.

Maciej

Maciej Manturewicz

Director of Engineering