Sub topic 1
Why automate Salesforce deployments
Prerequisites
Assume you know Git branches, pull requests, basic YAML, Salesforce metadata, and Apex tests. A Salesforce DX project is the source-controlled layout that the Salesforce CLI can deploy.
Why automate Salesforce deployments
Manual change sets depend on clicks, memory, and a person being available. A continuous integration and continuous delivery (CI/CD) pipeline makes deployment repeatable. It checks each proposed change, runs tests, records the result, and can deploy approved changes after a merge.
Automation also creates an audit trail. The commit identifies the source, while the workflow log shows the commands and test results. Keep production deployment separate from pull request checks so unreviewed code cannot reach production.
Preparing a Salesforce DX project for CI
Store deployable metadata under force-app/main/default. Commit sfdx-project.json, configuration files, Apex code, and metadata to Git. Do not commit authentication files or secrets.
The pipeline should be able to start from a clean checkout and deploy the project without local state. A consistent source layout prevents a workflow from depending on one developer's machine.
force-app/
└── main/default/
├── classes/
├── objects/
└── permissionsets/The Salesforce CLI commands a pipeline relies on
The Salesforce CLI command is sf. Common pipeline commands include:
sf org login sfdx-urlauthenticates with an organization.sf project deploy startdeploys source metadata.sf apex run testruns Apex tests.sf org create scratchcreates a temporary test organization.
The CLI returns a nonzero exit code when a command fails. GitHub Actions then marks the job as failed.
GitHub Actions in five minutes
A workflow is a YAML file in .github/workflows. It contains jobs, and each job contains steps that run on a hosted runner. A runner is a temporary machine provided by GitHub.
name: Validate Salesforce
on: [pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm install --global @salesforce/cli
- run: sf project deploy start --dry-run --source-dir force-app --test-level RunLocalTests --target-org targetThis example assumes the runner authenticates to target before deployment.
Choosing triggers: pull requests, pushes, and manual runs
Use pull_request to validate proposed changes before merging. Use push on main to deploy only reviewed code. Use workflow_dispatch for controlled reruns or planned deployments. Protect the production environment with required reviewers and environment-scoped secrets.
Exercise
Which trigger should normally run a check-only deployment?
Show answer
pull_request, because it validates the proposed change before it merges.