Sub topic 6
Steps: the sequence inside a job
This chapter assumes you can read a workflow file's on, jobs, and runs-on keys. It covers what a step is, how steps share state, and what happens when one fails.
What a step is
A step is one unit of work inside a job. The steps key holds a list, and the runner executes the entries from top to bottom. A step does one of two things:
usesruns an action, which is a packaged, reusable unit of work.runruns a command in a shell.
A step can have either uses or run, but not both.
All steps in a job run on the same runner. They share one workspace directory and one filesystem. A file written by step 2 is visible to step 5. Steps in different jobs do not share anything. Each job gets a fresh runner.
A worked example
name: Steps demo
on: push
jobs:
demo:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Write a file
run: echo "hello" > note.txt
- name: Read it back
run: cat note.txt
- name: Run in another directory
working-directory: /tmp
run: pwd
- name: Set a variable for later steps
run: echo "BUILD_MODE=fast" >> "$GITHUB_ENV"
- name: Use the variable
run: echo "Mode is $BUILD_MODE"Useful step keys:
namesets the label shown in the log.idgives the step a handle, so later steps can read its outputs.working-directorysets where aruncommand starts.shellpicks the shell, such asbashorpwsh.envsets variables for that step only.
Each run step is a new shell
Every run step starts a new shell process. A cd or export in one step is gone by the next. This is the most common surprise at this level. You have two ways to carry state forward:
- Use
working-directoryinstead ofcd. - Append
NAME=valueto the file named by$GITHUB_ENV. Later steps in the same job seeNAMEas an environment variable. The current step does not.
When a step fails
By default, a non-zero exit code fails the step, skips the remaining steps, and fails the job. You can change this with two keys:
if: always()orif: failure()makes a step run even after an earlier failure. Use it for cleanup or for uploading logs.continue-on-error: truelets a step fail without failing the job.
Common errors
- Expecting
cdorexportto last across steps. - Writing
usesandrunin the same step, which is invalid. - Reading a value from
$GITHUB_ENVin the same step that wrote it. - Forgetting
actions/checkout. The workspace starts empty, soruncommands cannot find your code.
Exercise
A step runs export TOKEN_MODE=test. The next step runs echo "$TOKEN_MODE" and prints an empty line. Why, and how do you fix it?
Show answer
Each run step starts a new shell, so the exported variable disappears when the first step ends. Fix it by writing echo "TOKEN_MODE=test" >> "$GITHUB_ENV" in the first step. Later steps in the same job will then see the variable.