Sub topic 1
Steps, run commands, and actions
Prerequisites
This chapter assumes you can do the following:
- Use Git and GitHub: repositories, branches, and pull requests.
- Read and write YAML.
- Recognize the parts of a workflow file: events, jobs, and steps.
You do not need to have used any Marketplace actions yet.
What a step is
A job in a workflow is a list of steps. The steps run one after another on the same runner, which is the machine that executes the job. Because they share the runner, they share its file system. A file that one step writes is visible to the next step.
Each step does exactly one of two things:
runexecutes a command in a shell on the runner.usesruns an action, which is a reusable unit of automation that someone else wrote and published.
A single step cannot have both run and uses.
Run steps
A run step is a shell command. Use it for anything you could type in a terminal, such as installing dependencies or running tests.
steps:
- name: Print a greeting
run: echo "Hello from the runner"
- name: Run several commands
run: |
node --version
npm --versionThe | character starts a multi-line block. All lines in the block run in the same shell.
A run step is simple and fully visible in your workflow file. The limitation is that it only does what the commands on that runner can do. Checking out your code, installing a specific Node.js version, or restoring a cache takes many lines of careful scripting. Actions package that work.
Action steps
A uses step points to an action. The action runs its own code, and you configure it with the with keyword. Here is a complete workflow that mixes both kinds of step.
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- run: npm testRead it from top to bottom:
actions/checkout@v4copies your repository onto the runner. A fresh runner starts with an empty workspace, so without this stepnpm ciwould find nopackage.json.actions/setup-node@v4installs Node.js 20. Thewithblock passes inputs to the action. Herecache: npmalso tells it to cache npm's download directory.npm ciandnpm testare ordinary shell commands. They rely on the work the two actions already did.
The actions/ prefix means these two actions are maintained by GitHub. The part after @ selects a version. Later chapters cover how to reference actions and how to choose versions.
How a job flows
The diagram shows how the steps in the example run on a single runner.
If any step fails, the later steps are skipped by default and the job fails. You can change this with an if condition on a step, which later chapters use.
Choosing between run and uses
| Situation | Better choice |
|---|---|
| A short command you already know, such as `npm test` | `run` |
| A task with many edge cases, such as installing a toolchain or caching | `uses` |
| You need to audit every line that executes | `run`, or an action you have reviewed |
| You want to receive updates and fixes from the maintainer | `uses` |
An action saves you effort, but it also runs someone else's code on your runner. It may be able to read your repository contents and your secrets. A run step contains only what you wrote. That trade-off is why this tutorial spends several chapters on vetting, pinning, and permissions.
Common mistakes at this stage
- Putting
runandusesin the same step. GitHub rejects the workflow file as invalid. - Forgetting
actions/checkout. Later steps then cannot find your files. - Placing
withon arunstep. Onlyusessteps take inputs. - Assuming a step's changes persist between jobs. Each job gets a new runner, so only steps inside the same job share files.
Exercise
A workflow job has these steps in order: run: npm test, then uses: actions/checkout@v4. The job fails with "package.json not found". What is wrong, and how do you fix it?
Show answer
The tests run before the repository has been copied to the runner, so the workspace is empty. Move the actions/checkout@v4 step to the top of the job so it runs before npm test.
Exercise
Why can a uses step have a with block but a run step cannot?
Show answer
with supplies inputs to an action, and a run step is only a shell command with no declared inputs. To pass values to a command, use the env keyword or write them in the command.