Sub topic 16
Running retrieve and deploy in scripts and CI
This chapter assumes you can already retrieve, deploy, validate, and quick-deploy by hand. It also assumes you know how to read a deploy failure. Here you move those commands into a script that runs without a person watching.
What changes when no one is watching
Automation needs three things that interactive use gets for free:
- Non-interactive authorization. A CI runner can't open a browser, so
sf org login webis not an option. - Exit codes you can trust. The pipeline must stop when a deploy fails.
- Output a program can read. Parsing text meant for people breaks easily.
Authorize without a browser
Two methods suit CI. The first is an auth URL file. You generate it once on a trusted machine with sf org display --verbose and store its contents as a secret. The second is the JWT flow, which uses a connected app and a private key.
sf org login sfdx-url --sfdx-url-file ./authurl.txt --alias ci-org --set-defaultsf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file ./server.key \
--username "$SF_USERNAME" \
--alias ci-org \
--set-defaultWrite the secret to a temporary file during the job and delete it afterward. Never commit it to the repository.
Use exit codes and JSON
An sf command exits with a non-zero code when it fails. Add --json to get a structured result on standard output. Add --wait to set how many minutes the command waits for the deployment to finish.
A script should stop at the first failure. Use set -euo pipefail for that.
#!/usr/bin/env bash
set -euo pipefail
ORG="${1:-ci-org}"
echo "Validating against $ORG"
sf project deploy validate \
--manifest manifest/package.xml \
--target-org "$ORG" \
--test-level RunLocalTests \
--wait 60 \
--json > validate.json
JOB_ID=$(jq -r '.result.id' validate.json)
echo "Validated job: $JOB_ID"The script saves the JSON so the pipeline can attach it as an artifact. It also reads the job ID from the saved file. The jq tool is a command-line JSON reader, and most CI images include it.
A release pipeline shape
A common pattern validates on every pull request and quick-deploys only after merge. The validated job ID has to travel between the two runs, either as an artifact or as a pipeline variable. Quick deploy only works on a validation that is still recent. Salesforce limits that window to 10 days.
Habits that prevent surprises
- Pass
--target-orgexplicitly. A shared runner may hold several authorized orgs, and a default can point at the wrong one. - Set a generous
--wait. If the wait runs out, the command exits with an error even though the deployment may still be running on the server. Usesf project deploy resumeorreportwith the job ID to check on it. - Pin the CLI version. Install a specific version of
@salesforce/cliso output formats and flags don't shift between runs. - Keep secrets out of logs. Don't
echothe auth URL or key, and mask the secrets in your CI settings. - Run a dry run first on risky changes.
--dry-runcosts a run but saves nothing to the org.
Exercise
A deploy step in your pipeline prints an error, but the pipeline still shows green and later steps run. Name two things to add or check in the script.
Show answer
Make the script stop on failure with set -euo pipefail, so a non-zero exit code from sf halts the job. Also check that no pipe or || true swallows the exit code. Adding --json and reading the status field gives a second check.