Sub topic 11
Building an environment pipeline
An environment pipeline is the ordered set of orgs that a change passes through on its way to production. Each stage answers one question about the change, and each stage uses the org type best suited to answering it. This chapter shows how to assemble those stages from the org types covered earlier, and how to keep them consistent.
Prerequisites
This chapter assumes you already know the following:
- The difference between production and non-production orgs.
- How to deploy metadata between orgs.
- Basic command-line use and the Salesforce CLI (
sf). - Basic Git concepts: branches, pull requests, merges.
It also builds on the earlier chapters about sandbox types, scratch orgs, source tracking, and the decision framework for choosing an org type.
What a pipeline is for
A pipeline gives you three things:
- Isolation. Developers work without overwriting each other's changes.
- Progressive confidence. Early stages are cheap and fast. Later stages are slower and closer to production.
- A single path to production. Every change takes the same route, so releases are repeatable.
The working model is simple. Source control holds the truth about your configuration and code. Orgs are disposable or refreshable places where that truth is exercised. Production is the only org where changes arrive last, and only after earlier stages have passed.
A reference pipeline
The diagram shows one common layout. Your team may drop or add stages.
Each stage in this layout has a distinct role.
| Stage | Org type | Question it answers | Data |
|---|---|---|---|
| Development | Scratch org (or Developer sandbox) | Does my change work in isolation? | Minimal seeded data |
| Pull request validation | Scratch org created by CI | Does the project deploy and pass tests from a clean state? | Seeded data or none |
| Integration | Developer Pro or Developer sandbox | Do merged changes from several developers work together? | Metadata only, plus test records |
| QA | Partial Copy sandbox | Does it work against realistic data? | Sample of production data |
| Staging | Full sandbox | Does it perform and behave like production? | Full copy of production data |
| Production | Production | Release | Live |
The stages are a pattern, not a requirement. A small team might use only scratch orgs, a single integration sandbox, and production. A team with heavy data-dependent logic or performance concerns will need a Full sandbox.
Choosing stages by what each one proves
Add a stage only when it proves something the earlier stages cannot.
- Scratch orgs prove deployability. A fresh scratch org has no hidden configuration. If your project deploys and its tests pass there, the source is self-contained.
- Integration sandboxes prove compatibility. They catch conflicts between changes that were each built in isolation.
- Partial Copy sandboxes prove behavior against realistic records. Use them for data-sensitive tests and user acceptance testing.
- Full sandboxes prove production-like behavior. Use them for performance testing, data volume checks, and release rehearsals.
Full sandboxes have long refresh intervals, so they are a scarce resource. Do not use one for work a cheaper org can do.
Source control is the backbone
The pipeline works only if every org is populated from source control and not from manual edits. Two rules keep it honest:
- Changes enter through Git. A developer builds in a scratch org or Developer sandbox, retrieves the change into the local project, commits it, and opens a pull request.
- Orgs receive changes through deployment. Nobody changes the integration, QA, or staging sandboxes by hand.
Here is the developer loop using a scratch org.
# Start a feature branch and a fresh scratch org
git checkout -b feature/account-scoring
sf org create scratch --definition-file config/project-scratch-def.json --alias feature-scratch --duration-days 7 --set-default
# Push the project source to the org
sf project deploy start
# ...make changes in the org with point-and-click tools, then bring them back...
sf project retrieve start
# Commit and push for review
git add force-app
git commit -m "Add account scoring fields and flow"
git push -u origin feature/account-scoringsf project deploy start and sf project retrieve start use source tracking when the target org supports it, which scratch orgs and Developer and Developer Pro sandboxes do. Tracking means the CLI sends and fetches only what changed. Partial Copy and Full sandboxes do not support source tracking by default, so deploys to those orgs must name what to deploy, typically with a manifest or a source directory.
Promoting changes between sandboxes
Promotion past the integration stage is a deployment from the main branch to each sandbox in turn. Use a validation first, then a real deployment of the same package.
# Check that the deployment would succeed and tests pass, without saving changes
sf project deploy validate --manifest manifest/package.xml --target-org qa --test-level RunLocalTests
# After approval, deploy to the same org
sf project deploy start --manifest manifest/package.xml --target-org qa --test-level RunLocalTestsFor production, the same pattern applies. A validated deployment can be quick-deployed later, which shortens the release window. Quick deploy is only available for a limited time after validation and only if the validation ran the required tests, so check your org's current rules before you rely on it.
# Validate against production ahead of the release window
sf project deploy validate --manifest manifest/package.xml --target-org prod --test-level RunLocalTests
# During the release window, deploy the validated package using the job ID printed by validate
sf project deploy quick --job-id 0AfXXXXXXXXXXXXXXX --target-org prodReplace the job ID with the one returned by your own validation.
Branch strategy and org mapping
Map long-lived branches to long-lived sandboxes. A simple mapping:
| Branch | Target org | Deployed when |
|---|---|---|
| Feature branch | Scratch org (disposable) | On every pull request, by CI |
| `main` | Integration sandbox | After each merge |
| `release/*` | QA, then staging sandbox | When a release candidate is cut |
| Tag on release branch | Production | At the release window |
Avoid a different branch for every sandbox. Branches that drift apart cause merge conflicts and make it unclear which version is in which org.
Keeping environments consistent
Environments drift when someone changes an org directly or when sandboxes are refreshed at different times. Drift makes a passing test in staging unreliable evidence about production.
- Refresh on a schedule. Refresh QA and staging sandboxes after each major release so they match production again. Each sandbox type has a minimum refresh interval, so plan around it.
- Automate post-refresh setup. A
SandboxPostCopyclass (see the earlier chapter) re-applies settings and seed data each time, so a refresh does not wipe out manual setup. - Redeploy after refresh. A refreshed sandbox takes the metadata of production at that moment. Anything in the pipeline that has not reached production yet must be deployed again from Git.
- Block direct edits where you can. Limit who has the right to change configuration in shared sandboxes, and treat a change made there as a defect to be captured in Git.
A refresh also affects people. Announce it, because developers working in a Developer sandbox lose any unretrieved changes when it is refreshed.
Data in the pipeline
Data needs change by stage, and so does the risk.
- Scratch orgs start empty unless you load data. Keep a small, versioned seed data set in the repository and load it with the CLI.
- Partial Copy and Full sandboxes can contain real customer data. Treat them as sensitive. Use data masking so that personal information is scrambled after copy, and restrict who can log in.
- Never load production exports into a scratch org or a developer's laptop unless your data policy allows it.
# Load versioned seed records into the current scratch org
sf data import tree --plan data/seed-plan.jsonPipeline failure modes
| Problem | Cause | Fix |
|---|---|---|
| Tests pass in CI but fail in staging | Scratch org has no data or different settings than production | Add a data-dependent test stage in a Partial Copy or Full sandbox. Align settings in the definition file. |
| Deployment to QA fails on missing components | QA was refreshed from production and lacks changes still in the pipeline | Deploy the earlier unreleased changes first, or merge in order. |
| A fix exists in an org but not in Git | Someone edited the org directly | Retrieve the change, commit it, and restrict direct edits. |
| Full sandbox is unavailable when needed | Refresh interval or someone else is using it | Reserve it for release rehearsals and book it in advance. |
| Release takes too long | Full test run during the release window | Validate ahead of time and use quick deploy. |
Good practices
- Keep the pipeline as short as your risk allows. Every stage adds wait time and upkeep.
- Make every stage's result visible. A deployment that fails should fail the pull request or the release job.
- Use the same deployment command and test level at every stage so results are comparable.
- Document which branch maps to which org and who owns each sandbox.
Exercises
Exercise
A team has scratch orgs for development and production, and nothing in between. Their releases keep failing in production because of problems with realistic data volumes. Which stage would you add, which org type fits it, and what does it prove?
Show answer
Add a staging stage using a Full sandbox. It holds a full copy of production data, so it proves that the change behaves correctly and performs acceptably at production data volumes before release. A Partial Copy sandbox is a cheaper choice if the problems come from data shape rather than volume.
Exercise
You deploy a change from main to a QA sandbox that was refreshed yesterday. The deployment fails because a custom field the change depends on is missing. The field is already merged to main but has not been released to production. Why did this happen, and what do you do?
Show answer
The refresh copied production's metadata, which does not yet include the unreleased field. Anything that has been merged but not released is absent after a refresh. Deploy the earlier merged changes from Git to the QA sandbox first (or deploy the whole main branch source), then deploy the dependent change.
Exercise
Why is it risky to promote a change by manually recreating a configuration in each sandbox, and what should you do instead?
Show answer
A manual copy leaves no record, differs slightly each time, and cannot be repeated or reviewed, so environments drift apart. Instead, retrieve the change into the project, commit it to Git, and deploy it to each org from source with the same command.