Sub topic 5
Choosing triggers: pull requests, pushes, and manual runs
What this chapter assumes
This chapter assumes you can read a basic GitHub Actions workflow file, that you know what a branch and a pull request are, and that you have a Salesforce DX project with source under force-app. You should also know that the Salesforce CLI (sf) can run a check-only deployment. A trigger decides when a workflow starts. In a Salesforce pipeline, that decision controls how often you spend org API calls, how long developers wait, and whether an unreviewed change can reach production.
The on: key at the top of a workflow file lists the events that start it. Each event can have filters that narrow it further. This chapter covers the three events a deployment pipeline uses most: pull_request, push, and workflow_dispatch.
The working model
GitHub emits an event when something happens in a repository. A workflow runs only if one of its on: entries matches the event and passes that entry's filters. If no entry matches, nothing happens and nothing is logged in the Actions tab.
A common branch strategy for Salesforce teams has two kinds of runs:
- Validation runs happen on a pull request. They check that the change would deploy and that tests pass, but they change nothing in the org.
- Deployment runs happen after a merge. They change the org.
The triggers map directly onto this split.
Pull request triggers
The pull_request event fires when a pull request is opened, when new commits are pushed to its branch, and when it is reopened. Those are the default activity types. You can change them with the types key, but the default suits validation.
on:
pull_request:
branches: [main]
paths:
- 'force-app/**'
- 'sfdx-project.json'Two filters appear here, and they behave differently from what people often expect.
branchesmatches the target branch of the pull request, not the source branch.branches: [main]means "pull requests that propose merging into main."pathsruns the workflow only if the pull request changes at least one matching file. A pull request that edits only the README does not trigger validation.
The paths filter saves real time. A check-only deployment with RunLocalTests can take many minutes, and a documentation change does not need it. Include sfdx-project.json and any pipeline-related config such as config/project-scratch-def.json, because changes to them can alter what deploys.
The required check problem
Path filters have a trap. If you mark the validation job as a required status check in branch protection, and a pull request skips the workflow because of paths, the check never reports. GitHub then shows it as "Expected" and blocks the merge forever.
You have two ways out:
- Remove
pathsfrom the workflow and skip work inside the job instead, for example with a step that detects changed files. - Keep
paths, and do not mark that check as required.
Option 1 is safer when you want a hard gate.
Pull requests from forks
For pull requests from forks, GitHub does not pass repository secrets to the workflow. Your authentication step would fail because SFDX_AUTH_URL would be empty. This is intentional protection, since a fork's author could otherwise edit the workflow to print your secrets. Most Salesforce teams work in a single repository with branches, so this matters mainly for open-source projects. Do not switch to pull_request_target to get around it without understanding the risk. That event runs with secrets while checking out code that you may not trust.
Push triggers
The push event fires when commits arrive on a branch. A merge into main creates a push to main, so this is the standard trigger for deployment.
on:
push:
branches: [main]
paths:
- 'force-app/**'Keep these points in mind:
brancheson apushevent matches the branch being pushed to. This differs in meaning frombranchesonpull_request.- Anyone with write access can push directly to
mainunless branch protection forbids it. Pair this trigger with a protection rule that requires a pull request, so that every deployment has passed validation first. - A push that creates a tag does not match a
branchesfilter. Usetagsif you want releases to deploy.
Several environments from several branches
A team with more than one org often maps branches to orgs. A push to develop can deploy to an integration sandbox, and a push to main can deploy to production.
on:
push:
branches:
- develop
- mainInside the job you then choose the org from github.ref_name. The chapter on promotion covers this in detail. For now, remember that a wider trigger means each branch needs its own credentials and safeguards.
Manual triggers with workflow_dispatch
workflow_dispatch adds a Run workflow button in the Actions tab and allows calls through the GitHub CLI or API. It is useful for redeploying after a transient failure, deploying a hotfix, or running a validation against a chosen org without opening a pull request.
You can define inputs that the person running the workflow fills in.
name: Manual deploy
on:
workflow_dispatch:
inputs:
test-level:
description: 'Apex test level'
type: choice
options:
- NoTestRun
- RunSpecifiedTests
- RunLocalTests
- RunAllTestsInOrg
default: RunLocalTests
dry-run:
description: 'Check only, do not change the org'
type: boolean
default: true
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Show chosen settings
run: |
echo "Test level: ${{ inputs.test-level }}"
echo "Dry run: ${{ inputs.dry-run }}"Two details matter for safety:
- Defaulting
dry-runtotruemeans a careless click validates instead of deploying. - A
choiceinput restricts values to a fixed list. A free-text input interpolated into a shell command with${{ }}can inject commands, so preferchoiceorbooleanfor anything that reachesrun:. Production orgs also rejectNoTestRun, so listing it only makes sense when the target is a sandbox.
The manual trigger only works when the workflow file exists on the repository's default branch. If you add it on a feature branch, the button will not appear until you merge.
Combining triggers
One workflow can list several events. Often, though, separate files are clearer, because validation and deployment have different permissions and different secrets.
| Event | Typical purpose | Changes the org? | Filter meaning for `branches` |
|---|---|---|---|
| `pull_request` | Check-only deploy and tests | No | Target branch of the pull request |
| `push` | Deploy after merge | Yes | Branch that received the commits |
| `workflow_dispatch` | On-demand deploy or validation | Depends on input | Not applicable |
| `schedule` | Nightly full test run | No | Runs on the default branch |
The schedule event uses cron syntax in UTC. It is useful for a nightly RunAllTestsInOrg job that catches drift, such as a setting changed directly in the org.
on:
schedule:
- cron: '0 2 * * *'Common errors
- Workflow never starts. Check that the file is under
.github/workflows/, that the YAML parses, and that the filters match. Apathsmismatch is the most frequent cause. - Validation runs, but deployment does not. The merge may have changed only files outside your
pathslist, or the push went to a branch not listed. - Deployment runs on a pull request. This happens when a
pushtrigger lists a wide branch pattern such as'**'. Restrict it. - Two events start the same work. If you list both
pushandpull_requestfor the same branch, a commit on a pull request branch can run the workflow twice. Limitpushto long-lived branches.
Exercise
A workflow has on: pull_request: branches: [main] and paths: ['force-app/**']. You mark its job as a required status check. A teammate opens a pull request that only edits README.md, and the merge button stays blocked. Why does this happen, and what is one fix?
Show answer
The paths filter does not match README.md, so the workflow never runs and the required check never reports. GitHub waits for it indefinitely. One fix is to remove the paths filter and skip the expensive steps inside the job when no Salesforce files changed. Another is to stop marking the check as required.
Exercise
You want a button in the Actions tab that deploys to a sandbox, with a choice of test level and a default that does not change the org. Which event and which input types would you use?
Show answer
Use workflow_dispatch. Add a choice input for the test level and a boolean input named for dry run with default: true. The workflow file must be on the default branch for the button to appear.