Secrets & Variables
Pipelines need credentials: a registry password, a deploy token, a signing key. GitHub Actions stores them as secrets (encrypted, masked in logs) and non-sensitive settings as variables. Where you define them decides who can read them.
Where secrets and variables live
| Level | Readable by | Good for |
|---|---|---|
| Organization | repositories the org admin selects (all, private only, or a list) | shared tokens such as a registry pull credential |
| Repository | every workflow in the repository, on any branch, except runs from forks | CI-only credentials (a code-coverage token) |
| Environment | only jobs that declare environment: <name>, after its protection rules pass |
deployment credentials (Environments) |
When names collide, the most specific wins: environment over repository over organization.
jobs:
publish:
runs-on: ubuntu-24.04
environment: production
steps:
- run: ./ci/push.sh "$REGISTRY" "$IMAGE_NAME"
env:
REGISTRY: ${{ vars.REGISTRY }} # a variable: plain text, shown in logs
IMAGE_NAME: ${{ vars.IMAGE_NAME }}
REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }} # a secret: masked in logsRules worth memorising:
- Pass secrets to steps through
env:(orwith:for actions), never by writing${{ secrets.X }}into arun:script. Inside the script, use the variable:"$REGISTRY_TOKEN". - Secrets can't be used directly in
if:. Map one to a job-levelenvand test the variable (if: env.HAS_TOKEN == 'true'), or testvars. - Forks get no secrets, and Dependabot runs get only Dependabot secrets (a separate store).
- The
GITHUB_TOKENis created per job, expires when the job ends, and its scopes come frompermissions:(Hardening Workflows). Prefer it to personal access tokens. - Cloud credentials shouldn't be secrets at all: use OIDC to get short-lived credentials per run (Permissions, OIDC & Pinning).
Set them with the web UI or the CLI (sample session):
gh secret set REGISTRY_TOKEN --env production < token.txt # read from a file: not in shell history
gh variable set REGISTRY --body ghcr.io
gh secret list --env productionMasking is a safety net, not a guarantee
The runner replaces every occurrence of each secret's value in the log with ***. It matches the exact string, plus a few common encodings of it that the runner registers automatically (such as base64 and URL- or JSON-escaped forms). Any other transformation escapes the mask. The session imitates the exact-string filter on a few lines a careless script might print:
secret='s3cr3t-T0ken-42'
mask() { sed "s/$secret/***/g"; } # what the runner does to every log line
{
echo "logging in with $secret"
echo "token length: ${#secret}"
printf '%s' "$secret" | od -An -tx1 | tr -d ' \n'; echo # hex
echo "$secret" | rev
echo "first half: ${secret:0:8}"
printf 'url: https://user:%s@registry.example.com\n' "$secret"
} | masklogging in with ***
token length: 15
7333637233742d54306b656e2d3432
24-nek0T-t3rc3s
first half: s3cr3t-T
url: https://user:***@registry.example.com
Hex, reversed text and a substring all leaked; only the exact value was hidden. Rules that follow:
- Never print secrets, even "just to debug". Use
set +xaround commands that receive them. - If a step derives a new sensitive value (a token fetched at run time, a transformed form), register it with
echo "::add-mask::$VALUE"before anything prints it. - Don't store structured secrets (JSON with many fields): parts of it may be printed without the whole matching the mask. Store each field as its own secret.
- Treat logs as public for public repositories, and as widely readable for private ones.
How secrets actually leak from CI
| Leak path | Defence |
|---|---|
committed to the repository (.env, test fixtures) |
secret scanning on every push (gitleaks); GitHub push protection |
printed in logs (debug output, set -x, error messages) |
no printing; ::add-mask:: for derived values |
| a malicious PR or dependency reads the environment | no secrets for forks; environment secrets only in deploy jobs; pin actions by SHA |
| a compromised third-party action | pin by SHA; minimal permissions; give actions only the inputs they need |
| a long-lived token copied somewhere | OIDC and short-lived tokens; rotation |
artifacts or caches containing credentials (.npmrc, ~/.docker/config.json) |
exclude them from uploads; docker logout at the end of self-hosted jobs |
A leaked secret must be revoked and rotated first; deleting the commit or the log line doesn't help, because it may already have been copied.
Test yourself
A step prints the secret reversed (echo $TOKEN | rev). What appears in the log?
- ***
- The reversed text, unmasked: masking matches the value and a few standard encodings, not arbitrary transformations
- Nothing
- An error
Which secrets can only a job with environment: production read?
- Repository secrets
- Organization secrets
- Environment secrets of production
- All secrets
A secret was committed and pushed an hour ago. What is the first thing to do?
- Force-push to remove the commit
- Revoke and rotate the secret
- Make the repository private
- Delete the CI logs