Sub topic 3
Default environment variables set by the runner
This chapter assumes you can read a workflow file (on, jobs, runs-on, steps) and run basic shell commands. It also assumes you know how env is scoped, from the previous chapter.
What the runner sets for you
Before each step, the runner adds a set of environment variables to the shell. You don't declare them. They describe the run, the repository, and the machine. Their names start with GITHUB_ or RUNNER_, with two exceptions: CI and GITHUB_ACTIONS are both always true.
| Variable | What it holds |
|---|---|
| `GITHUB_REPOSITORY` | Owner and repository name, such as `octo-org/octo-repo` |
| `GITHUB_SHA` | Commit SHA that triggered the run |
| `GITHUB_REF` | Full ref, such as `refs/heads/main` |
| `GITHUB_REF_NAME` | Short ref name, such as `main` |
| `GITHUB_EVENT_NAME` | Event that started the run, such as `push` |
| `GITHUB_ACTOR` | User that triggered the run |
| `GITHUB_RUN_ID` | Unique ID of the run |
| `GITHUB_RUN_NUMBER` | Counter that increases with each run of a workflow |
| `GITHUB_RUN_ATTEMPT` | Counter that increases with each re-run |
| `GITHUB_WORKSPACE` | Directory where `actions/checkout` puts your code |
| `RUNNER_OS` | `Linux`, `Windows`, or `macOS` |
| `RUNNER_TEMP` | Temporary directory that is emptied at the start and end of each job |
Four more variables hold file paths: GITHUB_OUTPUT, GITHUB_ENV, GITHUB_PATH, and GITHUB_STEP_SUMMARY. You write to these files to pass data on. Later chapters cover them.
Reading them in a step
Use normal shell syntax, because these are ordinary environment variables.
name: default-env
on: push
jobs:
show:
runs-on: ubuntu-latest
steps:
- name: Print run details
run: |
echo "Repository: $GITHUB_REPOSITORY"
echo "Commit: $GITHUB_SHA"
echo "Branch: $GITHUB_REF_NAME"
echo "Run: $GITHUB_RUN_NUMBER, attempt $GITHUB_RUN_ATTEMPT"
echo "Runner OS: $RUNNER_OS"
echo "Workspace: $GITHUB_WORKSPACE"The shell expands each $NAME when the step runs. No ${{ }} is involved.
They exist only inside the runner
Default variables exist only in the shell process of a step. Keys that GitHub evaluates before the step starts cannot see them. These keys include if, with, runs-on, and jobs.<id>.env. The env context also does not contain them, so env.GITHUB_REF is empty. In those places, read the matching context property instead: github.ref, github.sha, github.repository, runner.os.
steps:
- name: Only on main
if: github.ref == 'refs/heads/main'
run: echo "Branch is $GITHUB_REF_NAME"The if line uses the github context. The run line uses the shell variable. Both describe the same value, but each is read by a different system.
Common mistakes
- Writing
if: $GITHUB_REF == 'refs/heads/main'. Theifkey is an expression, not shell, so usegithub.ref. - Assuming
GITHUB_RUN_NUMBERchanges on a re-run. It stays the same, andGITHUB_RUN_ATTEMPTincreases. - Storing files in
RUNNER_TEMPand expecting them to last. It is cleaned between jobs.
Exercise
A step contains if: env.GITHUB_EVENT_NAME == 'push', and it never runs even on pushes. Why, and how do you fix it?
Show answer
Default variables exist only in the step's shell. The env context does not include them, so env.GITHUB_EVENT_NAME is empty. Use the context instead: if: github.event_name == 'push'.