Sub topic 4
GitHub Actions in five minutes
This chapter assumes you know basic Git and GitHub (branches, pull requests, merging), can read YAML, and can use a terminal. You don't need any Salesforce knowledge here. Later chapters add the Salesforce CLI and org credentials to the pieces introduced below.
The working model
GitHub Actions runs automation in response to events in your repository. Four terms cover most of what you will read in a pipeline:
- Workflow: a YAML file in
.github/workflows/. A repository can hold many workflows. - Event: something that starts a workflow, such as a pull request being opened or a push to a branch.
- Job: a group of steps that runs on one fresh machine. Jobs run in parallel unless you order them.
- Step: one unit of work inside a job. It is either a shell command (
run) or a reusable action (uses). - Runner: the machine that executes a job.
ubuntu-latestis a GitHub-hosted Linux virtual machine that is created for the job and discarded afterward.
A minimal workflow
Save this as .github/workflows/hello.yml.
name: Hello pipeline
on:
workflow_dispatch:
jobs:
greet:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Show the files in the repository
run: ls -la
- name: Print context values
run: echo "Running on ${{ github.ref_name }} for ${{ github.actor }}"Read it from the top:
onlists the events.workflow_dispatchadds a manual "Run workflow" button in the Actions tab.jobsholds one job namedgreet.runs-onpicks its runner.stepsrun in order. If one fails, the rest are skipped and the job fails.uses: actions/checkout@v4calls a published action that copies your repository onto the runner.@v4pins a version.${{ ... }}is an expression. It is filled in before the shell sees the command.github.ref_nameandgithub.actorcome from the built-ingithubcontext.
Common errors
- Forgetting checkout. The runner starts empty. Without
actions/checkout, your source files are not there. - Expecting jobs to share files. Each job gets its own runner. To pass files between jobs you must upload and download artifacts. To order jobs, use
needs: other-job. - Indentation mistakes. YAML is whitespace-sensitive. A misplaced
stepsorrunoften shows up as a workflow that does not start or a validation error on the file. - Mixing up
usesandrun.usescalls an action.runexecutes shell text. A step has one or the other, not both.
Exercise
A workflow has two jobs, build and test. The build job creates a file called output.txt. The test job runs cat output.txt and fails with "No such file". Why does it fail, and what two things would you change?
Show answer
Each job runs on its own fresh runner, so test never sees files created by build. Add needs: build to test so it runs after build. Pass the file with artifacts (upload it in build, download it in test), or put both commands in one job.