Sub topic 14
Deploying only what changed
This chapter shows how to build a deployment from the files a branch changed instead of the whole project. It covers where a delta helps, how to compute one, and which cases need a full deploy. It assumes you have read the earlier chapters on pull request validation and release promotion. It also assumes you can read a git diff and run sf commands.
Why deploy a delta
A full deployment sends every file in force-app to the org. That is fine for a small project. As the project grows, a full deploy gets slow, and the cost lands on every pull request. A check-only deploy of 4,000 components can take much longer than one of the 6 components the author changed.
A delta deployment sends only the components that differ between two Git commits. Because Git already records what changed, you can derive the deployment contents from the history.
A delta has three benefits:
- Shorter validation. Fewer components means less metadata for the org to compile and save.
- Smaller blast radius. Components nobody touched cannot be overwritten with stale versions.
- Clearer review. The deployment contents match the pull request diff.
A delta also has costs. It does not prove that the untouched parts of the project still work with your change. It can miss dependencies and deletions. The sections below cover each of these.
What a delta is made of
A delta has two parts.
- Additions and modifications. These are files that exist in the newer commit and are new or different compared with the older one. They go into a
package.xmlmanifest, or into a temporary source folder. - Deletions. These are files that existed in the older commit and are gone in the newer one. Salesforce does not remove a component just because it is missing from a deploy. You must list it in a
destructiveChanges.xmlfile.
The older commit is the base. For a pull request, the right base is the point where your branch left main, which Git calls the merge base. If you compare against the current tip of main instead, any commits that landed on main after you branched show up as changes you "deleted".
git fetch origin main
BASE=$(git merge-base origin/main HEAD)
# Files added, copied, modified, or renamed on this branch
git diff --name-only --diff-filter=ACMR "$BASE" HEAD -- force-app
# Files deleted on this branch
git diff --name-only --diff-filter=D "$BASE" HEAD -- force-appThe first command is the raw material for a deploy. The second is the raw material for a destructive manifest. For a few files you could pass the first list to --source-dir yourself. That breaks down quickly. One Apex class is two files, a .cls and a -meta.xml. A Lightning web component is a folder, and one changed file means the whole bundle must go. A custom object is spread across many small files. Mapping file paths to metadata types is the hard part, so most teams use a tool for it.
Generating the delta with sfdx-git-delta
sfdx-git-delta (often shortened to sgd) is a Salesforce CLI plugin. It compares two commits and writes a package.xml and a destructiveChanges.xml for you.
echo y | sf plugins install sfdx-git-delta
BASE=$(git merge-base origin/main HEAD)
sf sgd source delta \
--from "$BASE" \
--to HEAD \
--output-dir changed-sources \
--generate-delta
sf project deploy start \
--manifest changed-sources/package/package.xml \
--post-destructive-changes changed-sources/destructiveChanges/destructiveChanges.xml \
--dry-run \
--test-level RunLocalTests \
--target-org ci-org \
--wait 60Flag names and output folders can change between plugin versions, so check sf sgd source delta --help after you upgrade. The --dry-run flag keeps this a check-only deploy, as in the pull request validation chapter. If the branch deleted nothing, the destructive file is empty. Some teams skip the flag in that case.
The overall flow looks like this:
Run it in the pull request check
The generator needs the full Git history to find the merge base. GitHub Actions checks out a shallow copy by default, which breaks git merge-base. Set fetch-depth: 0 on the checkout step.
name: Validate pull request (delta)
on:
pull_request:
branches: [main]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI and delta plugin
run: |
npm install --global @salesforce/cli
echo y | sf plugins install sfdx-git-delta
- name: Authenticate to the validation org
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
printenv SFDX_AUTH_URL > auth-url.txt
sf org login sfdx-url --sfdx-url-file auth-url.txt --alias ci-org
rm auth-url.txt
- name: Build the delta
run: |
BASE=$(git merge-base origin/main HEAD)
sf sgd source delta --from "$BASE" --to HEAD --output-dir changed-sources --generate-delta
- name: Check-only deploy of the delta
run: |
sf project deploy start \
--manifest changed-sources/package/package.xml \
--post-destructive-changes changed-sources/destructiveChanges/destructiveChanges.xml \
--dry-run --test-level RunLocalTests --target-org ci-org --wait 60Where deltas go wrong
Missing dependencies
A delta contains what changed, not what the change needs. Suppose your branch adds a flow that references a custom field created by an earlier pull request. If the validation org does not already have that field, the check fails even though main is fine. Keep the validation org in line with main, because the delta assumes everything outside it is already deployed. The org drift chapter covers how to detect when that assumption has broken.
Deletions and renames
A renamed Apex class is a deletion of the old name plus an addition of the new one. If your tooling reads only added and modified files, the old class stays in the org. The two names then exist side by side, and old references keep working until someone removes them. Always generate and deploy the destructive file, and review it in the pull request like any other change.
Partial metadata
Some metadata types are containers: profiles, permission sets, and custom labels. A delta of a profile contains only the portions that changed. Deploying it adds or updates those portions and leaves the rest alone. That is usually what you want. It does not remove permissions that were deleted from the file, and you should not expect it to.
Test coverage
A delta cuts the number of components deployed, but it does not necessarily cut the tests that run. RunLocalTests runs every test in the org that is not in a managed package. Production deployments that include Apex have their own rules. All local tests run by default, and each deployed class and trigger must reach its coverage threshold. To run fewer tests, use RunSpecifiedTests with a --tests list. That list is a judgment call about which tests cover your change. A wrong list gives a green check on a breaking change.
When to use a full deploy instead
| Situation | Delta | Full |
|---|---|---|
| Pull request validation | Good default | Use on a schedule |
| Promotion to UAT | Acceptable | Preferred for release candidates |
| Production release | Possible | Prefer it when you can afford the time |
| Rebuilding a sandbox or scratch org | Not suitable | Required |
| Large refactor or type changes | Risky | Preferred |
| Suspected org drift | Hides problems | Reveals them |
A common pattern is to use deltas on pull requests and run a full validation of main nightly. If the nightly run fails, a delta missed a dependency, a deletion, or a drifted org. You then find out the next morning instead of during a release.
Keeping deltas small
How small a delta is depends on how the work is split. Short-lived branches, covered in the branching chapters, produce small deltas by themselves. Reformatting many files, or running a retrieve that rewrites every file's line endings or XML ordering, produces large deltas of noise. Commit generated changes separately, and keep your retrieve settings stable across the team.
Exercise
A pull request check compares origin/main to HEAD using a two-dot diff. Two developers merged into main after you branched. What goes wrong with the delta, and what base should you use instead?
Show answer
Files changed on main after you branched look like differences on your side. Files that were added to main appear to be deleted by your branch, so the destructive manifest can remove them from the validation org. Use the merge base, git merge-base origin/main HEAD, as the --from value so the delta contains only your branch's changes.
Exercise
Your delta check passes on every pull request, but the nightly full validation of main fails with a missing custom field. Name two likely causes.
Show answer
The validation org drifted from main, for example because someone changed or deleted the field directly in the org. Or an earlier pull request referenced a component that was never in any delta, such as a field that was never committed. Either way, the delta assumed everything outside it was already deployed, and that was false.
Exercise
Why must the CI checkout use fetch-depth: 0 when you build a delta against a merge base?
Show answer
The default checkout is shallow and holds only recent commits. git merge-base needs the shared history of both branches, so with a shallow clone it can fail or pick the wrong commit.