Sub topic 6
Secrets, variables, and environments
This chapter assumes you can read a GitHub Actions workflow, know what a kubeconfig is, and have a repository with a working build job. Here you will learn where a deploy workflow should keep its configuration and how to gate production with an environment.
Secrets versus variables
GitHub Actions stores configuration in two places.
- A secret is an encrypted value. GitHub masks it in logs and never shows it again after you save it. Use secrets for kubeconfigs, tokens, and passwords.
- A variable is a plain-text value that anyone with access can read. Use variables for non-sensitive settings such as a role ARN, a cluster name, or a namespace.
You read them through different contexts: ${{ secrets.NAME }} and ${{ vars.NAME }}.
| Kind | Context | Visible after saving | Typical use |
|---|---|---|---|
| Secret | `secrets` | No | `KUBE_CONFIG`, API tokens |
| Variable | `vars` | Yes | `AWS_ROLE_ARN`, cluster name |
Scopes
Both secrets and variables can be defined at three levels.
- Organization: shared by many repositories.
- Repository: available to every workflow in one repository.
- Environment: available only to jobs that name that environment.
If the same name exists at several levels, the most specific one wins: environment, then repository, then organization.
What an environment adds
A GitHub environment is a named deployment target, such as staging or production. A job opts in with environment: production. That gives it three things:
- Environment-scoped secrets and variables, so
KUBE_CONFIGcan hold a different cluster's credentials in each environment. - Protection rules, such as required reviewers, a wait timer, or a restriction to certain branches.
- A deployment record in the GitHub UI.
When a job references an environment with required reviewers, it pauses before any step runs. The environment's secrets are released only after someone approves.
Using them in a workflow
The deploy job below reads one environment secret and one environment variable. It passes the secret through env instead of interpolating it into the script.
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
steps:
- name: Write kubeconfig
env:
KUBE_CONFIG: ${{ secrets.KUBE_CONFIG }}
NAMESPACE: ${{ vars.K8S_NAMESPACE }}
run: |
mkdir -p "$HOME/.kube"
printf '%s' "$KUBE_CONFIG" > "$HOME/.kube/config"
chmod 600 "$HOME/.kube/config"
echo "Deploying to namespace $NAMESPACE"Setting the value in env keeps it out of the generated script text. This avoids shell-injection problems and keeps the masking reliable.
Common errors
- Empty secret. If a job lacks
environment:, environment-scoped secrets resolve to an empty string. The step runs and then fails with a confusing kubectl error. - Secrets on forked pull requests. Workflows triggered by a fork do not receive repository secrets. Deploy only from
pushon trusted branches. - Unmasked derived values. Masking matches the exact secret text. If you base64-encode or transform a secret, the new value is not masked, so do not print it.
- Secrets in the wrong place. Putting a role ARN in a secret hides it from your own team and makes debugging harder. Use a variable.
- No branch restriction. An environment without a branch rule lets any branch's workflow request production secrets. Limit it to
main.
Exercise
A deploy job reads ${{ secrets.KUBE_CONFIG }}, which you defined only on the production environment. The job fails with an empty kubeconfig. What is the likely cause, and what is the fix?
Show answer
The job does not declare environment: production, so the environment-scoped secret is not available and resolves to an empty string. Add environment: production to the job, or move the secret to repository scope if it should be shared (not advisable for production credentials).
Exercise
Why is AWS_ROLE_ARN better stored as a variable than as a secret?
Show answer
It is not sensitive, and a variable stays visible in the settings page and in logs. That makes the workflow easier to audit and debug, while secrets are reserved for values that must stay hidden.