Sub topic 2
Installing the Salesforce CLI on the Runner
Assume you know GitHub Actions, YAML, Salesforce metadata, and Apex tests. A runner is the temporary virtual machine that executes a workflow. The runner needs the Salesforce CLI before it can authenticate or deploy.
Install the CLI
Install the CLI after checking out the repository. setup-node provides Node.js, which the installation command uses.
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Salesforce CLI
run: npm install --global @salesforce/cliPin the Node.js and CLI versions when reproducible builds matter. This reduces unexpected changes between runs.
Store credentials with secrets and environments
Never commit an authentication URL or private key to the repository. Store the value in a GitHub Actions secret, such as SFDX_AUTH_URL.
A GitHub environment groups deployment settings. A production environment can hold separate secrets and require an approved reviewer before the job starts. Use a less privileged secret for validation when possible.
Authenticate to the org
Pass the secret through an environment variable. Write it to a temporary file, log in, and remove the file immediately.
- name: Authenticate to the target org
env:
SFDX_AUTH_URL: ${{ secrets.SFDX_AUTH_URL }}
run: |
echo "$SFDX_AUTH_URL" > authurl.txt
sf org login sfdx-url \
--sfdx-url-file authurl.txt \
--alias target \
--set-default
rm authurl.txtJWT bearer authentication is another option. It uses a connected app, a client ID, and a certificate stored as secrets. It avoids storing an auth URL but requires more setup.
Choose Apex tests in CI
RunLocalTests runs tests created in the target org, excluding managed-package tests. It is a common deployment choice. RunAllTestsInOrg also runs managed-package tests and can take longer. RunSpecifiedTests runs only named tests when the deployment rules allow it.
The deployment checks Apex coverage and test results. A check-only deployment validates metadata without changing the org.
Validate every pull request
name: Validate pull request
on:
pull_request:
branches: [main]
paths:
- 'force-app/**'
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm install --global @salesforce/cli
- name: Check-only deploy
run: >
sf project deploy start
--dry-run
--source-dir force-app
--test-level RunLocalTests
--target-org target
--wait 60Exercise
Why should a pull request workflow use --dry-run?
Show answer
It checks metadata, tests, and coverage without changing the target org.