Sub topic 13
Deploying only what changed with sfdx-git-delta
This chapter assumes you have a working pull request validation workflow and know the sf project deploy start flags from earlier chapters. It also assumes you can read a Git diff.
Why deploy a delta
A full deployment sends every component in force-app. As a project grows, the upload gets slower, and the test run grows with it. Most pull requests touch a handful of files.
sfdx-git-delta (SGD) is a Salesforce CLI plugin. It compares two Git revisions and copies only the changed metadata into a new folder. You then deploy that folder instead of the whole project.
Running the plugin
# 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 60--from and --to are the two revisions to compare. --output-dir is where SGD writes its results. --generate-delta makes SGD copy the changed source files into that folder. Without it, you only get a package.xml manifest.
Fetching enough history
The runner must have both revisions locally. By default, actions/checkout fetches a single commit, so the comparison fails. Set fetch-depth: 0 to fetch the full history.
- uses: actions/checkout@v4
with:
fetch-depth: 0Choosing the revisions
The right --from value depends on the trigger.
| Trigger | `--from` | `--to` |
|---|---|---|
| Pull request | `origin/main` | `HEAD` |
| Push to main | `${{ github.event.before }}` | `HEAD` |
On a push to main, comparing against origin/main would show nothing, because the merge has already happened. Use the commit that main pointed to before the push.
Deleted components
Deleting a file in Git does not delete the component in the org. SGD records removed components in a destructiveChanges folder inside the output directory. Pass the file to the deploy command with --post-destructive-changes so the org matches the repository.
sf project deploy start \
--source-dir changed-sources/force-app \
--post-destructive-changes changed-sources/destructiveChanges/destructiveChanges.xml \
--test-level RunLocalTests \
--target-org target \
--wait 60Trade-offs and common errors
- Empty delta. If nothing under
force-appchanged, the output folder may be empty and the deploy step fails. Guard the step, or rely on thepathsfilter in your trigger. - Missing dependencies. A changed component can reference one that did not change, such as a field used by a Flow. Validation in a full-copy sandbox catches this.
- Test level.
RunLocalTestsstill runs every local test. A delta shortens the upload, not the test run. UseRunSpecifiedTestsonly if your team accepts the coverage risk.
Exercise
A workflow on push to main uses --from origin/main --to HEAD and produces an empty package after every merge. Why, and what should --from be?
Show answer
After the merge, origin/main and HEAD are the same commit, so there is no difference. Use ${{ github.event.before }}, the commit main pointed to before the push.