Sub topic 15
Validating merges automatically in CI
This chapter assumes you can resolve a conflict by hand, run sf project deploy start --dry-run, and read a basic GitHub Actions workflow. Manual checks work, but people skip them under time pressure. A continuous integration (CI) job runs the same checks on every pull request, so a bad merge is caught before it reaches your main branch.
What CI should check
Each check in the earlier chapters becomes a CI step. They run from cheapest to most expensive, so the pipeline fails fast.
- Search for leftover conflict markers.
- Confirm every metadata XML file is well-formed.
- Run a deployment dry run against a dedicated sandbox.
A GitHub Actions example
The workflow below runs on every pull request to main. For pull_request events, GitHub checks out a temporary merge of your branch into the base branch. The job therefore validates the merged result, not just your branch.
name: validate-merge
on:
pull_request:
branches: [main]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install tools
run: |
npm install --global @salesforce/cli
sudo apt-get install -y libxml2-utils
- name: Fail on conflict markers
run: |
if grep -rnE '^(<<<<<<<|=======|>>>>>>>)' force-app; then
echo "Conflict markers found"
exit 1
fi
- name: Check XML is well-formed
run: find force-app -name '*-meta.xml' -print0 | xargs -0 xmllint --noout
- name: Authenticate to the CI sandbox
env:
SFDX_URL: ${{ secrets.SANDBOX_AUTH_URL }}
run: |
echo "$SFDX_URL" > auth-url.txt
sf org login sfdx-url --sfdx-url-file auth-url.txt --alias ci-sandbox
rm auth-url.txt
- name: Deployment dry run
run: >
sf project deploy start --dry-run
--source-dir force-app
--target-org ci-sandbox
--test-level RunLocalTestsStore the sandbox authentication URL as a repository secret. Never commit it to the project.
Common errors
- The dry run passes but production breaks. The CI sandbox differs from production, for example in features, licenses or existing components. Keep the sandbox as close to production as you can.
- Two pull requests pass separately but fail together. Each was validated against
mainbefore the other merged. Enable "require branches to be up to date before merging" in branch protection, or use a merge queue. - The marker search flags a legitimate line. A file such as a Markdown document might contain a line of
=======. Limit the search toforce-appand to metadata file types, as above. - Tests make the job slow.
RunLocalTestscan take a long time. Use it for merges intomain, and use a lighter test level for early feedback on feature branches.
Making the check required
A CI job only protects you if the merge is blocked when it fails. In the repository's branch protection rules, mark the validate job as a required status check for main. Then a pull request with leftover markers or a broken profile cannot be merged, even by accident.
Exercise
A teammate resolves a conflict in a permission set but forgets to delete the >>>>>>> line. The file is also no longer valid XML. Which CI step fails first, and why do you run it before the dry run?
Show answer
The conflict marker search fails first, because it is the cheapest check. Running it and the XML check before the dry run gives fast feedback and avoids using the sandbox's deployment slot for a deployment that cannot succeed.
Exercise
Why does the workflow trigger on pull_request instead of push to the feature branch?
Show answer
For pull_request events, GitHub checks out a temporary merge of the branch into the base branch. The checks therefore run against the result of the merge, which is the code that would actually land on main. A push trigger only tests the feature branch alone.