Sub topic 13
Deploying only what changed with sfdx-git-delta
This chapter assumes you have a working pull request validation workflow and a push-to-main deploy workflow like the ones in earlier chapters. You should be comfortable with git diff, actions/checkout, and sf project deploy start.
Why deploy a delta
A full --source-dir force-app deployment sends every component in the project on every run. In a mature org that can mean thousands of files, long upload and validation times, and more chances for an unrelated component to fail the run.
The sfdx-git-delta plugin (often called SGD) compares two Git revisions. It writes only the metadata that was added or modified to an output folder. It also lists the metadata that was deleted. You then deploy that smaller folder.
The basic command
This is the command from the pipeline, with --from and --to set to two revisions.
# Requires a checkout with full history (actions/checkout with fetch-depth: 0)
sf plugins install sfdx-git-delta
sf sgd source delta \
--from origin/main \
--to HEAD \
--output-dir changed-sources \
--generate-delta
sf project deploy start \
--source-dir changed-sources/force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60With --generate-delta, the output folder contains three things:
| Path | Contents |
|---|---|
| `changed-sources/force-app/...` | Copies of the added and modified source files, in the same folder layout |
| `changed-sources/package/package.xml` | A manifest of the added and modified components |
| `changed-sources/destructiveChanges/destructiveChanges.xml` | A manifest of the components deleted between the two revisions |
Choosing the two revisions
Choosing --from and --to correctly is the main design decision. The right values depend on the trigger.
- Pull request validation: compare the base branch to the PR head. Use
origin/${{ github.base_ref }}as--fromandHEADas--to. If the base branch moved after the PR branched, diff against the merge base instead:$(git merge-base origin/main HEAD). This keeps changes that landed on main from appearing as if the PR had reverted them. - Deploy on merge: compare the commit before the push to the commit after it. Use
${{ github.event.before }}and${{ github.sha }}. - Releases or scheduled runs: compare the last deployed tag to the current commit. This is the most reliable approach when some pushes fail to deploy, because the delta then covers everything since the last successful deployment.
Fetching enough history
SGD runs git diff locally, so both revisions must exist in the runner's clone. actions/checkout fetches one commit by default, which makes the comparison fail. Set fetch-depth: 0 to fetch all history. For large repositories you can instead fetch only the base branch.
A complete validation workflow with a delta
This workflow replaces the full check-only deployment with a delta check-only deployment. It also skips the deployment when nothing relevant changed.
name: Validate pull request (delta)
on:
pull_request:
branches: [main]
paths:
- 'force-app/**'
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 SGD
run: |
npm install --global @salesforce/cli
echo 'y' | sf plugins install sfdx-git-delta
- name: Authenticate to the target org
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
echo "$SFDX_AUTH_URL" > authurl.txt
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target --set-default
rm authurl.txt
- name: Build the delta
run: |
BASE=$(git merge-base "origin/${{ github.base_ref }}" HEAD)
sf sgd source delta \
--from "$BASE" \
--to HEAD \
--output-dir changed-sources \
--generate-delta
- name: Check-only deploy of the delta
run: |
if ! grep -q "<types>" changed-sources/package/package.xml; then
echo "No deployable changes in this pull request."
exit 0
fi
sf project deploy start \
--dry-run \
--source-dir changed-sources/force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60Three details matter here.
- The
echo 'y'answers the prompt that the CLI shows for plugins that are not signed by Salesforce. Without it, the install can hang or fail on a runner with no terminal. In production pipelines, pin the plugin version (for example[email protected]) so a new release cannot change your pipeline's behavior overnight. - The
grepguard handles an empty delta. Deploying an empty folder returns an error, so a PR that changes only a README would otherwise fail validation. paths: force-app/**already prevents the workflow from starting for PRs that touch no Salesforce source. The guard still matters when a PR changes only files that SGD ignores, such as.forceignored files.
Handling deleted components
Deploying --source-dir changed-sources/force-app adds and updates components, but it never removes anything. Deleted files appear only in destructiveChanges.xml. To apply deletions, deploy with the manifest and pass the destructive file explicitly:
sf project deploy start \
--manifest changed-sources/package/package.xml \
--post-destructive-changes changed-sources/destructiveChanges/destructiveChanges.xml \
--source-dir changed-sources/force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60Deleting metadata from an org is hard to reverse, so treat it with extra care. Review the destructive file in the PR validation step, and consider running deletions only against sandboxes automatically while requiring approval for production.
Failure modes and edge cases
The main risk of a delta is that the deployed subset may not represent the whole project.
- Missing dependencies. If you rename a custom field but leave an unchanged Apex class that still references it, the delta contains the field but not the class. Validation passes, yet the org is now inconsistent. Running
RunLocalTestshelps, because it runs all local tests in the org, not only tests for the delta. A periodic full deployment, for example nightly, catches the rest. - Test coverage in production. Production requires tests when the deployment contains Apex. Because the delta may include only a few classes, the coverage requirement applies to the classes you deploy. Use
RunLocalTestsunless you have a deliberate reason to select tests. - Wrong comparison range. A force push, a first push to a new branch, or a missing
github.event.before(all zeros) breaks the push-based range. Fall back to a full deployment when the base revision does not resolve. - Metadata that spans files. Some types, such as profiles, permission sets, and custom labels, are merged into larger files. SGD handles many of these cases, but check the plugin documentation for your metadata types before relying on it.
- Ignored files. Files excluded by
.forceignoreare excluded from the delta as well. If a change seems to vanish, check that file first.
When not to use deltas
Deltas are less useful when the project is small, because a full deployment may take only a minute or two. They also add a plugin dependency and a Git history requirement. Start with full deployments, and adopt SGD when deployment time becomes a real cost.
Exercise
A pull request renames the custom field Account.Region__c and changes nothing else in the field's file path. An Apex class that references the field was not modified. The delta validation passes, but the production deployment fails after merge. Why did validation pass, and what two changes to the pipeline reduce the chance of this happening?
Show answer
The delta contained only the changed field metadata, so the unchanged Apex class was not part of the check-only deployment. The class's reference to the old field was not checked against the change. Two mitigations: run a scheduled full check-only deployment of force-app (for example nightly) to catch dependency drift, and use --test-level RunLocalTests so tests across the org's local Apex code still run during validation.