Sub topic 12
Using scratch orgs in continuous integration
This chapter assumes you can write a scratch org definition file and use sf org create scratch. It also assumes your project is in Git with source in force-app, and that you know your CI service's basics, such as workflows and secrets. The examples use GitHub Actions, but the steps are the same in any CI system.
Why scratch orgs suit CI
A CI job needs a clean, repeatable environment. A scratch org is built from a definition file and your source, so every run starts from the same state. A shared sandbox accumulates changes from other developers and earlier runs, so the same commit can pass one day and fail the next.
Scratch orgs also cost little to throw away. The job creates one, tests against it, and deletes it. Nothing carries over between runs.
The job lifecycle
The delete step must run whether the tests pass or fail. Otherwise failed runs leave orgs behind, and those orgs count against your limits.
Authorizing the Dev Hub without a browser
sf org login web needs a person at a browser, so CI cannot use it. Use the JWT flow instead. Create a connected app in the Dev Hub with a certificate, and keep the matching private key in your CI system's secret store. The job then logs in without interaction.
A complete workflow
name: validate-pull-request
on:
pull_request:
branches: [main]
jobs:
validate:
runs-on: ubuntu-latest
env:
SCRATCH_ALIAS: ci-${{ github.run_id }}
steps:
- uses: actions/checkout@v4
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authorize Dev Hub
env:
JWT_KEY: ${{ secrets.SF_JWT_KEY }}
CLIENT_ID: ${{ secrets.SF_CLIENT_ID }}
DEVHUB_USERNAME: ${{ secrets.SF_DEVHUB_USERNAME }}
run: |
printf '%s' "$JWT_KEY" > server.key
sf org login jwt --client-id "$CLIENT_ID" --jwt-key-file server.key --username "$DEVHUB_USERNAME" --set-default-dev-hub --alias devhub
- name: Create scratch org
run: sf org create scratch --definition-file config/project-scratch-def.json --alias "$SCRATCH_ALIAS" --duration-days 1 --no-track-source --wait 15
- name: Deploy source
run: sf project deploy start --target-org "$SCRATCH_ALIAS"
- name: Run Apex tests
run: sf apex run test --target-org "$SCRATCH_ALIAS" --test-level RunLocalTests --code-coverage --result-format human --wait 20
- name: Delete scratch org
if: always()
run: |
sf org delete scratch --target-org "$SCRATCH_ALIAS" --no-prompt
rm -f server.keySome choices in this file matter:
- Unique alias.
github.run_idgives each run its own alias, so parallel runs do not collide. - One-day duration.
--duration-days 1is a safety net. If cleanup fails, Salesforce expires the org within a day. - No source tracking.
--no-track-sourceskips tracking that a throwaway org never uses. if: always(). The delete step runs after a failed deploy or failed tests.
Common problems
| Symptom | Likely cause |
|---|---|
| Job fails at org creation with a limit error | Daily or active scratch org allocation used up, often by orphaned orgs |
| Deploy succeeds locally but fails in CI | A feature or setting is missing from the definition file |
| Tests pass in CI but fail in a sandbox | The sandbox has data or configuration that the scratch org lacks |
| Login step fails | Wrong username, or the connected app is not pre-authorized for that user |
Keeping CI fast
Creating an org takes minutes, so you can reduce how often you do it. Run lightweight checks such as static analysis before the scratch org step. For larger projects, build scratch orgs from a pre-built snapshot or install unlocked package dependencies from a package version instead of deploying them each time.
Exercise
A CI job that validates pull requests starts failing at the "Create scratch org" step. Earlier runs worked, and the definition file has not changed. Name one likely cause and one change to the workflow that prevents it.
Show answer
The Dev Hub has probably hit its active or daily scratch org limit because earlier failed runs left orgs undeleted. Prevent it by running the delete step with if: always() so cleanup happens even when earlier steps fail, and by using a short --duration-days so leftover orgs expire quickly.