Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Trunk based development

4,029 views

Published on

Are you sick of Merge Hell? Do your feature branches go rogue? Do you spend more time fiddling with your Version Control System than doing actual development work? Then Trunk Based Development might be for you. Facebook does it. Google does it. Instead of messing with multiple branches, just use your master branch. Always. In addition to giving you an overview about how Trunk Based Development works, where it shines and where the pitfalls are, this talk will also cover the necessary techniques to succeed with it, such as Branch By abstraction, Feature Toggles and backwards compatible Database Migrations.

Published in: Technology

Trunk based development

  1. 1. Trunk Based DevelopmentTrunk Based Development Dutch PHP ConferenceDutch PHP Conference 2014-06-282014-06-28
  2. 2. Gordon OheimGordon Oheim Senior Craftsman, PHP Documentor,Senior Craftsman, PHP Documentor, Stack Overflow Contributor & Moderator,Stack Overflow Contributor & Moderator, AgilistAgilist →→ http://twitter.com/go_ohhttp://twitter.com/go_oh →→ http://about.me/goohhttp://about.me/gooh →→ gooh@php.netgooh@php.net
  3. 3. Before we start …Before we start …
  4. 4. x
  5. 5. Six months ago …Six months ago …
  6. 6. AdvantagesAdvantages ● Easy to understand when used properlyEasy to understand when used properly ● Branches document Sprints/FeaturesBranches document Sprints/Features ● Isolated development cannot break masterIsolated development cannot break master branchbranch
  7. 7. ProblemsProblems ● assumes Sprints end as plannedassumes Sprints end as planned ● rogue branches might be impossible to mergerogue branches might be impossible to merge ● requires more communication about what wasrequires more communication about what was merged yetmerged yet ● merge conflicts are postponedmerge conflicts are postponed ● merge conflicts are more expensivemerge conflicts are more expensive
  8. 8. Welcome to Merge HellWelcome to Merge Hell
  9. 9. Trunk Based Development to the rescue Image: https://commons.wikimedia.org/wiki/File:Placeholder_male_superhero_c.png (CC SA 3.0)
  10. 10. Trunk Based DevelopmentTrunk Based Development ● everyone commits to master at least once per dayeveryone commits to master at least once per day ● remote branches are only made for releasesremote branches are only made for releases ● developers may use local branchesdevelopers may use local branches ● hotfixes are also committed to masterhotfixes are also committed to master ● hotfixes are cherry picked into supported releaseshotfixes are cherry picked into supported releases ● only Release Managers may branch releaseonly Release Managers may branch release branchesbranches
  11. 11. WorkflowWorkflow
  12. 12. AdvantagesAdvantages ● leanlean ● can release at any timecan release at any time ● merge problems surface earlymerge problems surface early ● merge problems are smallermerge problems are smaller ● release branches are cheaprelease branches are cheap
  13. 13. ProblemsProblems ● Branches no longer document featuresBranches no longer document features ● Needs a way to hide/disable unfinished workNeeds a way to hide/disable unfinished work ● Requires additional care with db changesRequires additional care with db changes ● Requires OOD knowledgeRequires OOD knowledge
  14. 14. Branch by AbstractionBranch by Abstraction "Branch by Abstraction" is a technique for making a large-"Branch by Abstraction" is a technique for making a large- scale change to a software system in gradual way thatscale change to a software system in gradual way that allows you to release the system regularly while theallows you to release the system regularly while the change is still in-progress.change is still in-progress. - Martin Fowler- Martin Fowler
  15. 15. In a nutshellIn a nutshell ● modularity through design not through VCSmodularity through design not through VCS ● add an abstraction over any code you are going to change in aadd an abstraction over any code you are going to change in a featurefeature ● new features are added through Adapters and Interfacesnew features are added through Adapters and Interfaces ● production code keeps old implementations while feature isproduction code keeps old implementations while feature is incompleteincomplete ● development code uses the new implementationsdevelopment code uses the new implementations ● when the feature is ready, the new implementations arewhen the feature is ready, the new implementations are rolled outrolled out
  16. 16. Feature TogglesFeature Toggles ● features are hidden until readyfeatures are hidden until ready ● hides features at the entry pointshides features at the entry points ● easy to configure via config fileeasy to configure via config file ● easy to implement with a small POPOeasy to implement with a small POPO ● can be tailored to specific user groupscan be tailored to specific user groups ● bonus:bonus: allows for easy A/B testingallows for easy A/B testing
  17. 17. Feature Toggles at MVSFeature Toggles at MVS
  18. 18. Database ChangesDatabase Changes ● all changes to the database need to beall changes to the database need to be backwards compatiblebackwards compatible ● one statement per migrationone statement per migration ● migrations should be transactionalmigrations should be transactional ● needs to be integrated into the build processneeds to be integrated into the build process (DBDeploy, Liquibase, …)(DBDeploy, Liquibase, …)
  19. 19. Any Questions so far?Any Questions so far?
  20. 20. Example 1 - Migrate CodeExample 1 - Migrate Code Objective: replace a legacy component with aObjective: replace a legacy component with a newer componentnewer component
  21. 21. Current CodeCurrent Code
  22. 22. Step One: Decouple theStep One: Decouple the implementationimplementation
  23. 23. ProTipProTip ● In PHPStorm you can right-click class code andIn PHPStorm you can right-click class code and select Refactor Extract Interface→ →select Refactor Extract Interface→ → ● If your IDE doesn't support Extract Interface,If your IDE doesn't support Extract Interface, try https://github.com/gooh/InterfaceDistillertry https://github.com/gooh/InterfaceDistiller
  24. 24. Step Two: Change all consumersStep Two: Change all consumers to use the Interfaceto use the Interface
  25. 25. Step Three: Add feature toggleStep Three: Add feature toggle
  26. 26. Step Four: Add new componentStep Four: Add new component
  27. 27. Step Five: CleanupStep Five: Cleanup ● remove the toggle from bootstrapremove the toggle from bootstrap ● optional: remove the interface againoptional: remove the interface again ● Done o/Done o/
  28. 28. Example 2: Change APIExample 2: Change API Objective: adapt a new API graduallyObjective: adapt a new API gradually
  29. 29. Change API - SetupChange API - Setup
  30. 30. Change API: isolate the old APIChange API: isolate the old API
  31. 31. Change API: fading outChange API: fading out
  32. 32. Change API: cleanupChange API: cleanup ● when ready, remove the old API codewhen ready, remove the old API code ● run your tests to make sure it worksrun your tests to make sure it works ● remove the Migration Adapter from bootstrapremove the Migration Adapter from bootstrap ● inject the component with the new API onlyinject the component with the new API only ● done o/done o/
  33. 33. Bonus: Using a VerifyBonus: Using a Verify Objective: harden against failuresObjective: harden against failures
  34. 34. In a nutshellIn a nutshell ● Verify adds additional safety to the migrationVerify adds additional safety to the migration ● fail fast and tell you where at runtimefail fast and tell you where at runtime ● is basically a Runtime Assertionis basically a Runtime Assertion ● isis NOTNOT a replacement for your test QAa replacement for your test QA
  35. 35. Verify: ImplementationVerify: Implementation
  36. 36. Questions?Questions? Last Chance!Last Chance!
  37. 37. ReferencesReferences ● http://paulhammant.com/2013/04/05/what-is-trunk-based-developmenthttp://paulhammant.com/2013/04/05/what-is-trunk-based-development ● http://paulhammant.com/blog/branch_by_abstraction.htmlhttp://paulhammant.com/blog/branch_by_abstraction.html ● http://paulhammant.com/2011/05/13/avoid-big-bang-for-branch-by-abstraction/http://paulhammant.com/2011/05/13/avoid-big-bang-for-branch-by-abstraction/ ● http://continuousdelivery.com/2011/05/make-large-scale-changes-incrementally-with-http://continuousdelivery.com/2011/05/make-large-scale-changes-incrementally-with- branch-by-abstraction/branch-by-abstraction/ ● http://c2.com/cgi/wiki?BranchByAbstractionhttp://c2.com/cgi/wiki?BranchByAbstraction ● http://martinfowler.com/bliki/FeatureToggle.htmlhttp://martinfowler.com/bliki/FeatureToggle.html ● http://martinfowler.com/bliki/BranchByAbstraction.htmlhttp://martinfowler.com/bliki/BranchByAbstraction.html ● http://www.stephen-smith.co.uk/application-pattern-verify-branch-by-abstraction/http://www.stephen-smith.co.uk/application-pattern-verify-branch-by-abstraction/ ● http://dev.jimdo.com/2013/05/03/software-migration-strategies/http://dev.jimdo.com/2013/05/03/software-migration-strategies/ ● http://java.dzone.com/articles/application-pattern-verifyhttp://java.dzone.com/articles/application-pattern-verify ● http://www.whitewashing.de/2013/12/05/feature_flags_and_doctrine_entities.htmlhttp://www.whitewashing.de/2013/12/05/feature_flags_and_doctrine_entities.html ● http://abhishek-tiwari.com/post/decoupling-deployment-and-release-feature-toggleshttp://abhishek-tiwari.com/post/decoupling-deployment-and-release-feature-toggles
  38. 38. Thanks!Thanks! https://joind.in/10876https://joind.in/10876

×