Sub topic 4
GitHub Actions in five minutes
This chapter assumes you can read basic YAML, you know what a branch and a pull request are, and you have run commands in a terminal. It covers the vocabulary of GitHub Actions: workflows, events, jobs, steps, and runners. Later chapters use these pieces to build the Salesforce pipeline.
The mental model
GitHub Actions runs automation in response to something that happens in your repository. You describe the automation in a YAML file. GitHub reads that file, starts a temporary machine, and runs your commands on it.
| Term | What it is | Salesforce example |
|---|---|---|
| Workflow | One YAML file that defines an automated process | "Validate pull request" |
| Event | The occurrence that starts a workflow | A pull request opened against `main` |
| Job | A group of steps that run on one machine | Check-only deploy |
| Step | One command or one reusable action inside a job | `sf project deploy start --dry-run` |
| Runner | The machine that executes a job | An `ubuntu-latest` virtual machine |
| Action | A packaged, reusable step written by someone else | `actions/checkout@v4` |
Where workflows live
GitHub only looks for workflows in the .github/workflows/ directory at the root of the repository. Each .yml or .yaml file there is a separate workflow. A file in any other folder is ignored without an error, which is a common reason for "my workflow never ran".
The workflow file is read from the branch or commit that triggered the run. This means you can change a workflow in a pull request and test it before it reaches main.
A complete first workflow
This workflow has two jobs. It runs when a pull request targets main, and you can also start it by hand from the Actions tab.
name: Hello pipeline
on:
workflow_dispatch:
pull_request:
branches: [main]
jobs:
inspect:
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v4
- name: List the source folder
run: ls -la force-app
report:
runs-on: ubuntu-latest
needs: inspect
steps:
- name: Print the commit being tested
run: echo "Inspection passed for commit ${{ github.sha }}"Read it from the top down.
nameis the label shown in the Actions tab.onlists the events that start the workflow. The next chapter covers triggers in depth.jobsis a map. The keys (inspect,report) are job IDs that you choose.runs-onselects the runner type.stepsis an ordered list. Each item has eitherusesorrun.
Jobs: isolated machines
Every job gets its own fresh runner. Two rules follow from that, and both cause many beginner errors.
- Jobs run in parallel by default. To order them, add
needs: other-job-id. A job withneedsis skipped if the job it needs fails. - Jobs do not share a filesystem. A file written in
inspectdoes not exist inreport. If one job must hand data to another, use job outputs for small values or upload and download an artifact for files.
The fresh machine also means the Salesforce CLI you installed in one job is not there in the next. Each job that calls sf must install it and authenticate again. The chapters on the CLI and on authentication cover how to do this.
Steps: sequential and shared
Steps in the same job run one after another on the same machine. They share the working directory, installed tools, and environment variables set with GITHUB_ENV.
A step does one of two things:
runexecutes a shell command. On Linux runners the default shell isbash, and a step fails if any command in it exits with a non-zero code.usesruns an action, which is reusable code from a repository. You pass inputs to it withwith.
If a step fails, the remaining steps in that job are skipped and the job is marked failed. You can override this with if: always() or continue-on-error: true, but do so deliberately. A pipeline that hides a failed test is worse than no pipeline.
Multi-line commands
YAML offers two block styles, and the difference matters for shell commands.
steps:
- name: Literal block keeps each line as its own command
run: |
echo "first"
echo "second"
- name: Folded block joins lines into one command
run: >
echo
"one long command"With |, every line runs as a separate shell line. With >, YAML folds the lines into a single line with spaces, which is convenient for a long sf command with many flags. Use | for scripts and > for one command split for readability.
Why you need `actions/checkout`
A runner starts empty. It does not contain your repository. The actions/checkout action downloads your code into the working directory. Without it, a command such as ls force-app fails with "No such file or directory".
By default, checkout fetches a single commit. That is fast, but anything that compares history, such as the delta deployment chapter later, needs fetch-depth: 0 to get the full history.
Expressions and contexts
The ${{ ... }} syntax evaluates an expression before the step runs. Inside it you can read contexts, which are objects that GitHub fills in for you.
| Context | Example | Gives you |
|---|---|---|
| `github` | `${{ github.sha }}` | Commit SHA, branch name, event data |
| `env` | `${{ env.TARGET }}` | Variables set in the workflow |
| `secrets` | `${{ secrets.SFDX_AUTH_URL }}` | Encrypted values (covered in the secrets chapter) |
| `needs` | `${{ needs.inspect.result }}` | Results and outputs of earlier jobs |
An if: condition also uses expressions, and you can leave out the ${{ }} wrapper there. For example, if: github.event_name == 'push' runs a step only for pushes.
Runners
A runner is the machine that executes a job. You choose one with runs-on.
- GitHub-hosted runners such as
ubuntu-latestare fresh virtual machines that GitHub provides. They come with Git, Node.js, and common tooling already installed. Usage is billed or counted against free minutes depending on the plan. - Self-hosted runners are machines you manage. You might use one to reach a network that GitHub's machines cannot, but you then own patching and security.
For Salesforce work, ubuntu-latest is the usual choice. The Salesforce CLI talks to your org over the public internet, so a hosted runner is enough.
Reading a run
Open the Actions tab, pick a workflow run, and click a job. Each step is collapsed with its output, timing, and a green check or red cross. When a step fails, expand it and read upward from the last lines. The first error is usually the cause, and later lines are its consequences.
Common mistakes
- The file is in
.github/workflow/(missings) or has a YAML indentation error, so no run appears. - A step uses a file created in a different job.
actions/checkoutis missing, so the project files are not present.- A
needs:value does not match a job ID, which makes the workflow invalid. - A tool is installed in one job and assumed present in another.
Exercise
A workflow has two jobs. Job build installs the Salesforce CLI and writes a file result.txt. Job report, which has needs: build, runs sf --version and cat result.txt. Both commands fail. Give the reason for each failure and one fix for each.
Show answer
Each job runs on its own fresh runner, so nothing from build exists in report. sf --version fails because the CLI was never installed on the report runner. Fix it by adding an install step to report, for example npm install --global @salesforce/cli. cat result.txt fails because the file lives only on the build runner. Fix it by uploading the file as an artifact in build and downloading it in report, or by passing a small value through a job output. The needs setting only controls order. It does not share files or tools.