Sub topic 11
Automating with the Metadata API and CI
This chapter assumes you can already run sf project deploy validate and sf project deploy quick by hand. It also assumes you know what a job ID is and how quick deploy eligibility works. Here you move those steps into a script or a pipeline.
What the CLI calls underneath
The sf commands wrap the Metadata API. Any tool that can call that API can validate and quick deploy, whether it is a custom script, an IDE plugin, or a CI server.
| Manual step | CLI command | Metadata API call |
|---|---|---|
| Validate | `sf project deploy validate` | `deploy()` with `checkOnly` set to true |
| Check status | `sf project deploy report` | `checkDeployStatus()` |
| Quick deploy | `sf project deploy quick` | `deployRecentValidation()` |
The deploy() call returns an ID that starts with 0Af. This is the same job ID the CLI prints. deployRecentValidation() takes that ID and promotes the validation without running the tests again. All the eligibility rules from the earlier chapters still apply.
Two stages, two triggers
A validation and a quick deploy usually run at different times. Validation can run on a schedule, or when a release branch changes. Quick deploy runs once, inside the release window. The only thing that passes between the two stages is the job ID, so your automation must store it.
Capturing the job ID in a script
The --json flag makes the CLI print machine-readable output. The job ID is in result.id. This script validates and saves the ID to a file. It assumes jq is installed and the org alias prod is already authorized.
#!/usr/bin/env bash
set -euo pipefail
sf project deploy validate \
--manifest manifest/package.xml \
--test-level RunLocalTests \
--target-org prod \
--wait 60 \
--json > validation.json
jq -r '.result.id' validation.json > validated-job-id.txt
echo "Validated job ID: $(cat validated-job-id.txt)"With set -e, a failed validation returns a non-zero exit code and the script stops before it writes a job ID. That is the behavior you want, because a failed validation never leaves a usable ID behind.
The release-window script takes the stored ID as an argument:
#!/usr/bin/env bash
set -euo pipefail
JOB_ID="$1"
sf project deploy quick \
--job-id "$JOB_ID" \
--target-org prod \
--wait 30Running it from CI
A pipeline cannot open a browser, so it authorizes the org without one. The usual choice is the JWT bearer flow, which uses a connected app, a private key, and a named user.
sf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file server.key \
--username "$SF_USERNAME" \
--instance-url https://login.salesforce.com \
--alias prodKeep the client ID, username, and key in your CI system's secret store, never in the repository. Give the integration user only the permissions that deployment needs.
Passing the job ID between runs
The validation job and the quick deploy job often run hours or days apart, so they need somewhere shared to keep the ID. Common choices:
- A pipeline artifact from the validation run.
- A release record or tag that holds the ID as text.
- A manual input on the release job, where a person pastes the ID.
The manual input adds a human check, since someone confirms the ID belongs to the release being shipped. Automated storage removes that step and also removes a chance for typos.
Gotchas specific to automation
- Stale IDs. A stored job ID stops working when the validation is no longer eligible. A pipeline that blindly quick deploys an old ID will fail at release time. Have the release job check the ID with
sf project deploy reportfirst, and fall back to a full deploy if needed. - Source drift. A quick deploy promotes what was validated, not what is on the branch now. Record the commit hash alongside the job ID so you can tell which code the ID covers.
- Timeouts. A short
--waitvalue can make the CLI return before a long test run finishes. The deployment keeps running in the org. Report on the job ID rather than assuming failure. - Concurrent validations. Several validations running against the same org compete for test resources. Serialize them in your pipeline.
Exercise
A nightly pipeline validates the release branch, and a separate release job runs three days later. Name two pieces of information the validation job should save, and why.
Show answer
It should save the job ID, because the quick deploy command needs it. It should also save the commit hash, because the quick deploy promotes the validated code, and the hash shows which version of the branch that was. Saving the validation date is also useful for checking whether the validation is still eligible.