Sub topic 14
Validate once, quick deploy later
This chapter assumes you have already built the pull request validation workflow (a check-only deploy with RunLocalTests) and the deploy-on-merge workflow from earlier chapters. You should also be comfortable with GitHub secrets and environments, and with reading JSON output on the command line.
The problem quick deploy solves
In a production org, a deployment that runs all local Apex tests can take 30 to 90 minutes in a large org. If you validate on the pull request and then run the same tests again on merge, you pay for that time twice. The second run also holds up the release window.
A quick deploy removes the second test run. You first run a validation, which is a check-only deployment that runs the tests but saves nothing to the org. Salesforce keeps the result and gives it a job ID. Later, you ask Salesforce to deploy that already-validated package using the job ID. Salesforce skips the tests because they have already passed.
Rules and limits
A quick deploy is accepted only if all of these hold:
- The validation succeeded and ran tests. In production, that means a test level that runs Apex tests, such as
RunLocalTests. - The tests met the code coverage requirement.
- The validation finished in the last 10 days. After that, the job ID expires.
- You deploy to the same org that ran the validation.
Quick deploy is mainly a production feature, because that is where tests are mandatory and slow. Check the current Salesforce release notes before you rely on it for sandboxes.
A quick deploy always deploys the validated package. It does not re-read your repository. This matters for the pipeline design below.
The flow
Step 1: validate and capture the job ID
Use sf project deploy validate. With --json, the job ID is in result.id. The workflow below extracts it with jq, which is preinstalled on GitHub-hosted Ubuntu runners, and writes it to the run summary.
name: Validate for quick deploy
on:
pull_request:
branches: [main]
paths:
- 'force-app/**'
jobs:
validate:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to production
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
echo "$SFDX_AUTH_URL" > authurl.txt
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target --set-default
rm authurl.txt
- name: Validate with local tests
run: |
sf project deploy validate \
--source-dir force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60 \
--json > validate.json || { cat validate.json; exit 1; }
JOB_ID=$(jq -r '.result.id' validate.json)
echo "Validated job ID: $JOB_ID"
echo "Validated job ID: \`$JOB_ID\`" >> "$GITHUB_STEP_SUMMARY"Two details matter here. The || { cat validate.json; exit 1; } part prints the JSON error when the validation fails, so the log shows which tests or components failed. Without it, the redirect hides the output. Also, this job uses the production environment so it can read that environment's secret. If you add a required-reviewer rule to that environment, every pull request run will wait for approval. In that case, give validation its own environment, for example production-validate, with the same credentials and no reviewers.
Step 2: quick deploy with the job ID
The second workflow runs on demand and takes the job ID as an input. The production environment can require a reviewer before the job starts, which gives you the approval gate from the promotion chapter.
name: Quick deploy to production
on:
workflow_dispatch:
inputs:
job_id:
description: 'Job ID from a successful validation (starts with 0Af)'
required: true
type: string
concurrency:
group: deploy-production
cancel-in-progress: false
jobs:
quick-deploy:
runs-on: ubuntu-latest
environment: production
steps:
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to production
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
echo "$SFDX_AUTH_URL" > authurl.txt
sf org login sfdx-url --sfdx-url-file authurl.txt --alias target --set-default
rm authurl.txt
- name: Quick deploy
env:
JOB_ID: ${{ inputs.job_id }}
run: |
sf project deploy quick \
--job-id "$JOB_ID" \
--target-org target \
--wait 60This workflow does not check out the repository. A quick deploy needs only the job ID and an authenticated org. The input is passed through an environment variable and quoted in the shell, so a malformed value cannot inject commands.
Choosing what the job ID refers to
The job ID points to the exact package that was validated, not to a branch. This creates the main failure mode.
Suppose pull requests A and B are both validated against the same main. You merge A, then quick deploy B's job ID. Production now has B's validated package, but the code in main is the result of merging A and B together. That combination was never tested. Quick deploy also does not warn you about this.
Ways to reduce the risk:
- Turn on the branch protection rule "Require branches to be up to date before merging". Each pull request is then re-validated after another one merges.
- Use a merge queue, and validate on the
merge_groupevent so the tested code is the code that lands. - Quick deploy only the job ID from the latest validation of the commit you are releasing. Record the commit SHA next to the ID, for example in the run summary.
- If the validation is older than 10 days or the quick deploy is rejected, fall back to a full
sf project deploy start --test-level RunLocalTests.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Quick deploy reports the job ID is invalid or not found | Wrong org, a typo, or the validation is older than 10 days | Confirm the alias points to the validating org, and re-run the validation |
| Quick deploy rejected because the validation did not run tests | Validation used `NoTestRun` | Re-validate with `RunLocalTests` or another test level that runs Apex tests |
| `jq` prints `null` for the ID | The validation failed and the JSON holds an error, not a result | Read `validate.json` in the log and fix the failing component or test |
| Quick deploy waits, then times out | `--wait` is shorter than the deploy | Raise `--wait`, or check progress with `sf project deploy report --job-id` |
Exercise
Developers A and B each open a pull request against the same main and both validations pass. A merges first, and the team then quick deploys B's job ID. What is the risk, and name two ways to prevent it?
Show answer
The quick deploy sends B's package exactly as validated against the old main, so the combination of A and B that now exists in main was never tested together. Production can end up with something different from the repository, or with a hidden break. Prevent it by requiring branches to be up to date before merging (so B re-validates after A merges), or by validating in a merge queue on the merge_group event. Recording the validated commit SHA and quick deploying only the job ID for the release commit also works.
Exercise
A quick deploy fails with a message that the job ID cannot be used. Give three conditions to check, in the order you would check them.
Show answer
First, check that the target org alias is the org that ran the validation. Second, check that the validation finished within the last 10 days. Third, check that the validation succeeded and ran Apex tests (not NoTestRun) and met the code coverage requirement. If any check fails, re-run the validation and use the new job ID.