Path to Agile: Trunk-based Development

8grams
7 min readMay 24, 2023
Trunk-based Development

Introduction

In the software development world, managing changes effectively is paramount. This is where version control systems like Git come in handy, providing various methodologies to handle code changes. One such approach is trunk-based development, a technique gaining popularity due to its alignment with Agile principles and Continuous Integration/Continuous Deployment (CI/CD) practices.

What is Trunk-based Development

Trunk-based development is a source code management strategy where all developers work on a single branch, often referred to as trunk or main. Originating from the earlier days of version control systems like Subversion, this practice has found relevance in the Git environment as well.

Key principles of trunk-based development include:

  • A single branch for all code: Everyone commits to the trunk/main branch, and feature branches, if used, are short-lived.
  • Frequent commits: Changes are integrated regularly, minimizing the risk of conflicts.
  • Green build policy: The trunk is always kept in a releasable state.

Working with trunkin Git entails a shift from long-lived feature branches to shorter-lived ones, with developers merging their changes frequently to maintain synchronization with the main codebase.

Git offers various development models, each with its own benefits and drawbacks. GitFlow, feature branching, and fork-based workflows are examples. The primary difference between trunk-based development and these models lies in the lifespan of branches and frequency of commits. In trunk-based development, all changes are integrated frequently into the main branch, thereby minimizing the scope of conflicts and simplifying the merging process.

GitFlow: A Brief

Source: Stackoverflow

GitFlow, another popular Git workflow, is more structured and involves multiple types of branches, each serving a specific purpose. Unlike trunk-based development, where all changes are integrated frequently into the main branch, GitFlow allows long-lived feature branches, which are merged back only once the feature is complete.

While GitFlow offers more structure, it may lead to complex merges and conflicts if features are large or long-lived. Trunk-based development, on the other hand, advocates for smaller, more frequent merges to avoid such issues.

The Best Use Cases for Trunk-based Development

Trunk-based development is best suited for teams that aim for rapid and frequent delivery. It is particularly beneficial for medium to large-sized teams where synchronization of changes can become a challenge. The practice shines in environments that value speed, efficiency, and minimal blockers in the development pipeline.

Projects requiring fast-paced delivery and frequent releases, like those employing CI/CD practices, can greatly benefit from trunk-based development. This model ensures that the main codebase is always in a releasable state, thereby enabling continuous deployment.

Agile development prioritizes adaptability and swift delivery. In this light, trunk-based development aligns well with Agile principles. It supports the Agile tenet of frequent, incremental delivery, as developers continually integrate their changes into the main branch, keeping it in a shippable state.

Consider the example of a tech company that shifted from GitFlow to trunk-based development to better fit their Agile process. The team managed to reduce their deployment time significantly as all changes were integrated regularly into the main branch, reducing the complexity and time spent on resolving merge conflicts.

Source: Unleash

Benefits

The rapid advancements in technology call for development methodologies that support quick and seamless software delivery. This is where trunk-based development can provide substantial benefits:

  1. Minimizing Merge Conflicts: In a trunk-based development approach, developers are encouraged to commit and merge their changes frequently. This frequent integration of changes means the difference between the main codebase and the feature branch is minimal. Consequently, the likelihood of encountering merge conflicts is significantly reduced. This benefit is particularly notable in large teams where multiple developers might be working on different features simultaneously.
  2. Maintaining a Releasable Codebase: In trunk-based development, maintaining the trunk or main branch in a releasable state is a key principle. This means, at any given point, the codebase should be ready for deployment. This aligns well with Continuous Integration/Continuous Deployment (CI/CD) practices where the code is frequently integrated, tested, and deployed. Trunk-based development enables teams to move faster towards their delivery goals.
  3. Promoting Collaboration and Communication: Trunk-based development encourages all developers to work on the same branch. This promotes a shared understanding of the codebase and fosters better communication and collaboration among team members. It can help in avoiding the silo effect that can occur when developers work on long-lived feature branches in isolation.
  4. Reducing Code Drift: In other development models where feature branches can be long-lived, there’s a risk of ‘code drift’. This happens when the main codebase and the feature branch diverge significantly, making it harder to merge back the changes. With trunk-based development, the frequent merging helps in keeping the codebase aligned and reduces the risk of code drift.
  5. Simplifying the Development Process: Trunk-based development simplifies the development process by reducing the complexity of managing multiple long-lived branches. There’s no need to regularly rebase or merge from the main branch to keep the feature branch updated. Also, since everyone works off the main branch, it eliminates the need to decide which branch to base the new feature branch on.

However, it’s important to remember that trunk-based development also comes with its own challenges:

  1. Requires Discipline and Rigorous Testing: Given that developers are merging their changes frequently into the main branch, it requires a high level of discipline to ensure that these changes do not break the build. It also necessitates more rigorous and frequent testing practices to maintain the trunk in a releasable state.
  2. Not Ideal for All Scenarios: While trunk-based development can streamline the development process, it may not be ideal for all scenarios. For example, for complex projects with large features that take longer to develop and cannot be broken down into smaller, incremental changes, working directly on the main branch might not be feasible.

Despite these challenges, many development teams find that the benefits of trunk-based development, particularly its synergy with Agile and CI/CD practices, outweigh the drawbacks. It’s a tool that, when implemented correctly, can lead to more efficient development workflows and faster delivery times.

Unit Testing in Trunk-Based Development

Unit testing is a critical component of any software development methodology, and trunk-based development is no exception. In this approach, developers are encouraged to commit changes frequently to the main branch, maintaining its constant readiness for production deployment. This makes it vital to ensure that every commit is of high quality and does not break the existing functionality. Here is where unit testing comes into play.

Source: Katalon

A unit test targets the smallest testable part of an application — typically a function or method within a class. It helps verify that each individual component of the software works as expected. In the context of trunk-based development, each commit that a developer makes would ideally have associated unit tests to verify the functionality of the changed or newly added code.

Unit tests help catch issues at an early stage, before the code is merged into the main branch. They help maintain the integrity of the main branch, ensuring that it remains in a deployable state, thereby promoting Continuous Integration/Continuous Deployment (CI/CD) practices.

Why is Unit Testing Critical in Trunk-Based Development?

  1. Preserving Code Quality: As developers are working directly on the main branch or short-lived feature branches, there’s a greater responsibility to ensure the code committed is of high quality. Unit tests provide a way to validate the functionality of individual components, helping to maintain the overall quality of the codebase.
  2. Preventing Regression: Frequent merges in trunk-based development increase the risk of regression — where new changes unintentionally affect existing functionalities. Unit tests can detect these issues early, preventing problematic code from being merged into the trunk.
  3. Facilitating Continuous Integration: In trunk-based development, continuous integration is key. Unit testing facilitates this by providing regular feedback on the functionality and health of the codebase. Any failure in a unit test should be considered a failure in the integration process, providing an opportunity for immediate correction.
  4. Promoting Fearless Refactoring: Unit tests can act as a safety net, allowing developers to refactor code with confidence. If a piece of refactored code breaks an existing unit test, it signals that the change has affected the software’s behavior. Thus, developers can refactor more freely, knowing that they will be alerted to any unexpected behavior changes.
  5. Supporting Agile Practices: Trunk-based development aligns well with Agile methodologies, which prioritize frequent, incremental delivery. Unit tests are a core component of Agile practices, supporting the iterative development and continuous feedback loop that Agile encourages.

In conclusion, unit testing is not just beneficial but critical in trunk-based development. It ensures the main branch’s stability, supports continuous integration, and encourages high code quality, making it an indispensable tool in a trunk-based development environment.

About 8grams

We are a small DevOps Consulting Firm that has a mission to empower businesses with modern DevOps practices and technologies, enabling them to achieve digital transformation, improve efficiency, and drive growth.

Ready to transform your IT Operations and Software Development processes? Let’s join forces and create innovative solutions that drive your business forward.

--

--

8grams

We are a DevOps Consulting Firm with a mission to empower businesses with modern DevOps practices and technologies