Sub topic 5
A trunk-based flow with main as the source of truth
This chapter assumes you have read the comparison of branching models and the chapter on mapping orgs to places code lives. You should know that production, sandboxes, and scratch orgs hold deployed metadata, and that Git holds the source. You also need to be comfortable with pull requests and the sf CLI.
What "source of truth" means here
In a trunk-based flow, the main branch is the one place that defines what the team intends to ship. Every other branch is a short-lived proposal to change main. Every org is a deployed copy of some version of main.
Three statements follow from this:
- If a change is not in
main, it is not part of the product, however well it works in someone's sandbox. - If an org differs from
main, the org is wrong, unless the difference is a change you have not yet merged. - Any release can be rebuilt from a commit on
main, so you do not need to preserve a sandbox to preserve a release.
Salesforce makes the third statement harder to hold than it is for most stacks. Orgs accumulate state that Git cannot see: manual setup changes, data, and sandbox refresh history. A strict rule that main wins gives you a reference point for finding and fixing that state. Later chapters on org drift and release promotion build on it.
The rules of the flow
The model needs only a few rules.
- There is one long-lived branch,
main. There are no permanentdevelop,qa, oruatbranches. - All work happens on short-lived branches created from the current tip of
main. - Work reaches
mainonly through a pull request that passes validation and review. mainis always in a deployable state. A brokenmainis the team's top priority.- Environments are promoted by deploying commits from
main. They are never promoted by merging between branches. - Releases are marked with tags on
main.
The last two rules are the biggest change for teams used to environment branches. With a uat branch, "what is in UAT" means "what is merged into that branch," and the branches slowly diverge. Here, "what is in UAT" means "which commit of main was last deployed there." That is a single value you can look up, and a tag or a deployment log records it.
Mapping main to environments
main does not correspond to a single org. It is deployed to several orgs, each at its own pace.
| Place | What it holds | How it relates to main |
|---|---|---|
| Scratch org or developer sandbox | One developer's work in progress | Created from `main`, changed on a feature branch, thrown away or refreshed |
| Validation target (for pull requests) | Nothing is saved | Receives check-only deployments of the feature branch merged with `main` |
| Integration sandbox | The latest merged work | Updated automatically after each merge to `main` |
| UAT sandbox | A release candidate | Receives a chosen commit of `main` on a schedule or on demand |
| Production | The released product | Receives a commit that has already passed UAT, then gets a tag |
The integration sandbox is optional for small teams, but it catches problems that a check-only deployment cannot, such as a flow that behaves differently with real data. UAT is the stage where business users test. Because UAT and production receive the same commit, what users approved is what ships.
A sandbox can be ahead of or behind production only in terms of which main commit it holds. It never holds code that is not in main.
Merging into main
Choose a merge method and apply it consistently. For Salesforce, squash merge is usually the best fit.
- Squash merge turns each pull request into one commit on
main. Reverting a feature means reverting one commit. History reads as a list of features, and noisy intermediate commits disappear. - Merge commit keeps every commit and adds a merge commit. It preserves detail, but
mainbecomes harder to scan and to revert. - Rebase merge keeps history linear, but it rewrites commits, which can confuse people who built on the original branch.
Whichever you choose, keep local copies of main clean by using fast-forward-only pulls, as in the feature workflow:
git switch main
git pull --ff-only origin mainThe --ff-only flag makes Git refuse to create a merge commit locally. If your local main has diverged from the remote, the command fails and tells you. A local main should never hold commits of its own.
Keeping main deployable with unfinished work
Short branches are the goal, but some features take weeks. The usual answer in trunk-based development is to merge the work in small pieces and hide the unfinished parts. Salesforce offers several ways to do that:
- Custom permissions. Gate a feature behind a custom permission and assign it only to testers. Apex can check it with
FeatureManagement.checkPermission('Your_Permission_Api_Name'). Flows and validation rules can check it too. - Custom metadata flags. Store an on/off switch in a custom metadata type record. Changing a flag is a metadata deployment, so it is reviewed and tracked.
- Inactive or unassigned components. A flow can be deployed in draft status. A Lightning page or app that is not assigned to anyone does nothing.
- Unreferenced code. A new Apex class that nothing calls is safe to merge. The trigger or component that uses it comes later.
Use the simplest option that makes the merged code safe. Do not use a flag to cover for an unfinished change that breaks existing behavior. If the old behavior breaks, the flag is not doing its job.
Automating the deployment from main
Promotion should be a deployment of main, not a manual copy. This workflow deploys every merge to an integration sandbox. It uses the same authentication pattern as the validation check.
name: Deploy main to integration
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to the integration sandbox
env:
SFDX_AUTH_URL: ${{ secrets.INTEGRATION_AUTH_URL }}
run: |
printenv SFDX_AUTH_URL > auth-url.txt
sf org login sfdx-url --sfdx-url-file auth-url.txt --alias integration
rm auth-url.txt
- name: Deploy
run: sf project deploy start --source-dir force-app --target-org integration --test-level RunLocalTests --wait 60Two points matter here. First, the job runs only on pushes to main, so the sandbox can never receive unreviewed code. Second, the auth URL is a stored secret scoped to this one sandbox. Give production and UAT their own secrets, and protect them with environment approvals so a person confirms those deployments.
Common errors
Treating a sandbox as a branch. A developer deploys from a feature branch straight to UAT "to let people look at it." Now UAT holds code that is not in main, and the next deployment from main may silently remove it. Use a scratch org or a developer sandbox for early feedback, and keep UAT for commits from main.
Letting main break. If validation is skipped or flaky, a bad merge blocks everyone's next deployment. Fix or revert first. Because squash merges give you one commit per change, git revert <sha> is a fast way to restore a deployable main.
Long-lived feature branches. A branch that lives for a month will conflict with changes to the same profiles, flows, and layouts. Split the work and merge behind a flag instead. The next chapter on branch names and lifecycle sets concrete age limits.
Hotfixing in production first. Fixing something directly in production creates a change that exists nowhere in Git. Reproduce the change in source and merge it, or the next deployment will overwrite it.
Promoting by merging. Merging main into a uat branch to "promote" recreates the environment-branch model with all its drift. Deploy a commit, not a branch.
Exercise
A team has a uat branch. A developer merges feature A into uat for testing, then feature B is merged into main and released. Two weeks later, UAT contains feature A but production does not, and nobody can say which commit UAT matches. Describe how the trunk-based flow with main as the source of truth would prevent this, and name where a developer should show feature A to testers instead.
Show answer
In the trunk-based flow there is no uat branch. UAT receives only commits from main, so its contents are identified by a single commit that a tag or deployment log records. Feature A would reach UAT only after passing validation and review and merging into main. If it is not finished, it merges behind a custom permission or flag so it is inactive. For early feedback before merging, the developer shows feature A in a scratch org or developer sandbox created from the feature branch.