Sub topic 15
Wiring seeding into a setup script and CI
Earlier chapters covered each seeding step on its own. This chapter joins them into one script that a developer runs locally and a CI job runs on every pull request. It assumes you have a DX project with a plan file, an Apex seed script, and a Dev Hub you can authorize from a pipeline.
One script, one entry point
Put the whole sequence in a single script, so local setup and CI run the same steps. If they diverge, CI stops telling you whether a new developer can get a working org.
#!/usr/bin/env bash
set -euo pipefail
ALIAS="${1:-my-scratch}"
if ! sf org display --target-org "$ALIAS" > /dev/null 2>&1; then
sf org create scratch \
--definition-file config/project-scratch-def.json \
--alias "$ALIAS" \
--set-default \
--duration-days 7 \
--wait 10
fi
sf project deploy start --target-org "$ALIAS"
sf data upsert bulk \
--sobject Account \
--file data/accounts.csv \
--external-id External_Id__c \
--target-org "$ALIAS" \
--wait 10
sf apex run --file scripts/apex/seed.apex --target-org "$ALIAS"set -euo pipefail makes the script stop at the first failed command. Without it, a failed deploy would be followed by an import into an org that has no fields, and the later error would hide the real cause.
The sf org display check skips creation when the alias already exists, so rerunning the script refreshes an existing org.
The script uses sf data upsert bulk with an external ID instead of sf data import tree. Tree import always creates new records, so a second run would duplicate them. Upsert updates rows it has already loaded. The Apex seed script inserts every time, so give it a guard, such as a query for an existing Seed Account, before you rely on it for reruns.
Running it in CI
CI creates a fresh scratch org per run, uses it, and deletes it. The pipeline authorizes the Dev Hub with an SFDX auth URL stored as a secret.
name: scratch-org-check
on: pull_request
jobs:
seed-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authorize Dev Hub
env:
DEVHUB_URL: ${{ secrets.DEVHUB_SFDX_URL }}
run: |
echo "$DEVHUB_URL" > devhub.txt
sf org login sfdx-url --sfdx-url-file devhub.txt --alias devhub --set-default-dev-hub
rm devhub.txt
- name: Create and seed scratch org
run: ./scripts/setup.sh ci-scratch
- name: Run Apex tests
run: sf apex run test --target-org ci-scratch --wait 20 --result-format human
- name: Delete scratch org
if: always()
run: sf org delete scratch --target-org ci-scratch --no-promptif: always() on the last step deletes the org even when an earlier step fails. Scratch orgs count against your Dev Hub's active limit, so orphaned ones can block later runs.
Practical rules
- Keep secrets out of the repository. Store the auth URL in the CI secret store and delete the file after login.
- Seed only from files in the repository, such as plan files, CSVs, and Apex. Do not export from a production org during CI.
- Order steps by dependency. Metadata goes first, then data, then tests.
- Fail loudly. A seeding step that fails silently produces tests that fail for confusing reasons.
Exercise
A teammate reruns setup.sh against an existing scratch org and finds duplicate Accounts. Which seeding step is the likely cause, and how do you fix it?
Show answer
The likely cause is sf data import tree, or an Apex insert with no guard, because both create new records on every run. Switch to sf data upsert bulk with an external ID field such as ExternalId_c, or make the Apex script check for existing records before inserting.