Skip to content
Visual explainer 2h to learn 3 steps

Secrets, variables & environments

Gate deployments with environment protection rules.

By Invicly Tech, , in Git, GitHub & GitHub Actions. About 10 minutes to read.

Topic 11 of 18

In short

Secrets hold sensitive values like API keys and are masked in logs; variables hold plain configuration. Environments group secrets for one target and can pause deployments until a reviewer approves.

Explained simply

Workflows often need passwords: an API token to deploy, a key to publish a package. Putting them in the YAML would show them to everyone who can read the repo. Secrets solve this. You save the value in GitHub's settings once, and workflows refer to it by name.

GitHub encrypts secrets, never shows them again in the web page, and hides them in logs. If a secret's value appears in output, the log shows asterisks instead. Secrets are not given to pull requests from forks, so a stranger cannot write a workflow that prints them.

Not everything is secret. The cloud region, a server URL or a feature switch are configuration. Store those as variables: they are readable, show up in logs and are easy to change without editing code.

Environments are named deploy targets like staging and production. Each can have its own secrets and its own rules, such as "a team lead must approve" or "only the main branch may deploy here". The job waits at the gate until the rules are met, and only then receives the production secrets.

Press Play on a diagram to walk through it one step at a time.

CompareSecrets vs variables
Secrets
  • Encrypted, write-only in UI
  • Masked as *** in logs
  • ${{ secrets.NAME }}
  • API keys, tokens, passwords
Variables
  • Plain text, readable
  • Shown in logs
  • ${{ vars.NAME }}
  • Region, URLs, feature switches
SequenceDeploy gated by an environment
Workflow
GitHub
Reviewer
Runner
  1. 1Job uses environment: production
    Workflow to GitHub: Job uses environment: production
  2. 2Approval requested
    GitHub to Reviewer: Approval requested
  3. 3Approve deployment
    Reviewer to GitHub: Approve deployment
  4. 4Start job with prod secrets
    GitHub to Runner: Start job with prod secrets
  5. 5Deployment status: success
    Runner to GitHub: Deployment status: success
CompareWhere to put which value
Org secret
  • Shared by many repos
  • Registry or bot tokens
  • Access policy limits repos
  • Managed by org owners
Repo secret or variable
  • One repo, any workflow
  • CI tokens, test service URLs
  • Not given to forked PRs
  • Managed by repo admins
Environment secret or variable
  • Only jobs in that environment
  • Production credentials
  • Behind reviewers and branch rules
  • Same names per environment

In depth

How secrets are stored and injected

When you save a secret, it is encrypted with a public key before it is stored (gh and the web UI do this for you), and only decrypted when a job that needs it starts. The runner registers each secret value with the log masker, so exact matches are replaced by asterisks. Masking is string matching: if you base64-encode, reverse or split a secret, the new form is not masked. Use echo "::add-mask::$VALUE" to mask a derived value yourself.

Secret names may contain only letters, numbers and underscores, cannot start with a number or with GITHUB_, and are not case-sensitive. Each secret can be up to 48 KB. Secrets cannot be used directly in an if: condition; copy them into env and test the env value instead.

  • Workflows triggered by Dependabot only get Dependabot secrets
  • Forked PRs get no secrets and a read-only GITHUB_TOKEN
  • Pass secrets to commands through env, not inside the command text

Three levels, and which one wins

Secrets and variables can be defined for an organization, a repository or an environment. Organization secrets can be shared with all repos, only private repos, or a selected list, which is ideal for something like a shared package registry token. Repository secrets are for one repo. Environment secrets are only given to jobs that reference that environment and pass its protection rules.

If the same name exists at more than one level, the most specific one wins: environment beats repository, and repository beats organization. A good rule: production credentials belong at environment level only, so no ordinary CI job on a feature branch can ever read them.

LayersSecret lookup, most specific first
  1. 1
    Environment

    Only jobs with environment: NAME, after its rules pass

  2. 2
    Repository

    Any workflow in this repo (not forks)

  3. 3
    Organization

    Repos chosen by the access policy

Variables and the env keyword

Configuration variables are set in the same places as secrets (organization, repository, environment) and read with vars.NAME. They are stored in plain text and printed normally in logs. Do not confuse them with env:, which you write in the YAML at workflow, job or step level, or with the default variables every runner sets, such as GITHUB_SHA, GITHUB_REF and RUNNER_OS.

A useful pattern is the same variable name in each environment. vars.API_URL can be https://staging.api.example.com in staging and https://api.example.com in production, and one deploy job works for both, depending only on which environment it targets. Variables can also be used in if: conditions, which secrets cannot.

Environment protection rules

Required reviewers: up to six people or teams, and one approval is enough; you can also block the person who started the run from approving it. Wait timer: delays the job by up to 43,200 minutes (30 days). Deployment branches and tags: limit which refs can deploy, for example only main or tags matching v*. Custom deployment protection rules let a GitHub App approve or reject, for example based on monitoring.

The job pauses before it starts, so no runner minutes are used while waiting. Approved deployments appear in the repo's Deployments list with history and an optional URL. Plans matter: environments work on every plan for public repos, while private repos need a paid plan, and required reviewers and wait timers in private repos need GitHub Enterprise.

FlowA job that targets production
  1. 1
    Job starts

    environment: production

  2. 2
    Branch rule

    Is this ref allowed to deploy?

  3. 3
    Wait timer

    Optional delay

  4. 4
    Reviewer approves

    One required reviewer is enough

  5. 5
    Secrets released

    Environment secrets and vars

  6. 6
    Deployment recorded

    History and URL on GitHub

Learn it in 3 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Store repo and org secrets~40m
  2. 2
    Use configuration variables~40m
  3. 3
    Add environments with required reviewers~40m

Key points

  • Secrets exist at organization, repository and environment level; the narrowest wins.
  • Environment secrets are only available to jobs that reference that environment.
  • Required reviewers and branch rules make production deploys deliberate.
  • Workflows from forked PRs do not receive your secrets.

Key terms

Tap a card to flip it. 0/4 known

Examples

yaml
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - run: ./deploy.sh --region "${{ vars.AWS_REGION }}"
        env:
          API_TOKEN: ${{ secrets.API_TOKEN }}

Pauses for approval, then gets production's secrets.

Staging deploys automatically, production waits

You want every push to main deployed to staging, and production only after a lead approves. Step 1: create environments staging and production; give production a required reviewer and restrict it to the main branch. Step 2: set API_URL as an environment variable and DEPLOY_TOKEN as an environment secret in each. Step 3: two jobs use the same steps with different environments; production needs staging. Step 4: the environment URL shows as a link on the run page.

yaml
jobs:
  staging:
    runs-on: ubuntu-latest
    environment: { name: staging, url: "${{ vars.API_URL }}" }
    steps:
      - run: ./deploy.sh "$API_URL"
        env: { API_URL: "${{ vars.API_URL }}", TOKEN: "${{ secrets.DEPLOY_TOKEN }}" }
  production:
    needs: staging
    runs-on: ubuntu-latest
    environment: { name: production, url: "${{ vars.API_URL }}" }
    steps:
      - run: ./deploy.sh "$API_URL"
        env: { API_URL: "${{ vars.API_URL }}", TOKEN: "${{ secrets.DEPLOY_TOKEN }}" }

Using a multi-line secret file safely

A deploy needs a kubeconfig file. Step 1: store the file content as a secret from the terminal, so it never lands in shell history or the repo. Step 2: in the job, write it to a file that only the runner user can read. Step 3: point the tool at it with an env variable. Step 4: never cat the file or print it; masking works line by line for multi-line secrets, but derived values like a base64 copy would leak.

bash
# once, on your laptop
gh secret set KUBECONFIG_DATA --env production < ~/.kube/prod-config

# in the workflow step (env: KUBECONFIG_DATA: ${{ secrets.KUBECONFIG_DATA }})
umask 077
printf '%s' "$KUBECONFIG_DATA" > "$RUNNER_TEMP/kubeconfig"
export KUBECONFIG="$RUNNER_TEMP/kubeconfig"
kubectl get nodes

Quick reference

  • gh secret set NAME --env production < file stores a secret from a file
  • gh variable set AWS_REGION --body eu-west-1 creates a repository variable
  • Precedence: environment, then repository, then organization
  • echo "::add-mask::$VALUE" hides a derived value in later logs
  • Secrets max 48 KB each; names cannot start with GITHUB_
  • Use vars.NAME in if: conditions; secrets cannot be used there directly
  • Restrict production to the main branch in the environment's deployment branches
  • Forked PRs and Dependabot runs do not get your normal repository secrets

Common mistakes

  • Echoing transformed secrets, like base64, which are no longer masked.
  • Putting production secrets at repo level, available to every workflow.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1API_KEY exists as an organization secret and as a production environment secret. A job with environment: production reads secrets.API_KEY. Which value does it get?

Question 2

2A workflow prints the base64 form of a secret. What appears in the log?

Question 3

3Which value should be a variable rather than a secret?

Question 4

4A deploy job waits for an environment reviewer. Is it using runner minutes while waiting?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create a repository secret and a variable from the terminal, then use both in a workflow step without printing the secret.

  2. Exercise 2medium

    Run a step only when a secret is configured, so forks and new clones skip it instead of failing.

  3. Exercise 3hard

    Make a production environment that only main can deploy to, needs approval, and gives its URL. Show the workflow job and the settings you would choose.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Can I read a secret's value back from GitHub?
No. The UI and API only let you create, update or delete secrets. If you lose the value, generate a new one at the source and update the secret.
Why is my secret empty in a pull request workflow?
If the pull request comes from a fork, GitHub does not pass secrets to the workflow. Dependabot-triggered runs also only get Dependabot secrets.
Are GitHub secrets safe enough for production keys?
They are encrypted and access-controlled, but any workflow that can read them can leak them. For cloud access, prefer OIDC with short-lived credentials, and keep remaining production secrets at environment level behind reviewers.

Go deeper

ShareXLinkedInWhatsApp