Sub topic 1
Where Values Come From in a Workflow
Prerequisites
This chapter assumes you can read basic YAML and know the layout of a workflow file: on, jobs, runs-on, and steps. You should be comfortable with shell commands on a Linux runner and with Git and GitHub repositories.
Two places values live
A workflow handles values in two separate places. Confusing them causes most of the errors you will see later in this tutorial.
- Shell environment variables exist inside the runner process. A shell reads them as
$NAME. You set them with theenvkeyword. - Contexts are objects that GitHub holds on its side. You read them in expressions written as
${{ ... }}, for examplegithub.reforrunner.os.
GitHub evaluates expressions first. It replaces each ${{ ... }} with its result, and only then does the runner start the shell. The shell never sees the expression, only the text that replaced it.
The kinds of values
| Kind | Where you define it | How you read it |
|---|---|---|
| Environment variable | `env` in the workflow file, or set by the runner | `$NAME` in a shell, or the `env` context |
| Configuration variable | Repository, environment, or organization settings | `vars` context |
| Secret | Repository, environment, or organization settings | `secrets` context |
| Context value | Supplied by GitHub for each run | Expressions such as `github.sha` |
| Output | A step or job writes it during the run | `steps` and `needs` contexts |
Configuration variables hold plain settings such as a deploy region. Secrets hold sensitive data such as tokens, and GitHub masks them in logs. Outputs carry results from one step or job to another.
Choosing the right one
Ask these questions in order:
- Is the value sensitive? Use a secret.
- Is it a non-sensitive setting shared across workflows? Use a configuration variable.
- Does only one workflow, job, or step need it, and is it fixed? Use
env. - Does GitHub already know it, such as the branch, the actor, or the event name? Read it from a context.
- Is it computed while the workflow runs? Write it as an output.
A first example
This workflow reads a value from each of three sources.
name: value-sources
on: push
env:
GREETING: hello
jobs:
show:
runs-on: ubuntu-latest
steps:
- name: Read three sources
env:
REF: ${{ github.ref }}
run: |
echo "From env: $GREETING"
echo "From a context: $REF"
echo "From the runner: $RUNNER_OS"GREETING is a plain environment variable. REF is a context value that an expression copied into the environment before the shell started. RUNNER_OS is a default variable that the runner sets for you.
What comes next
The following chapters take each source in turn: env scope, default variables, expression syntax, contexts, functions, conditionals, and finally passing data between steps and jobs.
Exercise
A step runs echo "Branch is ${{ github.ref_name }}". Which happens first: the shell reads the line, or GitHub replaces the expression? What does the shell actually receive?
Show answer
GitHub replaces the expression first. The shell receives plain text such as echo "Branch is main", and it never sees the ${{ }} syntax.
Exercise
You need to store an API token and a deploy region used by several workflows. Which kind of value fits each?
Show answer
Store the API token as a secret, because it is sensitive. Store the deploy region as a configuration variable, because it is a non-sensitive setting shared across workflows.