The user wants a guide or plan for implementing "Trunk-Based Development" (TBD).
Specifically, they asked for four key components:
- Branch Policy: Rules for branches (protection, naming, lifespan).
- Merge Queue: How to handle merging into the trunk without breaking things.
- Flake Triage: How to deal with flaky tests (crucial in TBD because they block the fast flow).
- Small PR Coaching: How to teach/encourage developers to write small, incremental changes.
Target Audience: Engineering Managers, Tech Leads, or DevOps engineers looking to implement this methodology.
Tone: Actionable, structured, and authoritative but supportive.
1. Branch Policy:
- Core concept: Everyone commits to
main (or master). Branches are short-lived.
- Rules: No direct commits to
main (require PRs for code review). No long-lived feature branches (like develop or release/*).
- Naming: Short, descriptive, maybe prefixed with the ticket ID, but the key is they are ephemeral.
- Lifespan: Ideally < 1 day. Max 2-3 days.
- Protection: Required status checks, required approvals.
2. Merge Queue:
- Why? "It works on my machine" doesn't mean it works when merged with other people's changes. In high-velocity TBD, race conditions happen.
- Mechanism: Instead of clicking "Merge" immediately, PRs enter a queue. The system merges the first one, runs tests. If pass, merge. Then takes the second, rebases/merges on top of the new trunk, runs tests.
- Tools: GitHub Merge Queue, Mergify, Zuul, Buildkite (with specific plugins).
- Config: Batch size (usually 1 for strictness, or batching for speed), timeout settings.
3. Flake Triage:
- The Problem: Flaky tests kill TBD. If tests are flaky, the merge queue clogs up, and developers lose trust in CI.
- Strategy:
- Detection: Automated detection (test passes/fails inconsistently).
- Quarantine: Move flaky tests out of the critical path immediately. Don't let them block a release.
- Triage Process: "Flake Friday" or a dedicated rotation.
- Fix or Delete: If a test is flaky and hard to fix, is it valuable? Maybe delete it.
4. Small PR Coaching:
- The Problem: Developers love "big bang" features. TBD requires incrementalism.
- Techniques:
- Feature Flags: The #1 enabler. Merge code that isn't visible yet.
- Refactoring first: Preparatory PRs (renaming, moving files) before logic changes.
- The "How to Review" angle: If a PR takes > 30 mins to review, it's too big.
- Culture: Praise small PRs. Don't punish "incomplete" code if it's behind a flag.
Structuring the Output:
I will structure this as a "Rollout Plan" or "Playbook". It makes it easier for a manager to say "Do step 1, then do step 2."
- Phase 1: The Policy (The Rules)
- Phase 2: The Infrastructure (Merge Queue)
- Phase 3: The Maintenance (Flake Triage)
- Phase 4: The Culture Shift (Small PRs)
Refining the content:
- Branch Policy: Be strict about "short-lived". Mention the "3 day rule".
- Merge Queue: Explain the "Merge vs. Rebase" debate briefly (usually Merge Queues rebase the PR against the queued tip).
- Flakes: Need a specific process. "The Flake Jar" or "Quarantine".
- Coaching: Use the "Vertical Slicing" analogy. Don't build the whole database layer; build one endpoint.
Self-Correction/Review:
- Did I explain why Merge Queues are needed? Yes, because
main moves fast.
- Is the distinction between GitFlow and TBD clear? Implied, but I should focus on the TBD specifics (no
develop branch).
- Are the "Small PRs" practical? I need to mention Feature Flags explicitly, otherwise small PRs are impossible for unfinished user-facing features.
Final Polish:
Organize with clear headings, bullet points, and actionable steps. Use bold text for emphasis.
Let's draft the response based on these points.
- Branch Policy: Enforce <1 day lifespan. Protect
main. Ban direct pushes.
- Merge Queue: Use GitHub's native or Mergify. Explain the "speculative check" concept.
- Flake Triage: The "Quarantine" pattern is standard. Automate failure detection.
- Coaching: Teach "slicing". Use "Draft" PRs for early feedback if the PR is getting big.
Let's write it out.
This is a comprehensive rollout plan for adopting Trunk-Based Development (TBD). This plan moves away from long-lived feature branches and heavy release branches toward a model of continuous integration to main.
1. Branch Policy (The Rules of the Road)
The goal is to minimize branching complexity to reduce merge conflict hell and increase velocity.
The Setup:
main is King: There is only one permanent branch: main. It must always be deployable.
- No
develop or release/* branches: These create a "merge façade" that hides integration problems until release day.
The Policy:
- Short-lived Feature Branches:
- Branch from
main.
- Lifespan: Max 1–3 days. If it takes longer, break it down.
- Naming:
feature/ticket-id-short-desc or bugfix/ticket-id.
- Branch Protection Rules (GitHub/GitLab/Bitbucket):
- No Direct Pushes: Nobody pushes directly to
main.
- Required Reviews: Minimum 1 approval (optional for trivial changes if you have pair programming).
- Status Checks: CI must pass (Build, Unit Tests, Lint).
- Signed Commits: Enforce identity verification.
- Forbidden Actions:
- No force-pushing to
main.
- No "skipping" CI checks on
main.
2. Merge Queue (The Gatekeeper)
In TBD, everyone merges into main constantly. Without a queue, you get "test flakiness" and "race conditions" where PR A passes tests, but PR B merges first, causing PR A to break main when it lands.
The Solution: Use a Merge Queue (GitHub Merge Queue, Mergify, Zuul, or Buildkite).
The Workflow:
- Developer: Clicks "Ready to Merge" / "Add to Queue."
- System: Takes the PR and rebases it on top of the current tip of
main (which includes the PR currently being merged ahead of it).
- Validation: Runs the CI suite on this "speculative future state" of
main.
- Action:
- Pass: Auto-merge to
main.
- Fail: Kick out of queue, notify developer to fix conflicts.
Configuration Recommendation:
- Batching: If tests are slow, configure the queue to batch 2-3 PRs together and test them as a group.
- Race Protection: Ensure the queue checks that the PR is rebased on the very latest commit before merging.
3. Flake Triage (Maintaining Velocity)
In TBD, if main breaks, development stops. If tests are flaky (pass/fail randomly), developers lose trust in CI and will ignore failures.
The Protocol:
-
The "Main is Sacred" Rule:
- If
main breaks (Red CI), it is an all-hands priority. All coding stops until it is fixed.
- Rollback is preferred over "fixing forward" if the fix isn't immediate.
-
Flake Quarantine Strategy:
- Detection: Use a tool (like
flake8 wrappers or CI analytics) to detect tests that pass on retry.
- Quarantine: Move flaky tests to a separate "Quarantine Suite" immediately. They should not block the Merge Queue.
- SLA: A quarantined test is a critical debt ticket. It must be fixed or deleted within 3 days.
- Responsibility: The engineer who wrote the test (or the last one to touch it) owns the fix.
-
Retry Policy:
- Do not allow automatic retries on the Merge Queue. It hides flakiness.
- Allow retries on PRs (to save developer sanity), but investigate if a test passes on the 2nd try.
4. Small PR Coaching (The Cultural Shift)
The biggest blocker to TBD is developers creating "Mega-PRs" (1000+ lines). Mega-PRs kill code review quality and block the merge queue.
Coaching Tactics:
-
The "Lunch Break" Rule:
- If you can't finish the feature by lunch, split it.
- Use Feature Flags to merge incomplete code safely into
main without exposing it to users.
-
Slicing Techniques (Vertical vs. Horizontal):
- Bad (Horizontal): "I'm merging all the database schema changes first, then the backend logic next week." (Breaks
main if DB is incompatible).
- Good (Vertical): "I am adding one new column, and the code to read/write to it, hidden behind a flag." (Safe to merge).
-
Refactoring vs. Features:
- Encourage "Pre-factoring" PRs. Clean up the code before adding the new feature, in a separate PR. This keeps the diff easy to review.
-
Review SLAs:
- Small PRs require fast reviews.
- Rule: A PR under 200 lines must be reviewed within 2 hours. A PR over 400 lines requires a scheduled meeting to walk through it (this incentivizes developers to keep PRs small).
Summary Checklist for Rollout
- Configure CI: Ensure tests run fast (aim for <10 mins for Merge Queue).
- Setup Feature Flags: Install a system (LaunchDarkly, Unleash, or simple ENV vars) so code can be merged but inactive.
- Implement Merge Queue: Turn on the queue feature in your Git provider.
- Education: Workshop on "How to slice stories into 1-day tasks."
- Go Live: Start with a single team, then expand.