Sub topic 10
Configuration Variables with the vars Context
This chapter assumes you can read a workflow file (on, jobs, runs-on, steps) and that you know how env works at workflow, job, and step scope. If not, review those first.
What a configuration variable is
A configuration variable is a named, plain-text value stored in GitHub, outside your workflow file. You read it through the vars context. Use one for non-sensitive settings that change between environments or that several workflows share, such as a deploy region, a build flavor, or a base URL.
Configuration variables differ from the values you have seen so far:
| Kind | Defined | Read with | Sensitive? |
|---|---|---|---|
| `env` variable | In the workflow file | `$NAME` in the shell, `env` context in expressions | No |
| Configuration variable | In GitHub settings | `vars` context | No |
| Secret | In GitHub settings | `secrets` context | Yes, masked in logs |
Because the value lives in settings, you can change it without editing or committing a workflow file. The next run picks up the new value.
Where they are defined
You can define a configuration variable at three levels:
- Repository: available to workflows in that repository.
- Environment: available only to jobs that reference that environment.
- Organization: shared across selected repositories.
If the same name exists at more than one level, the most specific one wins. An environment variable overrides a repository variable, and a repository variable overrides an organization variable.
Reading a variable
Use ${{ vars.NAME }} anywhere an expression is allowed. The expression is replaced with the value before the shell runs.
name: vars-and-secrets
on: push
jobs:
deploy:
runs-on: ubuntu-latest
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
steps:
- name: Show a configuration variable
run: echo "Region is ${{ vars.DEPLOY_REGION }}"
- name: Deploy only when the secret exists
if: env.API_TOKEN != ''
run: ./deploy.shThe first step prints the value of DEPLOY_REGION. The second step is covered in the next chapter on secrets. It maps a secret into env so an if condition can test it.
Passing a variable to the shell
Pasting ${{ vars.NAME }} directly into a run script works for trusted values like a region. For anything a script uses more than once, copy the value into env and read it as a shell variable:
jobs:
build:
runs-on: ubuntu-latest
env:
DEPLOY_REGION: ${{ vars.DEPLOY_REGION }}
steps:
- run: echo "Deploying to $DEPLOY_REGION"This also lets you set a default for a missing variable: ${{ vars.DEPLOY_REGION || 'us-east-1' }}.
Common errors
- Empty value. A variable that does not exist evaluates to an empty string. It does not fail the run. A typo such as
vars.DEPLOY_REGONsilently produces nothing. - Wrong scope. An environment-level variable is empty in a job that does not declare
environment:. - Forked pull requests. Workflows triggered from forks may not have access to your variables.
- Storing secrets. Configuration variables are not masked. Anyone who can read the logs can read them. Keep tokens and passwords in secrets.
- Case. Variable names are case-insensitive in the
varscontext, but convention is uppercase with underscores.
Exercise
A job prints Region is with nothing after it, although you set DEPLOY_REGION as an environment-level variable. Name two likely causes.
Show answer
The job does not declare environment: for that environment, so the environment-level variable is not in scope. Or the name in the workflow is misspelled, and a missing variable evaluates to an empty string instead of causing an error.