Sub topic 11
Secrets and how to use them safely
A secret is an encrypted value stored in GitHub and exposed to workflows through the secrets context. Use secrets for tokens, passwords, and keys. This chapter assumes you know the env keyword, the ${{ }} syntax, and if conditions from earlier chapters.
How secrets differ from other values
Configuration variables (the vars context) are plain text and appear in logs. Secrets are encrypted at rest and masked in logs. GitHub replaces a secret's exact value with *** in log output.
You create secrets at the repository, environment, or organization level. If the same name exists at several levels, the most specific one wins: environment, then repository, then organization.
Passing a secret to a step
Read a secret with ${{ secrets.NAME }}. Map it into env and let the shell read the variable. This is the same pattern that protects against expression injection.
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 DEPLOY_REGION value comes from the vars context. The token is set once at job scope, so every step in the job can read $API_TOKEN.
Testing whether a secret exists
You can't write if: secrets.API_TOKEN != '' in a step. The secrets context is not available in step if conditions. Map the secret into env first, then test env.API_TOKEN, as the example above does.
Pull requests from forks don't receive your secrets, except GITHUB_TOKEN, which gets read-only permissions. A step that depends on a secret should expect an empty value, and the if test above handles that case.
Safe habits
- Scope the secret as narrowly as you can. Set it in the step's
envwhen only one step needs it, instead of at workflow scope. - Pass secrets through
env, not by pasting${{ secrets.NAME }}into the script text. - Never print a secret, even for debugging. Masking matches the exact value only. A transformed value, such as base64 or a substring, is not masked.
- Don't store structured data such as JSON as one secret. Masking can fail on parts of it. Store each sensitive value separately.
- Use environment secrets with required reviewers for production credentials.
- Don't use
toJSON(secrets). It dumps every secret into the job.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `if: secrets.X` is rejected or never works | `secrets` is unavailable in step `if` | Map the secret to `env` and test `env.X` |
| Secret is empty in a pull request | The pull request comes from a fork | Expect empty values, or gate the step |
| Secret value shows up in a log | The value was changed before printing | Don't print secrets |
Exercise
A step needs to run only when the secret NPM_TOKEN is set. Why does if: secrets.NPM_TOKEN != '' fail, and how do you fix it?
Show answer
The secrets context is not available in a step if condition. Add env: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} at the job or step level, then write if: env.NPM_TOKEN != ''.