Sub topic 14
Checking, resuming, and canceling deployments
A deployment runs on the Salesforce server, not on your laptop. The sf command only starts the job and watches it. This chapter covers what you can do when the watching stops early, when you want to look at a job later, and when you need to stop a job.
This chapter assumes you can already run sf project deploy start and sf project deploy validate against an authorized org. It also assumes you know what a deploy ID is. If you skipped the chapters on test levels and validation, read those first.
Why a deployment can outlive your terminal
When you run sf project deploy start, the CLI uploads your components and receives a deploy ID. This is an identifier that starts with 0Af. Salesforce queues the job and processes it. The CLI then polls the org until the job finishes or the wait time runs out.
The wait time is set with --wait, in minutes. If the wait expires, the CLI stops polling and exits. The deployment keeps running in the org. Closing your terminal, losing your network connection, or pressing Ctrl+C has the same effect: only the local watcher stops.
This is why the commands in this chapter exist.
| Command | What it does | Changes the org? |
|---|---|---|
| `sf project deploy report` | Prints the current status and details of a job once, then exits. | No |
| `sf project deploy resume` | Reattaches to a job and waits for it to finish, showing progress as it goes. | No |
| `sf project deploy cancel` | Asks Salesforce to stop a job that is still running. | Stops the job |
Job states
A deployment moves through a small set of states. Knowing them tells you whether to wait, read errors, or retry.
- Pending means the job is queued and has not started. Salesforce runs one deployment at a time per org, so a job can wait behind another.
- InProgress means components are being saved or tests are running.
- Succeeded means everything was saved. For a validation-only job, it means the job passed and can be quick-deployed.
- SucceededPartial appears in some cases, such as a deployment with
ignoreWarningsor a partial result. Read the result table before assuming everything landed. - Failed means the deployment was rolled back. By default a deployment is all-or-nothing, so nothing was saved.
- Canceling and Canceled follow a cancel request.
Starting a deploy you can walk away from
If you expect a long run, such as a large manifest with RunLocalTests, start it with --async. The command returns right after the upload and prints the deploy ID.
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --test-level RunLocalTests --asyncYou can also keep the default behavior and set a bounded wait:
sf project deploy start --manifest manifest/package.xml --target-org my-sandbox --test-level RunLocalTests --wait 10If the job takes longer than 10 minutes, the command exits with a non-zero code, which is not the same as the deploy failing. A script that treats every non-zero exit as a failed deploy will report false failures. Check sf project deploy start --help for the exit codes in your CLI version, and use report to learn the real state.
Checking status with report
report reads the job's current state from the org. It does not wait.
sf project deploy report --job-id 0AfXXXXXXXXXXXXXXX --target-org my-sandboxThe --use-most-recent flag selects the last deployment the CLI started from this machine. It does not need the ID.
sf project deploy report --use-most-recentThe output includes the status, the number of components deployed, any component errors, and Apex test results with failures and code coverage. The same detail is what you need to diagnose a failure, so report is also the main tool for reading errors after the fact.
Where the most recent ID comes from
--use-most-recent relies on a small cache in your home directory, written when the CLI starts a deployment. This has two consequences:
- The cache is per machine and per user. A deployment started by a teammate or by a CI runner is not in your cache.
- In a CI job that starts a deployment in one step and reports in another, the second step may run on a fresh machine. Pass the ID explicitly with
--job-id.
Finding a job you did not start from this machine
Every deployment, whoever started it, appears in the org under Setup, in Deployment Status. You can copy the deploy ID from there and pass it to report. Use this when someone else's deployment is blocking yours and you want to know what it is doing.
Resuming a watch
resume is report with waiting. It reattaches to the job and polls until the job reaches a final state or the new --wait time ends.
sf project deploy resume --use-most-recent
sf project deploy resume --job-id 0AfXXXXXXXXXXXXXXX --wait 30Use resume after a timeout, or after you started with --async and want to watch the rest of the job. It does not start a new deployment and does not upload anything again, so it is safe to run repeatedly.
Resuming also works for validation jobs and quick deployments, because the job ID identifies the job whatever its type.
Canceling a job
cancel asks Salesforce to stop a job that has not finished.
sf project deploy cancel --job-id 0AfXXXXXXXXXXXXXXX --target-org my-sandboxThings to know before you rely on it:
- Cancellation is a request. The job moves to Canceling, and Salesforce then finishes or abandons its current step. Wait for Canceled before you start another deployment.
- A canceled deployment is rolled back, the same as a failed one. Components that were saved during the run are not kept.
- You can only cancel a job that is Pending or InProgress. A job that already finished returns an error from
cancel. - If the job is in the last stage of saving, it may complete before the cancel takes effect. Run
reportafterward to confirm the final state.
Cancel is the right choice when you started the wrong manifest, targeted the wrong org, or are blocking a more urgent deployment. It is not a way to speed up a slow test run, because a canceled job leaves you to start again from the beginning.
A polling script
In automation, --async plus polling gives you control over timeouts and logging. This script starts a deployment, polls report, and exits with a code that reflects the final state. It needs jq.
#!/usr/bin/env bash
set -u
ORG="my-sandbox"
ID=$(sf project deploy start \
--manifest manifest/package.xml \
--target-org "$ORG" \
--test-level RunLocalTests \
--async --json | jq -r '.result.id')
if [ -z "$ID" ] || [ "$ID" = "null" ]; then
echo "Could not start deployment" >&2
exit 1
fi
echo "Started deployment $ID"
while true; do
STATUS=$(sf project deploy report --job-id "$ID" --target-org "$ORG" --json \
| jq -r '.result.status')
echo "Status: $STATUS"
case "$STATUS" in
Succeeded)
exit 0 ;;
Failed|Canceled|SucceededPartial)
sf project deploy report --job-id "$ID" --target-org "$ORG"
exit 1 ;;
*)
sleep 30 ;;
esac
doneThe script treats SucceededPartial as a failure on purpose. Whether that is correct depends on your release rules. The script has no overall time limit, so add a maximum number of loops if a stuck job should fail the pipeline.
Common problems
- "No deploy ID found" with
--use-most-recent. The cache has no entry, usually because you are on a different machine or the cache was cleared. Get the ID from Deployment Status and use--job-id. - The job stays Pending for a long time. Another deployment holds the org's queue. Check Deployment Status to see which one.
resumeorreporttargets the wrong org. The job ID belongs to one org. If your default target org changed since you started the job, pass--target-orgexplicitly.cancelfails with a "not cancelable" message. The job already reached a final state. Runreportto see which.
Exercise
You start a RunLocalTests deployment with --wait 5 in a sandbox. After five minutes the command exits with a non-zero code. What should you do to find out whether the deployment failed, and what must you do before starting a second deployment if you decide the first one is wrong?
Show answer
A timeout does not mean failure, so run sf project deploy report --use-most-recent (or --job-id with the ID) to read the real status, or sf project deploy resume --use-most-recent to keep watching. If you decide the job is wrong, run sf project deploy cancel --job-id <id> and wait until the status is Canceled before starting another deployment, because the org runs one deployment at a time.
Exercise
A CI pipeline starts a deployment in one step and runs sf project deploy report --use-most-recent in the next step on a new runner. The report step fails. Why, and how do you fix it?
Show answer
--use-most-recent reads a cache stored on the machine that started the job, and the new runner has no such cache. Capture the deploy ID from the first step (for example with --async --json and jq -r '.result.id') and pass it to the later step with --job-id.