Chapter 12 · Lesson 6 of 10
CI/CD and automation

Secrets and variables in CI pipelines, GitHub Actions guide

CI secrets are encrypted values (tokens, keys, passwords) stored in the CI system and injected into jobs at run time, while variables hold non-sensitive configuration in plain text.

By the end you will

Choose the right scope for each secret and variable, keep values out of logs, and avoid the fork and pull_request_target traps.

Time
15 min
Level
Intermediate
Check
4 questions
You need
  • A GitHub repository with Actions enabled

Pipelines need credentials: a registry token, an SSH key, a cloud login. They also need plain configuration like a hostname or a region. GitHub Actions has separate places for each, and using the wrong one is how credentials end up in logs and forks. This lesson covers the places, the scopes, and the few traps that cause real leaks. The idea of a secret on a server is in Environment variables and secrets.

How it works#

GitHub stores secrets encrypted. When a job runs, it decrypts the ones the job may use and exposes them through the secrets context. Any value registered as a secret is masked in the log, replaced by asterisks, if it appears verbatim. Variables work the same way but are stored in plain text and read through the vars context.

yaml
steps:
  - run: ./deploy.sh
    env:
      API_TOKEN: ${{ secrets.API_TOKEN }}
      TARGET_REGION: ${{ vars.TARGET_REGION }}

Pass a secret to a step through env on that step only. Do not set it at workflow level, where every step and every action can read it.

Scopes: repository, environment, organization#

  • Repository secrets and variables are available to workflows in that repository.
  • Environment secrets and variables belong to a named environment such as production. They are only given to jobs that declare environment: production, and you can require reviewers or restrict branches for that environment. Prefer these for deploy credentials.
  • Organization secrets and variables are shared across selected repositories, so you set them once. Limit which repositories can see them.

The narrowest scope that works is the right one. A production SSH key as an environment secret with a reviewer means a random workflow cannot read it.

Masking and never echoing#

Masking is a safety net, not a plan. It matches the exact value, so a transformed value (base64, split into parts, URL-encoded) is not masked. Do not print secrets, do not run env or set -x in steps that hold them, and do not write them to files that you upload as artifacts. Build tools that print their configuration on error can leak a value without anyone asking them to. If a value is multi-line, such as a private key, register each sensitive line or avoid logging around it.

GITHUB_TOKEN and permissions#

Every run gets a short-lived GITHUB_TOKEN scoped to the repository. Its rights come from the permissions key. Set the minimum:

yaml
permissions:
  contents: read

Add packages: write for pushing images, or pull-requests: write for commenting on pull requests, only in the jobs that need them. You rarely need a personal access token for things the built-in token can do. A narrow token also limits the damage from a compromised third-party action.

OIDC for cloud deploys, in general terms#

A stored cloud key lasts until someone rotates it. OpenID Connect (OIDC) replaces it. The workflow asks GitHub for a signed identity token that states which repository, branch and environment it runs for. The cloud provider is configured to trust that identity, and it exchanges the token for temporary credentials. Nothing long-lived sits in your secrets. To use it, a job needs id-token: write permission, and the cloud side needs a trust rule that names your repository. Most major clouds support this. The exact setup is in each provider’s documentation.

Secrets and pull requests from forks#

For a pull_request run from a fork, secrets are not passed and the token is read-only. This is deliberate: the code in that run is the contributor’s, and it could print anything it can read. Do not look for a way around it. Write tests that need no secrets, and keep deploys on branches in your own repository.

The pull_request_target trap#

pull_request_target runs the workflow file from your base branch with access to secrets and a write token, even for a fork. It exists for tasks like labelling a pull request. The danger is combining it with actions/checkout of the pull request’s head and then running that code, such as npm install or a test script. The attacker’s code now runs with your secrets. Rules that hold up:

  • Do not check out or run untrusted PR code in a pull_request_target job.
  • If you must process PR content, treat it as data, never as code.
  • Keep that workflow’s permissions as narrow as possible.

Check it worked#

Open Settings, then Secrets and variables, then Actions. You should see each credential in the narrowest scope that serves it, with deploy credentials in an environment. Open a recent run log and search for a known secret name: you should find *** masks and no raw values. Confirm each workflow has a top-level permissions block. Finally, open a test pull request from a fork account, if you have one, and check that it passes without secrets.

Edge cases and gotchas#

  • Secrets are not versioned. You cannot read one back in the UI. Keep the source of truth in a password manager so you can rotate it.
  • Variables are not private. Anyone who can read the repository settings can see them. Never put a token in vars.
  • Structured secrets. A JSON blob as one secret is not masked per field. Prefer one secret per value.
  • Third-party actions get your env. An action you pin to @v4 can read what you give it. Pass only what it needs, and review actions you did not write.
  • Re-run keeps old values. A re-run may use the secrets at the time of the re-run, but cached or already-uploaded artifacts keep what they had. Check artifacts for secrets.
  • Public repositories. Logs and artifacts are readable by anyone. Be stricter there.

Troubleshooting#

SymptomLikely causeFix
Secret is empty in a stepFork PR, wrong environment or typo in the nameCheck the trigger, the environment: key and the name
Context access might be invalid: SECRET_NAMESecret not defined in this scopeAdd it to repository or environment secrets
Resource not accessible by integrationToken permission missingAdd the key under permissions
Value shows in the log unmaskedValue was transformed before printingRevoke it, stop printing, avoid transforms
Environment secret not availableJob lacks environment:Add environment: production to the job
Not authorized to perform sts:AssumeRoleWithWebIdentityOIDC trust rule does not match the repo or branchFix the trust condition on the cloud side
Deploy runs from a fork PRWrong triggerUse workflow_dispatch or push on your own branch

Quick reference#

yaml
permissions:
  contents: read
jobs:
  deploy:
    environment: production
    steps:
      - run: ./deploy.sh
        env:
          API_TOKEN: ${{ secrets.API_TOKEN }}
          REGION: ${{ vars.REGION }}
output
leak response: revoke -> replace -> update the secret -> check where it was used

For secrets on the server side, see the blog guide Manage secrets in self-hosted Docker.

Next#

Continue with Cache dependencies in GitHub Actions.

Check what you learned4 questions
1Which context do you use to read a non-sensitive configuration value in a workflow?
2A secret leaked into a public log. What is the first step?
3What is the main benefit of OIDC for cloud deploys?
4Why avoid checking out pull request code in a pull_request_target workflow?

Frequently asked questions

What is the difference between secrets and variables in GitHub Actions?

Secrets are encrypted and masked in logs, and you use them for credentials. Variables are plain configuration values, visible to anyone who can edit the settings, and you read them with the vars context.

Are secrets available to pull requests from forks?

Not for the pull_request event. GitHub does not pass repository secrets to workflows triggered by a pull request from a fork, and the token is read-only.

Why is pull_request_target dangerous?

It runs in the context of the base repository with access to secrets and a write token. If it checks out and runs code from the pull request, an attacker's code runs with those privileges.

What should I do if a secret leaked?

Revoke it at the source first, create a new one, update the CI secret, and then look for where it was used. Deleting the log line or commit does not make the old value safe again.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios