Sub topic 11
Building an Environment Pipeline
This chapter assumes you know the sandbox types and scratch orgs from earlier chapters, can deploy metadata with the Salesforce CLI, and use Git. Here you connect those org types into a path that carries a change from a developer's machine to production.
What a pipeline is
An environment pipeline is an ordered series of orgs. A change enters at the left, passes a test gate at each stage, and reaches production only after every earlier stage accepts it. Each stage answers one question, and each stage uses the org type best suited to that question.
The version control repository is the source of truth. Orgs are disposable or refreshable copies of what the repository describes. Changes move between orgs by deploying from Git, not by copying from one org to another.
The stages
| Stage | Org type | Question it answers | Data |
|---|---|---|---|
| Develop | Scratch org or Developer sandbox | Does my change work in isolation? | Seeded test records |
| Validate | Scratch org created by CI | Does the change deploy and pass tests from a clean state? | Seeded test records |
| Integrate | Developer Pro or Partial Copy sandbox | Do changes from several developers work together? | Sample production data |
| Stage | Full sandbox | Does it behave correctly at production scale and configuration? | Full production copy |
| Release | Production | Deploy the validated package. | Live data |
Teams do not need every stage. A small team can skip the integration stage and go from CI straight to a Full sandbox. Add a stage only when it answers a question the others cannot.
Rules that keep a pipeline healthy
- Deploy the same source at every stage. Use the same commit or package version. If you rebuild the change for each org, you are not testing what you will release.
- Never edit metadata directly in a downstream org. A change made by hand in the Full sandbox exists nowhere else, so the next deployment can overwrite it or fail because of it. This is configuration drift.
- Keep the order fixed. Production receives only what the staging org has already accepted.
- Refresh on a schedule. Integration and staging sandboxes go stale as production changes. Align refreshes with your release cadence and the refresh interval of each sandbox type.
- Automate setup. Use scratch org definition files, seed data scripts, and a
SandboxPostCopyclass so a new or refreshed org is ready without manual steps.
Moving a change through the stages
The commands below show the CLI side of one change. The CI job creates a scratch org, deploys, runs tests, and discards the org. A release job then validates against a sandbox and production.
# Validate in a fresh scratch org
sf org create scratch --definition-file config/project-scratch-def.json --alias ci-scratch --duration-days 1
sf project deploy start --target-org ci-scratch
sf apex run test --target-org ci-scratch --test-level RunLocalTests --wait 20 --result-format human
sf org delete scratch --target-org ci-scratch --no-prompt
# Deploy to the staging sandbox
sf project deploy start --target-org staging --test-level RunLocalTests
# Check-only deployment to production before the release window
sf project deploy validate --target-org prod --test-level RunLocalTestsThe check-only step runs the deployment and tests in production without saving changes. After it passes, you can quick-deploy the validated result during the release window, which shortens the time the release takes.
Handling data across the pipeline
Data differs by stage, and so do the risks. Scratch orgs hold only the records you seed, so they carry no production data. Partial Copy and Full sandboxes can contain real customer records. Apply data masking or run a post-copy class that anonymizes sensitive fields before anyone logs in. Limit who can access these sandboxes in the same way you limit production access.
Common mistakes
- Using a Full sandbox for daily development. It refreshes rarely, so it becomes a shared bottleneck. Reserve it for staging and performance testing.
- Skipping the scratch org stage and testing only in a sandbox. Hidden dependencies on existing sandbox configuration then go unnoticed.
- Letting the integration sandbox drift from production for months. Tests pass there and fail at release.
Exercise
Your team has a Full sandbox that developers use for daily work. Releases keep failing because other developers' unfinished changes are in it. Which two changes to the pipeline would fix this, and what does each stage then do?
Show answer
Move daily development to scratch orgs or Developer sandboxes, one per developer or feature, and create a CI scratch org to validate each pull request. Then reserve the Full sandbox for staging, where only changes that already passed CI are deployed from Git. Developers get isolation, CI gets a clean-state check, and staging reflects only accepted changes.
Exercise
A hotfix was made by hand in the staging sandbox and works there. Why is it a problem, and how should it be handled?
Show answer
The fix exists only in that org, so it is not in Git and will not reach production, or it will be overwritten by the next deployment. Recreate the fix in source, commit it, and deploy it through the pipeline stages in order.