Hands-on Demo – Trunk-Based Development for Continuous Delivery with Git branch model
Trunk-Based Development (TBD) is a streamlined Git branching strategy designed for rapid iteration and Continuous Delivery (CD). It emphasizes a single primary branch (main) for integration and encourages small, frequent commits to avoid long-lived branches.
Trunk-Based Development Core Principles
Single Long-Lived Branch:
The
mainbranch is the single source of truth.Always deployable.
Short-Lived Feature Work:
Developers commit directly to
mainor use short-lived feature branches merged quickly.Encourages small, incremental changes.
CI/CD Pipeline:
Every commit triggers automated testing and validation.
Ensures the
mainbranch remains stable.
Scenario: Adding a New Feature Using TBD
1. Initial Setup
Branches:
main: The primary branch for development and deployment.Tools:
CI/CD pipeline for automated testing and deployment.
Feature toggles for deploying incomplete or experimental features.
2. Workflow Steps
Step 1: Pull the Latest main
Before starting new work, pull the latest changes from the main branch:
xxxxxxxxxx21git checkout main2git pull origin mainStep 2: Make Changes
Option 1: Commit Directly to main
Suitable for small, low-risk changes.
Example:
xxxxxxxxxx41echo "<h1>Login Page</h1>" > login.html2git add login.html3git commit -m "Add basic login page structure"4git push origin mainOption 2: Use a Short-Lived Feature Branch
For slightly more complex changes, create a short-lived branch:
xxxxxxxxxx51git checkout -b add-login-page2echo "<h1>Login Page</h1>" > login.html3git add login.html4git commit -m "Add basic login page structure"5git push origin add-login-pageMerge the branch back into main quickly (after review if necessary):
xxxxxxxxxx31git checkout main2git merge add-login-page3git push origin mainStep 3: Automated Testing and Deployment
Every commit to main triggers the CI/CD pipeline to:
Run automated tests (unit, integration, end-to-end).
Validate code quality (e.g., linting, static analysis).
Deploy to staging or production environments.
Example CI/CD Pipeline with GitHub Actions:
xxxxxxxxxx181nameCI/CD Pipeline2on3 push4 branches5main6jobs7 test-and-deploy8 runs-onubuntu-latest9 steps10nameCheckout code11 usesactions/checkout@v312nameInstall dependencies13 runnpm install14nameRun tests15 runnpm test16nameDeploy to production17 ifsuccess()18 run./deploy.shStep 4: Use Feature Toggles for Incomplete Work
If a feature isn’t fully complete but must be merged, use a feature toggle to hide or disable the feature in production:
xxxxxxxxxx61const isFeatureEnabled = process.env.FEATURE_LOGIN === 'true';2if (isFeatureEnabled) {3 renderLoginPage();4} else {5 renderComingSoonMessage();6}Set the toggle via environment variables in the CI/CD pipeline:
xxxxxxxxxx11FEATURE_LOGIN=falseThis allows the code to exist in production without being user-facing.
Step 5: Maintain a Clean History
Periodically clean up by removing unused feature toggles or redundant code.
Example TBD Git Workflow
xxxxxxxxxx171# Pull the latest main branch2git checkout main3git pull origin main4
5# Create a short-lived branch (if necessary)6git checkout -b add-login-page7
8# Make changes and commit9echo "<h1>Login Page</h1>" > login.html10git add login.html11git commit -m "Add basic login page structure"12
13# Push and merge quickly14git push origin add-login-page15git checkout main16git merge add-login-page17git push origin mainAdvantages of Trunk-Based Development for CD
Rapid Delivery:
Frequent commits reduce integration conflicts.
Encourages small, manageable changes.
Simplified Workflow: No long-lived branches to manage.
Always Deployable: Ensures the
mainbranch is production-ready.Encourages Automation: Requires robust CI/CD pipelines for testing and deployment.
Challenges
Discipline: Requires teams to commit small, incremental changes.
Strong Automation: Relies heavily on CI/CD pipelines to maintain branch quality.
Risk of Incomplete Features: Feature toggles must be used to hide unfinished work.






















Leave a Reply