Secrets, variables and contexts
Give a workflow credentials and settings safely: secrets, variables, the GITHUB_TOKEN, and the contexts behind every ${{ }} expression.
Workflows, triggers, jobs and stepsRunners and the marketplace
Secrets, variables and contexts
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A secret is an encrypted value stored in GitHub and exposed to a workflow as secrets.NAME, while a variable is a plain configuration value exposed as vars.NAME.
- Secrets can be defined for a repository, an environment or an organization, and environment secrets can require a reviewer's approval before a job gets them.
- Secrets are not passed to workflows triggered from forked repositories, apart from the GITHUB_TOKEN, and they cannot be used directly in if conditions.
- Every ${{ }} expression reads from a context such as github, env, vars, secrets, matrix, needs or steps, and where each context is available is restricted.
- GitHub masks secrets in logs only when it recognises the exact value, so never print them and never derive values from them without masking.
Two kinds of settings
Almost every real workflow needs values that are not in the repository. A deployment needs a credential. A build might need to know which region to target. You cannot hard-code the first, because committing a credential is a leak. And you do not want to edit YAML every time the second changes.
GitHub gives you two stores for this.
- Secrets are for sensitive values. They are encrypted, and once created you cannot read them back. You can only replace or delete them. In a workflow they are
secrets.NAME. - Variables are for configuration that is not sensitive. You can read and edit them in the settings. In a workflow they are
vars.NAME.
If you would be fine with a stranger reading the value, it is a variable. Otherwise it is a secret.
Creating them
Both live under Settings, then Secrets and variables, then Actions. There is a Secrets tab with New repository secret, and a Variables tab with New repository variable. You need write access to the repository to add either. I took these paths from GitHub’s docs, read on 2026-11-23, and could not use them, because I have no GitHub account where I wrote this lesson.
There are three scopes.
- Repository values are available to workflows in that repository.
- Environment values belong to a named environment, such as
production. A job that setsenvironment: productiongets them. The docs describe required reviewers for environments, so a deployment pauses until someone approves it. That makes an environment the right place for production credentials. - Organization values can be shared with chosen repositories. The docs add that on GitHub Free, organization secrets are not accessible to private repositories.
From the terminal, gh secret set NAME and gh variable set do the same. The GitHub CLI lesson covers the tool.
Using them in a workflow
name: Deploy notes site
on:
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
env:
TARGET_REGION: ${{ vars.TARGET_REGION }}
steps:
- uses: actions/checkout@v7
- name: Show configuration
run: echo "Deploying to $TARGET_REGION as run ${{ github.run_number }}"
- name: Publish
env:
DEPLOY_TOKEN: ${{ secrets.DEPLOY_TOKEN }}
run: ./scripts/publish.sh
The YAML parses, but I could not run it, since it needs GitHub, so scripts/publish.sh is a stand-in and I have not seen the output. A few points about it.
- The secret is put into an environment variable of one step, not the whole job. Fewer steps can see it.
- The script reads
$DEPLOY_TOKEN. The docs advise against passing secrets as command-line arguments, because other processes on the machine can see them. environment: productionis what unlocks environment-level secrets and reviewer approval.- An unset secret or variable evaluates to an empty string, not an error. A typo in a name fails quietly, so test it.
What is a context
The ${{ ... }} syntax is an expression. GitHub evaluates it before the step runs and substitutes the result. Expressions read from contexts, which are objects holding information about the run. From GitHub’s contexts reference:
| Context | What it holds |
|---|---|
github |
The run and the event: github.event_name, github.ref_name, github.sha, github.actor, github.repository, and the full github.event payload |
env |
Variables you set at workflow, job or step level |
vars |
Configuration variables from the settings |
secrets |
Secrets, including the automatic GITHUB_TOKEN |
needs |
Outputs of jobs this job depends on |
steps |
Outputs and results of earlier steps that have an id |
matrix, strategy |
Values for the current matrix combination |
runner |
The runner’s OS, architecture and temp path |
inputs |
Inputs to a manual or reusable workflow |
job |
Information about the current job |
Which context you can use depends on where in the file you are. matrix is not available in a job-level if, for example, and secrets is not available in if at all. The docs have a table of availability, and an error such as “Unrecognized named-value” usually means you used a context somewhere it does not exist.
Do not confuse expressions with shell variables. ${{ github.sha }} is replaced by GitHub before the shell starts. $HOME is expanded by the shell afterwards. That difference is the source of the injection problem in the hardening lesson, where text from a pull request title is pasted into a script.
The GITHUB_TOKEN
Every run automatically gets a token, secrets.GITHUB_TOKEN, also available as github.token. It is created for the run, lasts until the run ends, and acts as the workflow on this repository only. Its power is set by the permissions key. In the examples so far, permissions: contents: read gave it read-only access. For a job that comments on pull requests you would add pull-requests: write to that job.
This is why you rarely need a personal access token in a workflow. It is also why forked pull requests are restricted. The docs say the GITHUB_TOKEN is read-only for pull requests from forks, and other secrets are not passed at all.
Rules to keep secrets secret
GitHub redacts a secret’s value from the logs, but only when it recognises the exact string. Several of its guidance points follow from that, from the hardening guide I read on 2026-11-23.
- One value per secret. Do not store a JSON or YAML blob as a single secret, because a part of it will not be matched when printed. Store each sensitive value separately.
- Mask derived values. If a script turns a secret into something else, such as a signed token, register it with
::add-mask::VALUE, or it will be shown. - Never
echoa secret to debug. Redaction can fail, for example when the value is transformed. - Environments for production. Secrets that can deploy belong in an environment with required reviewers.
- Rotate. If one appears in a log, delete the log and replace the secret.
Better still, avoid long-lived secrets. Deploying with OIDC lets a workflow obtain a short-lived cloud credential with nothing stored at all.
What the video covers
Mickey Gousset’s “Introduction to GitHub Actions - Part 3 - The GitHub Context Object and Variables” is about 23 minutes and was published around 2025 (YouTube shows “1 year ago”). I know its contents from the title only, since the search snippet did not show a description, and I could not read a transcript or chapters. It should cover the github context and variables, which is the contexts half of this lesson. Secrets, environments and masking are covered better by the docs and by Mickey’s separate “Masking Variables and Secrets” video, linked under deeper, alongside a 2025 DevTips Daily video on secrets. Mickey Gousset works at GitHub, and the video is on his own channel. English is judged from the title and channel. It replaces a small-channel pick from the first draft.
Where this fits
Your workflows can now read settings and credentials. The next lesson makes them faster and wider: caching, artifacts and matrix builds.
Check yourself
When should a value be a variable and not a secret?
When it is not sensitive, such as a region name, a build mode or a feature flag. Variables are readable and editable in the settings and their values appear in logs, while secrets are write-only after creation and are masked.
Why does a pull request from a fork not get your repository's secrets?
Otherwise anyone could open a pull request that edits the workflow to print or send your secrets. GitHub passes only a read-only GITHUB_TOKEN to those runs.
What is the GITHUB_TOKEN?
A short-lived token GitHub creates for each workflow run, scoped to the repository, whose permissions you control with the permissions key. It lives only as long as the run.
You write if: secrets.API_KEY != '' and it never works. Why?
The secrets context is not available in if conditions. Put the secret in an env variable on the job and test that variable in a shell step, or restructure the logic.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.