Sub topic 7
Secrets and variables
This chapter assumes you can read a workflow file, know what jobs and steps are, and have a repository where you can open Settings. It covers where to keep configuration and credentials so a deploy job can read them without putting them in the repository.
Two kinds of stored values
GitHub Actions has two places to keep values outside your workflow file.
- Secrets hold sensitive data such as API tokens and passwords. They are encrypted, and GitHub masks them in logs. You can write a secret, but you cannot read it back in the web interface.
- Variables hold plain configuration such as a region name, a bucket name, or a site URL. They are stored as readable text and you can edit them at any time.
Use a variable when the value would be harmless on a public page. Use a secret when leaking it would let someone act as you.
| Row | Secret | Variable |
|---|---|---|
| Context | `secrets` | `vars` |
| Visible after saving | No | Yes |
| Masked in logs | Yes | No |
| Typical use | Tokens, passwords | Region, bucket, URL |
Scopes
Both kinds can be defined at three levels.
- Repository. Available to every workflow in one repository.
- Environment. Available only to jobs that name that environment. The next chapters build on this level.
- Organization. Shared by several repositories, with a policy that controls which ones can use it.
If the same name exists at several levels, the most specific one wins: environment over repository, and repository over organization. This lets you define AWS_REGION once and override it for production only.
Reading values in a workflow
Reference a secret with secrets.NAME and a variable with vars.NAME. The first example below passes a value to one step through env, which keeps it out of the command text.
name: Config demo
on:
workflow_dispatch:
jobs:
show:
runs-on: ubuntu-latest
env:
SITE_URL: ${{ vars.SITE_URL }}
steps:
- name: Call the API
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
run: |
echo "Site: $SITE_URL"
curl --fail -H "Authorization: Bearer $API_TOKEN" "$SITE_URL/api/health"Set env at the narrowest level that works. Here SITE_URL is available to every step in the job, while API_TOKEN exists only in the one step that needs it.
Keeping secrets safe
Masking is a convenience, not a guarantee. GitHub hides the exact stored string, so a transformed copy can still appear in a log. Base64 encoding, splitting the value, or printing it with extra characters are examples.
Follow these habits:
- Pass secrets through
envand use them as shell variables. Do not write${{ secrets.NAME }}directly inside arunscript, because the value is pasted into the script text before the shell runs. - Give each secret the least power it needs, and rotate it when someone leaves the team.
- Do not expect secrets in workflows triggered by pull requests from forks. GitHub withholds them, and this is intended.
- Prefer short-lived credentials to stored keys. The OIDC chapter shows how to remove cloud keys entirely.
Common errors
- Empty value. A misspelled name does not fail. The expression evaluates to an empty string, and the failure shows up later as an authentication error. Check the spelling and the scope first.
- Wrong context.
secrets.AWS_REGIONis empty if you saved it as a variable. Matchsecretsto secrets andvarsto variables. - Missing environment. An environment secret is visible only if the job declares
environment: <name>. - Secret in a
if:condition. Thesecretscontext is not available in job-levelif:expressions. Copy it to anenvvariable first, or use a variable.
Exercise
A deploy job reads ${{ secrets.SITE_URL }} and the request goes to an empty address. You saved SITE_URL under Settings, in the Variables tab. What is wrong, and what is the fix?
Show answer
SITEURL is a variable, not a secret, so the secrets context returns an empty string. Change the expression to `${{ vars.SITEURL }}`. Since a URL is not sensitive, keeping it as a variable is correct.
Exercise
The repository defines AWS_REGION as us-east-1, and the production environment defines it as eu-west-1. Which value does a job with environment: production receive, and what does a job with no environment receive?
Show answer
The production job receives eu-west-1, because the environment scope is the most specific. A job with no environment receives the repository value, us-east-1.