Sub topic 1
Why automate Salesforce deployments
This chapter explains what problems manual Salesforce deployments cause, what a CI/CD pipeline changes, and when automation is worth the setup cost. The later chapters build the pipeline step by step. This one gives you the model that those steps rest on.
Prerequisites
This chapter assumes you already have:
- Basic Git and GitHub usage: branches, pull requests, and merging.
- Familiarity with Salesforce metadata (objects, fields, Apex classes, flows) and with writing or running Apex tests.
- Basic command-line use.
- Basic YAML syntax, which you will need from the GitHub Actions chapters onward.
You do not need to have used the Salesforce CLI or GitHub Actions yet. Both are introduced later.
What CI/CD means here
Continuous integration (CI) means every change is merged into a shared branch often, and each change is automatically checked when it is proposed. Continuous delivery or deployment (CD) means a change that passes those checks is released to an org by a script instead of by a person clicking through a UI.
For Salesforce, this translates into two concrete automations:
- Validation. When someone opens a pull request, a job runs a check-only deployment against a real org. The check compiles the metadata and runs Apex tests, but it does not change the org.
- Deployment. When the pull request merges, a job deploys the same source to the target org.
In this tutorial, Git is the source of truth. The org is a deployment target, not the place where the "real" version of your configuration lives.
Problems with change sets and manual releases
Change sets are the default deployment tool in many teams. They work for small, occasional releases. They break down as teams and orgs grow, for these reasons:
- No version history. A change set records what was uploaded, not who reviewed it or why. You cannot diff two change sets or roll back to an earlier one.
- Manual assembly. Someone picks every component by hand. Forgetting a field, a profile entry, or a Flow dependency is the most common cause of a failed release.
- No deletions. Change sets cannot delete components, so orgs slowly drift apart.
- Limited paths. A change set only moves between orgs that share a deployment connection. Each target needs its own upload.
- Repetition. Promoting through several sandboxes means repeating the same manual work and trusting that each repetition was identical.
- Late test feedback. Failing tests are often discovered during the production deployment window, when the cost of a failure is highest.
- Source drift. When two developers change the same component in different sandboxes, the last upload wins and nobody is warned.
None of these are bugs in change sets. They follow from a tool that moves components without tracking history, reviews, or intent.
What a pipeline gives you
A pipeline replaces human steps with repeatable ones. The benefits are specific:
| Concern | Manual change set | Automated pipeline |
|---|---|---|
| What is deployed | Components chosen by hand | The exact commit on the branch |
| Review | Informal | Pull request review, required checks |
| Tests | Run when someone remembers, or at production deploy | Run on every pull request |
| Repeatability | Each promotion is a new manual job | Same commands, same order, every time |
| Audit trail | Deployment status page | Git history plus workflow run logs |
| Credentials | Personal logins | A dedicated integration identity stored as a secret |
| Rollback | Rebuild a change set by hand | Deploy an earlier commit |
Rollback deserves a caveat. Deploying an earlier commit restores the metadata that Git tracks. It does not undo data changes, and it does not remove components that the later commit added unless you also deploy a destructive change. Automation makes rollback easier to attempt, but you still need to plan for it.
How the pieces fit together
The pipeline built in this tutorial uses four parts:
- A Salesforce DX project, which stores metadata as source files in Git.
- The Salesforce CLI (
sf), which deploys that source and runs tests. - GitHub Actions, which runs commands on a hosted machine (a runner) when events happen in the repository.
- Secrets, which let the runner log in to an org without a person typing a password.
The following diagram shows the flow you will have by the end of the tutorial.
Notice that the check-only deployment happens before the merge, not after. Problems are found while the author still has the change in mind and while main is still healthy.
A small look at the commands
You do not need to run this yet, but it shows how little the two core operations differ. The only change between validating and deploying is one flag:
# Validate: compile and run tests, but do not change the org
sf project deploy start \
--dry-run \
--source-dir force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60
# Deploy: same command without --dry-run
sf project deploy start \
--source-dir force-app \
--test-level RunLocalTests \
--target-org target \
--wait 60Because validation and deployment share the same arguments, a passing validation is good evidence that the real deployment will also pass. It is not proof. The org can change between the two runs, which is why later chapters cover concurrency control and quick deploys.
When automation is worth it
Automation has a cost: someone has to build it, secure it, and keep it working. Consider these criteria.
Strong reasons to automate:
- More than one person changes the same org or the same metadata.
- You promote through two or more environments before production.
- Releases fail or need manual repair more than occasionally.
- You need an audit trail for compliance.
- Test failures are discovered late.
Reasons to wait or start small:
- A single admin makes occasional declarative changes in one org, and the org has no Apex.
- The team has no Git workflow yet. Adopt version control and pull requests first, because the pipeline depends on them.
- The org has years of metadata drift. Retrieve it into a DX project and clean it up before automating. A pipeline will only repeat the existing mess faster.
A sensible path is incremental. Start with pull request validation only. It carries the least risk because it never changes an org, and it delivers most of the early value. Add deployment on merge once the team trusts the validation results.
Common mistakes to avoid
- Treating the pipeline as a replacement for review. The pipeline checks that metadata compiles and tests pass. It cannot judge whether a change is a good idea.
- Letting people keep changing production directly. If admins edit production by hand, Git and the org diverge, and deployments will overwrite those edits or fail on conflicts. Agree on a rule, and enforce it with permissions where you can.
- Using a personal login for automation. Tie credentials to a dedicated integration user with the permissions deployments need, so a staff departure does not break releases.
- Skipping tests to make the pipeline faster. Production deployments that include Apex require tests to pass and coverage to meet Salesforce's thresholds. Speed comes from delta deployments and quick deploys, covered later, not from removing checks.
Exercise
A team of six developers promotes changes through two sandboxes to production using change sets. Releases often fail because a field or dependency was left out, and failures show up only during the production deployment. Name two specific problems a pull request validation job would address, and say why it should run before the merge instead of after.
Show answer
First, it catches missing components and compile errors, because a check-only deployment fails when a dependency is absent, and it does so before production is involved. Second, it runs Apex tests early, so test failures appear on the pull request instead of in the production release window. It should run before the merge so that failures are fixed while the author still has the change in mind, and so that a broken change never reaches the main branch that the deployment job uses.