1. Overview
1.概述
In this tutorial, we’ll learn about the trunk-based development approach in Version Control Systems (VCS).
在本教程中,我们将学习版本控制系统 (VCS) 中基于主干的开发方法。
First, we’ll explore the general concept, see how it’s different from the feature branch development model, the common characteristics involved, and the workflow to achieve it.
首先,我们将探讨其总体概念,了解它与功能分支开发模式的不同之处、所涉及的共同特征以及实现它的工作流程。
Then, we’ll discuss the different factors to consider while going for trunk-based development. Finally, we’ll see the pros and cons of using trunk-based development.
然后,我们将讨论在进行基于主干的开发时需要考虑的不同因素。最后,我们将了解使用基于主干的开发的利弊。
2. What Is Trunk-Based Development?
2.什么是基于主干的开发?
Trunk-based development is a source-control branching model, where the developers work on a single branch called ‘trunk’ or ‘mainline’ (‘master’ or ‘main’ in Git).
基于主干的开发是一种源代码控制分支模型,开发人员在称为 “主干 “或 “主线”(Git 中为 “master “或 “main”)的单一分支上工作。
Since there’s no long-lived development branch in this model, merge conflicts are generally avoided, and the builds aren’t broken frequently.
由于在这种模式下没有长期存在的开发分支,因此通常可以避免合并冲突,也不会经常出现构建失败。
In the trunk-based development approach, the developers mainly collaborate on the ‘trunk’. However, larger teams may use short-lived branches that can last no more than a couple of days.
在基于主干的开发方法中,开发人员主要在 “主干 “上协作。不过,规模较大的团队可能会使用持续时间不超过几天的分支。
Developers commit the code very often to push the latest copy in the trunk. Subsequently, the builds trigger automated tests, which help to minimize the integration issues at a later stage.
开发人员经常提交代码,以便在主干中推送最新的副本。随后,构建会触发自动测试,这有助于最大限度地减少后期的集成问题。
As we can see, this approach encourages continuous integration and works best for small agile teams.
我们可以看到,这种方法鼓励持续集成,最适合小型敏捷团队。
3. Comparison With Feature Branch Model
3.与特征分支模型的比较
Feature branch-based development is the most common approach the teams chose to practice. In this decentralized version control approach, the developers create long-lived dedicated branches from the main codebase to isolate the changes needed for a particular feature/improvement.
在这种分散式版本控制方法中,开发人员从主代码库中创建长期存在的专用分支,以隔离特定功能/改进所需的更改。
Once the code is ready and tested, the commit will be made to the respective branch. Finally, these branches will be merged back to the main code base after resolving the merge conflicts.
一旦代码准备就绪并经过测试,就会提交到相应的分支。最后,这些分支将在解决合并冲突后合并回主代码库。
Here, the idea is to enable developers to work independently and commit the changes without affecting other developers or teams. Specifically, this can be useful in relatively large projects with multiple contributors and/or complex codebases.
这里的想法是让开发人员能够独立工作,并在不影响其他开发人员或团队的情况下提交更改。具体来说,这在有多个贡献者和/或复杂代码库的相对大型项目中非常有用。
4. Characteristics of Trunk-based Development
4.主干式发展的特点
The trunk-based development approach has some common characteristics. These factors influence how effectively the developers work and collaborate between themselves. Let’s see them in brief.
基于主干的开发方法有一些共同特点。这些因素会影响开发人员之间的工作和协作效率。让我们来简单了解一下。
4.1. Continuous Integration
4.1.持续集成
In trunk-based development, all the developers work on a single branch (master/trunk). This enables faster code integrations, thereby reducing merge conflicts.
在基于主干的开发中,所有开发人员都在一个分支(主干/主干)上工作。这样可以加快代码整合,从而减少合并冲突。
The developers integrate the code changes as frequently as they can instead of waiting for a large chunk of new code.
开发人员尽可能频繁地整合代码变更,而不是等待一大段新代码。
4.2. Small, Frequent Commits
4.2.小规模频繁提交
Integrating small but frequent commits regularly avoids the risk of doing a big merge at a later stage. Subsequently, the merges will be much easier with relatively fewer conflicts.
定期整合小而频繁的提交,可以避免在后期进行大合并的风险。
Also, as the commits are more regular, the chance of introducing bugs is minimal. Moreover, it’s much easier to identify and resolve merge conflicts.
此外,由于提交更有规律,引入错误的几率也会降到最低。此外,发现和解决合并冲突也更容易。
4.3. Test Automation
4.3.测试自动化
Test automation is an essential feature in the trunk-based development model. Since there are regular commits, we need to make sure that the builds aren’t broken. In addition, the new commits shouldn’t introduce regressions.
测试自动化是基于主干的开发模式中的一项基本功能。由于存在定期提交,我们需要确保构建过程不会被破坏。此外,新提交不应引入回归。
Increasing the test coverage helps to reduce the risk of regression and helps in fixing the identified issues quickly.
提高测试覆盖率有助于降低回归风险,并有助于快速修复已发现的问题。
Moreover, these automated tests provide instant feedback so that the issues are resolved faster, which results in improved code quality.
此外,这些自动化测试还能提供即时反馈,以便更快地解决问题,从而提高代码质量。
4.4. Code Review
4.4.代码审查
For each new commit, other developers review and provide feedback before merging the code to the main branch. This helps to improve the code quality and identify potential issues in advance. The timeframe to complete the code review needs to be defined so that it doesn’t delay the commits.
对于每个新提交的代码,其他开发人员都会在将其合并到主分支之前进行审核并提供反馈。这有助于提高代码质量并提前发现潜在问题。需要确定完成代码审查的时间框架,以免延误提交。
4.5. No Long-Lived Branches
4.5.无长期分支机构
Any branch that is longer than two days may end up becoming a long-lived branch. In trunk-based development, long-lived branches are avoided to prevent merge conflicts and allow faster integration.
任何超过两天的分支最终都可能成为长寿命分支。在基于主干的开发中,为了防止合并冲突并加快集成速度,应避免使用长寿命分支。
5. The Workflow
5.工作流程
Let’s see the major steps involved in the trunk-based development model.
让我们来看看基于主干的开发模式所涉及的主要步骤。
5.1. Code Change
5.1.代码变更
In the first step, once the code is ready, the developer commits the code directly to the trunk or a short-lived branch that was created from the trunk. Here, the rule of thumb is that everyone commits to the mainline every day.
第一步,一旦代码准备就绪,开发人员就会直接将代码提交到主干或从主干创建的短期分支。在这里,经验法则是每个人每天都提交到主干。
More precisely, we should never have more than a day’s work sitting unintegrated in the local repository.
更确切地说,我们不应该把一天以上的工作放在本地存储库中。
5.2. Pull Request
5.2.拉动请求
As soon as the code is committed, the developer creates a pull request to merge the new code with the main branch.
一旦提交了代码,开发人员就会创建一个拉取请求,将新代码与主分支合并。
The pull request would contain the basic description of the change, the list of code changes, and any other relevant information.
拉取请求将包含更改的基本描述、代码更改列表以及任何其他相关信息。
5.3. Code Review
5.3.代码审查
In this step, other developers review the code changes from the pull request. This step helps to identify the defects which could have escaped from the eyes of the original author.
在此步骤中,其他开发人员会审查拉取请求中的代码变更。这一步有助于找出原作者可能忽略的缺陷。
Further, having this 4-eyes validation for the new code enforces the best practices for new developers.
此外,对新代码进行四眼验证还能为新开发人员提供最佳实践。
5.4. Automated Testing
5.4.自动测试
Trunk-based development relies heavily on automated tests. These tests include unit and integration tests that are triggered automatically when a pull request is created.
基于主干的开发在很大程度上依赖于自动化测试。这些测试包括单元测试和集成测试,在创建拉取请求时自动触发。
Automated tests prevent the new code changes from breaking the existing functionalities. More importantly, it gives the confidence that the new code works fine without affecting the old code.
自动测试可防止新代码更改破坏现有功能。更重要的是,它让人确信,新代码在不影响旧代码的情况下也能正常工作。
5.5. Merging
5.5.合并
After passing through the peer review and automated tests, the new code is ready for merge with the trunk. Since there’s no long-lived branch and the commit interval is short, we minimize the risk of merge conflicts.
通过同行评审和自动测试后,新代码就可以与主干合并了。由于没有长期存在的分支,提交间隔也很短,因此我们最大限度地降低了合并冲突的风险。
6. Factors Influencing Trunk-based Development
6.影响主干式发展的因素
Multiple factors have influence when choosing between trunk-based development and feature branch development.
在选择基于主干的开发还是基于功能分支的开发时,会受到多种因素的影响。
Specifically, we should take care of the specific needs of the project and the development team involved. Both these development approaches have their advantages and disadvantages. However, the primary factors to consider include the complexity of the codebase, the actual development process, and the number of developers.
具体来说,我们应该考虑到项目和相关开发团队的具体需求。这两种开发方法各有利弊。但是,需要考虑的主要因素包括代码库的复杂性、实际开发流程和开发人员的数量。
Let’s see what are the major pros and cons of trunk-based development.
让我们看看基于主干的开发有哪些主要优缺点。
6.1. Pros
6.1.
- Speed and efficiency: As the team works on a single branch, Trunk-based development enables faster code integrations due to frequent check-ins, which results in fewer merge conflicts. As a result, the team can enjoy the speed and efficiency this model provides.
- Greater code stability: Frequent check-ins also help to detect and fix any code issues. As there’s no long-lived branch, we avoid the risk of bigger mergers. In other words, this results in greater code stability as we identify the issues earlier and fix them.
- Enhanced team collaboration: In contrast with the branch model, where the developers work on individual branches, trunk-based development fosters good understanding and better collaboration between themselves by providing more awareness of all the current changes.
- Continuous integration and delivery (CI/CD): By way of frequent commits, developers make sure that the code in the trunk is always the latest. While doing so, they make sure that the builds always pass, and in case of any failures, it’s addressed instantly. Subsequently, the code in the trunk will always be in a deployable state.
- Reduced technical debt: While working with long-lived branches, there are chances that the developers may go for quick fixes, which may result in new technical debt. In trunk-based development, with the help of frequent merges and smaller commits, we avoid big merges to reduce technical debt.
6.2. Cons
6.2.同意
- Risk of breaking the build: Since the developers commit more often, there’s always a chance of breaking the build. When the build fails, we may need more time to fix it and make it pass again.
- Change isolation: There’s a risk of mixing two or more changes without testing those individual changes separately. The developer needs to be ready, always knowing the current codebase may get updated anytime.
- Release management: It’s difficult to maintain different codebases in the trunk. Thus, management of releases may become a headache if we need an intermediate release in between.
- Number of developers: If the developers are more in number, working on the trunk may be more challenging. The team collaboration may be affected if the process isn’t clear.
7. Conclusion
7.结论
In this article, we had an overview of the usage of trunk-based development in modern software development.
本文概述了基于主干的开发在现代软件开发中的应用。
Also, we discussed briefly the characteristics and workflow of the trunk-based development model. While on the subject, we also compared the differences between branch-based and trunk-based development.
此外,我们还简要讨论了基于主干的开发模式的特点和工作流程。同时,我们还比较了基于分支的开发和基于主干的开发之间的差异。
Finally, we saw the benefits and drawbacks of using the trunk-based development. To conclude, trunk-based development is worth considering for teams that want to streamline their development process, improve team collaboration, and reduce technical debt.
最后,我们了解了使用基于主干的开发的优点和缺点。最后,对于希望简化开发流程、改善团队协作和减少技术债务的团队来说,基于主干的开发值得考虑。