TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Secrets & Variables

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.

yaml
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 logs

Rules worth memorising:

  • Pass secrets to steps through env: (or with: for actions), never by writing ${{ secrets.X }} into a run: script. Inside the script, use the variable: "$REGISTRY_TOKEN".
  • Secrets can't be used directly in if:. Map one to a job-level env and test the variable (if: env.HAS_TOKEN == 'true'), or test vars.
  • Forks get no secrets, and Dependabot runs get only Dependabot secrets (a separate store).
  • The GITHUB_TOKEN is created per job, expires when the job ends, and its scopes come from permissions: (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):

bash
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 production

Masking 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:

bash · try it yourself
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"
} | mask
logging 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 +x around 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?

  1. ***
  2. The reversed text, unmasked: masking matches the value and a few standard encodings, not arbitrary transformations
  3. Nothing
  4. An error

Which secrets can only a job with environment: production read?

  1. Repository secrets
  2. Organization secrets
  3. Environment secrets of production
  4. All secrets

A secret was committed and pushed an hour ago. What is the first thing to do?

  1. Force-push to remove the commit
  2. Revoke and rotate the secret
  3. Make the repository private
  4. Delete the CI logs