Advanced 20 min read

Building a CI/CD Pipeline for Salesforce

By the end of this lesson, you'll be able to:

  • Describe the stages of a typical Salesforce CI/CD pipeline
  • Explain the role of scratch orgs and automated tests in a pipeline

Prerequisites: package.xml and Change Sets vs. Source Deploy

Typical pipeline stages

A typical pipeline runs through: checking out the code, authenticating to a Dev Hub using stored credentials, creating a fresh scratch org and deploying source into it, running Apex tests, and — on success — either deploying onward to the next environment or simply reporting status back on the pull request.

Why scratch orgs fit CI so well

Scratch orgs are cheap, disposable, and created fresh from source on every single pipeline run — that gives every pull request a genuine clean-room test, catching problems a long-lived, gradually-drifted sandbox might quietly hide. Each PR can get its own throwaway scratch org: validated, tested, then destroyed.

A GitHub Actions pull request validation workflow

name: Validate Pull Request
on: pull_request
jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm install --global @salesforce/cli
      - run: sf org login sfdx-url --sfdx-url-file <(echo "$DEVHUB_AUTH_URL") --alias devhub
      - run: sf org create scratch --target-dev-hub devhub --definition-file config/project-scratch-def.json --alias ci-scratch
      - run: sf project deploy start --target-org ci-scratch
      - run: sf apex run test --target-org ci-scratch --result-format human --wait 10

Every step here is a CLI command you'd otherwise type by hand — a CI pipeline is really just those same commands, automated to run on every pull request.

Exercise

Write the CLI command that runs all local Apex tests against an org aliased 'ci-scratch' and waits up to 10 minutes for results.

Show hint

sf apex run test --target-org <alias> --wait <minutes>

BASH

Building a CI/CD Pipeline for Salesforce — Quick Check

1. Why are scratch orgs well-suited to CI pipelines?

2. A typical Salesforce CI pipeline runs Apex tests before deploying to the next environment.

3. Which CLI command executes Apex unit tests against a target org?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A Salesforce CI/CD pipeline automates what a developer would otherwise do by hand — validating, testing, and deploying metadata every time code is pushed — using the same CLI commands you've already been running manually, wired into a tool like GitHub Actions.