Continuous deployment — this is a software engineering practice that relies on continuously delivering each change to the production environment automatically. Such changes must be preceded by a series of automated tests that allow us to ensure that newly integrated code doesn’t have any bugs (CD assumes you trust those tests).
You may wonder, what is all of this for… Generally, removing manual work is considered the way you should follow, because this is a continuous and repeatable process. People tend to make mistakes in such processes, which drives us to bugs during deployments (I have seen many times the prod environment on fire, like in hell).
Sometimes we would like to say that it is better to make deployments less frequently, cuz they are sensitive, and we could introduce many bugs during them. We need to be careful — the less often, the better. This way users will find fewer bugs. Even better would be to deploy our apps at midnight: less traffic, and maybe even block access to our app, so the users would not disturb us… Right? Actually, no. Life likes to be surprising here, too.
A bit of my story: I was working in both approaches - rarely at midnight and during the workday often (daily deployments).
What did I find? It was a big surprise for me - the more often deployments, the better. Bugs appear very rarely (at the time of writing this article, I can’t remember a production fire after deployment). We could ask why… the answer is simpler than you may think. Actually, you deploy a smaller scope than with rarely deployed code that accumulates. I found that the developer is able to keep in mind all changes he is deploying - when comparing 20 files against 1000+ files.
What is a branching strategy?
This is something that everyone uses, and doesn’t really know that it is the name of it ;) Putting it in plain language: this is a way you create and merge your branches in your repository. There are many strategies; some of them fit more or less into your environment.
What do I have in mind by telling the environment? Some strategies may be a better fit for different platforms. For example, for mobile apps there may be a better fit for GitFlow than Trunk-Based Development (TBD). For microservice apps there may be a better fit for TBD than GitFlow. For game-dev there may be better… Well, I don’t have experience in this field, so I don’t have an opinion. To be fair, I have some, but not enough.
So what are branching strategies for? This is to define how the team will control its changes in the version control system.
Trunk-Based Development (TBD)
I found this to be well fitted in a web services-based team from my experience. How does it work?
Take a note: I’m usually talking about the master branch, but I have in mind your main branch — trunk/main/master, it doesn’t matter what you call it.
There are a few versions of integrating:
- You commit directly to the
master, usually you use pair programming - You create short-lived feature branches and merge them to the
masterthroughPR(usually such a branch lives one day, max 2)
And a few versions of delivering:
- You deploy the
masterbranch - You deploy your
release/*branch that you created frommaster(this is a variant, standardTBDassumes releases frommaster).
Principles of TBD say that after integration (PR, commit), there should be deployment with those changes to production. It requires the code to have automated tests. I also found feature flags well fitted, so I can hide the feature that is not finished yet. Although it’s not possible to create a whole feature in one PR or commit.
If the team wasn’t working earlier with TBD but in strategies like GitFlow, then we have to change the mindset of our team; if we want to switch to that, it may take a while. TBD requires maturity from the team - daily deployments are something different from what our mind tells us (and pressure from the business telling us to stop making bugs on production).
GitFlow
GitFlow is a strategy that says to deploy from master, but: before there will be the code in this branch, we have to create this feature on the develop branch earlier.
When develop has all the functionalities we want to have, then we create from it a release branch, which will be under regression tests perhaps. After tests, such a branch is merged into master, which we deploy to production later.
Such a complicated flow.
Once I was on a GitFlow team we made some variation of this, we resigned from the develop branch in favor of release branches (we were just developing features on branches with a given version). Why? Developers were missing when switching branches as develop became a release branch, and they didn’t realize what resulted from making changes on develop.
This strategy is not a great fit for web services in my experience, but it looks like a good fit for mobile apps where more frequent deploying is hard - applications must pass review in the Apple Store or Google Play before they will be available for end users. Or do you have a different experience?
What does Continuous Deployment require from our branching strategy?
Continuous Deployment is a bit fussy with requirements.
A production branch
CD requires a single point of production deploy from the branching strategy. If we always have code on production from release/* branches, then pipelines would never know which branch to deploy after merging a PR.
Assuming that the change is merged into the master branch, then we have to deploy this change to production, but then we don’t know how to move this change to the release branch.
Technically, pipelines could trigger creation of release branches from master after a PR and start to deploy them. But what if we need to cherry-pick? What in case of conflicts? It would require manual work. This is no way for CD and is even overcomplicated.
So there is a requirement of a single point of deploying (a branch) to production, so the pipeline knows where to start.
Frequent integration
CD requires the code to be integrated with the main branch frequently. By definition, code that lived a very long time on a feature branch is problematic for integration. The longer the time, the bigger the drift against the target branch. Sometimes it is even hard to remember what the scope of the change was!
In big projects there are many changes in the main branch. Assuming that you wait with your changes, e.g. 2 weeks, then during this time others complete their PRs, which causes conflicts with the target branch on your side and manual merging - additional work. Code may be 100% perfect, tests are green, but after merge with main we may find that they become red or even the build is not possible. Sometimes it is even hard to find the problematic code. If PRs are frequent, then eventual merge conflicts are easier to resolve.
Master branch has to be always ready to go
CD requires the master branch to be ready to go to production. What does it imply? Each change has to be tested automatically before it is merged to master. This is needed to ensure that there is no regression. So we need such things:
- Unit tests
- Integration tests
feature flags- Contract tests (backward compatibility)
- Quick rollback/revert option
- Small changes
Avoid long-lived feature branches
Creating a feature branch for the whole feature or even bigger scope (like in GitFlow) is problematic, cuz it creates big changes that we can’t remember all of during deployment. What’s more, we extend the path that CD is telling us to make shorter. This would become unnatural.
Let’s take GitFlow into account. Imagine features A, B, C. A and B are finished and ready for deployment, but C is half-finished or has a regression. How can the pipeline know whether C is ready to go or not? Or how would the pipeline take A and B changes without C?
Setting that aside, CD wants:
PR/Commit feature A -> test ->proddeploymentPR/Commit feature B -> test ->proddeploymentPR/Commit feature C -> test ->proddeployment
Is Continuous Deployment possible with any branching strategy?
As you could already see in previous sections and examples, not every branching strategy is well fitted for CD. When we look at GitFlow and how it juggles branches, it is not easy to accomplish CD.
In GitFlow, the problem is that it prefers batch feature branches, so we don’t have frequent integrations. Sometimes teams may even skip feature flags with GitFlow because they don’t need them; those flags are helpful to avoid showing the end user a half-finished feature (Martin Fowler on feature toggles).
So we arrive at the moment in GitFlow where we need to merge our release branch into master, and yes, there are often lots of changes. All this causes lots of conflicts in merging these two branches, which makes automatic merge impossible, if we would ever want that.
Thus, I don’t see good fit between GitFlow and CD (especially in distributed systems). Even if we could make such an automatic process, I don’t see the justification.
Why I found Trunk-Based Development a better fit
In my opinion TBD naturally fits CD. It has all the principles required by it. TBD looks like it was created to be used with this together. In this branching strategy we could find rules like:
- Short-lived feature branches
- Frequent merging
- Single point of deployment to production
- Main branch is always ready to go
- Feature branches are merged directly to the
mainbranch
Adding to it:
feature flags(they are naturally needed anyway)- Automatic tests
- Small changes (nowadays with AI, there may be a short-lived feature branch that is big, which is also bad in my opinion)
So we have a very good stack to start CD. I don’t say that it is everything that we need, there may be other stuff needed because of specific requirements of the project, but this is a good starting point for CD.
TL;DR
CD is a practice that relies on continuously delivering small changes to production, after each merge, automatically. Such changes should be validated all the time with automated tests.
There are lots of branching strategies, but not all of them fit the principles of CD, some of them took very different paths that do not fit with CD.
CD has specific requirements for the branching strategy, such as:
- Main branch always releasable
- Short-lived feature branches (or direct commits to the
master) - Frequent merging
- Single point of releasing to production, usually the
master/main/trunkbranch feature flags(helpful but not required)- Small changes
- Automated tests on many levels (unit, integration, etc.)
TBD looks like an ideal fit that can go with CD, because in its assumptions it has many principles that are required by CD.
Different branching strategies such as GitFlow look like they are going in a totally different direction than CD requires. It means that making CD with it may be extremely hard.
Comments
Join the conversation using your GitHub account. You can also browse the discussions on GitHub.