Shades of Trunk Based Development
Join the DZone community and get the full member experience.Join For Free
i’m going to try to explain the alternatives for trunk based development (tbd) in terms of branch layout. there’s few different styles, that different types of development organization might try to implement.
make branches for a release (just in time)
all developers work on the trunk (theirs are the blue commits). just before a release, a branch is cut for that release. that’s probably a release engineer, and you might change permissions for that branch so that the larger group of developers cannot commit to it even if they can see the branch. the first commits on that branch, by the release engineers, might be to make it ready for release. the release is build from there. most likely that’s by the same ci technology you were already using (say jenkins) and it pushes a binary into an repository for ultimate certification and release (also for posterity).
a defect in production
the defect is found after release. indeed, the trunk (which had no code-freeze whatsoever) has already moved past release. the organization in question does not want to release a new version of the product with the latest commits on trunk, so it has to release something from the pre-existing release branch.
the development does not sweat that the trunk is more advanced than the release. they give it a go trying to fix bug in the trunk (the red commit). there’s a fair chance that if they can reproduce it, fix it, and get qa agreement that it’s fixed on the trunk, then a cherry-pick (of the whole commit) to the release branch will merge just fine (the green commit). if it doesn’t then, some merge resolution might be needed. the remediation of the bug is again agreed by qa, and a point release goes out from the release branch.
fix on trunk, and cherry-pick to the release branch is the way we guarantee that regressions (bad) don’t happen. regressions (bad) should not be confused with regression testing (good) of course.
release from trunk, make a branch to support a release
though the trunk has moved past the commit that’s intended to go live, the release engineers are going to make a tag for that commit, build a binary, and push it towards release. maybe jenkins had already made that binary, and it’s certification over a few days is just fine. the developers, again, did not experience a code-freeze.
a defect in production
long after the event, a branch is created from the tag. it’s as if the branch were created in the style above of course. everything after the branch creation is as before.
release from trunk, roll forward
this organization is willing to put code live ahead of schedule, in the event of a defect. they probably have enough feature-toggles and build switches to make sure that features do not go live ahead of schedule. there is no release branch, even in the event of a defect. even though the team is not continuously deploying, the “next release” is always going to be from trunk. even for bug-fixes, which don’t need to be merged anywhere.
release from master, bug fixed on temporary task branch
this is more the style of the pull-request systems that has been popularized by dvcs source-control systems like git and mercurial, whereas those above are more the preserve of subversion and perforce.
developers in the team work on lightweight forks of the master (trunk), and submit their pull request when their task is finished. in this case, we’re modeling a bug-fix (#1), so the developer may deliberately reset the working copy to the same commit as that of the defect (#2) and make a lightweight local branch. going back to the last commit that made the release is optional, as the outcomes can be the same. the defect is fixed, the developer brings themselves up to date (#3) merging if they have to, then issue the pull request as they publish the temporary task branch. they or someone else merges it into the master (trunk), maybe after a code review, and delete the transient work branch. deletion of the task branch in a dvcs system is as if the branch had never been created (#4) – all incremental history is kept by default and the fact that a branch was created for the work is irrelevant. the release goes out from there as usual. this is also a roll-forward way of doing things.
the communities in question don’t conventionally refer to this model as trunk based development. perhaps because of “master” rather than “trunk” being the main branch. perhaps because linus had a you just don’t get it speech at google in 2007 re git versus their tbd way of working. perhaps they’d be happier calling the model master based development, or master rebased development, but neither name precludes multi-person non-transient branches that tbd is against, anddvcs embraces via many published workflows.
fixing defects on the release branch
i really don’t recommend this, as theres a risk of regression (bad). developers fix the defect on the release branch and merge it back to the trunk. what if they forget though? if the team cuts another branch in the future, that unmerged prior fix now reappears as a regression, and the team gets embarrassed on some large scale. maybe someone loses a job. of course, you could guard against unmerged code systematically, but that’s harder to do when some changes should not be merged back to the trunk (things to do with the release itself). there are ways of merging that don’t bring changes with them, but otherwise mark the merges as done.
branch models with more branches represented
more branches that i’m showing above? those models are not trunk based development. they may be branching models that feature a branch called “trunk”, but they are not tbd. clearcase popularized a “mainline” model, where many developers worked on each branch. those models are also possible with any of the more modern choices (svn, p4, tfs, mercurial, git), but are not recommended. with subversion in particular, its “mergeinfo” properties design historically caused issues with reintegrations into the trunk. indeed for the non-dvcs tools bringing back a merge on a branch, obscures the original commit enough for modern dvcs proponents to be quite vocal in their objections.
java project using maven specifically
use of maven in a java project gives rise to some interesting problems. maven projects have a pom.xml file in their root. that drives the additional choices you have to make given it contains the version number of the buildable/deployable thing within it. in a release cycle you might want to change it to
. maven has a build in “release” command, that will make the changes to pom.xml in-situ on the trunk. the command even does incremental commits. the trouble is that it kinda needs a code-freeze, which tbd doesn’t require generally. this might drive an organization to using the “branch just before release” model, as it creates a naturally code-frozen place, that doesn’t change developer use of the trunk at all. maven gets a lot of bad press, but many organizations use it well.
Published at DZone with permission of Paul Hammant, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Designing a New Framework for Ephemeral Resources
What Is Retesting?
Building Resilient Systems With Chaos Engineering