Skip to main content
Using Secrets in GitHub Actions Safely

Using Secrets in GitHub Actions Safely

KU
· Founder & Chief Editor
• 8 min read•Updated •55 views

A practical guide to scoping GitHub Actions secrets correctly, avoiding leaks in logs, blocking fork-PR theft, and preferring OIDC over stored keys.

Key takeaways

  • Store each GitHub Actions secret at the narrowest scope that works, pass it to steps through env instead of interpolating it into run, and never print it.
  • Use pull_request, not pull_request_target, for fork contributions.
  • Gate production secrets behind an environment with required reviewers, and replace stored cloud keys with OIDC.

Use GitHub Actions secrets safely by scoping each one as narrowly as possible (environment over repository over organization), passing it to steps through env: rather than inline ${{ }} in run, never running fork code under pull_request_target, and putting production secrets behind an environment with required reviewers. For cloud access, use OIDC and store no keys.

Verified against

GitHub Actions with actions/checkout v7.0.1, against the docs.github.com pages on using secrets, events that trigger workflows, and deployments and environments. Checked October 2026.

Every CI pipeline eventually needs a credential: a registry token, a database password, a cloud key. The store is fine. The trouble is that the defaults are permissive and the masking is shallower than most people assume, so it is easy to build a workflow that quietly hands your production credentials to code you have never read. This is the guide we wish every new engineer got before they wrote their first deploy.yml.

Three scopes, three jobs

GitHub secrets live at three levels, and picking the wrong one is the most common structural mistake.

Repository secrets: available to every workflow in a single repo. Good for things that belong to exactly that project and nothing else, like a project-specific API token.

Environment secrets: attached to a named environment (staging, production) and only readable by a job that declares environment: production. This is the level that lets you gate access with protection rules, which we will come back to.

Organization secrets: shared across many repos, with a visibility setting that can be all repos, private repos only (the gh secret set --org default), or an explicit list of selected repos. Great for a shared package-registry token. Dangerous when you set visibility to "all repositories" and forget, because a brand-new repo inherits it on day one.

The rule we follow: use the narrowest scope that works. Reach for org secrets only when the same credential genuinely belongs to many repos, and always pin visibility to a selected list. One hard limit to know: a single secret tops out at 48 KB, and GitHub's documented workaround for anything bigger is a gpg-encrypted file in the repo with only the passphrase stored as a secret.

Referencing secrets correctly

Secrets are exposed through the secrets context. Pull them into env at the step or job level rather than sprinkling ${{ }} through your shell commands.

YAML
# .github/workflows/publish.yml
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - name: Publish
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npm publish

Notice what this does not do. It never writes ${{ secrets.NPM_TOKEN }} directly inside the run block. When you interpolate a secret into the command string, GitHub substitutes the raw value before the shell ever sees it, so the value can land in set -x trace output, show up in the process list, or break parsing if it contains quotes or backticks. GitHub's own docs warn that secrets "often contain special characters that may unintentionally affect your shell" and recommend environment variables or STDIN over the command line. Passing through env hands the tool the value via the environment and keeps it out of the command text.

Masking is real, but shallow

GitHub automatically registers every secret value and replaces it with *** in logs. That protects you from the obvious case of a tool printing a token. It does not protect you from yourself, because masking only matches the exact stored string. Secrets still leak when:

  • You echo a secret on purpose to "debug" it. Even masked, related derived values around it can expose structure.
  • The secret is multiline and a tool prints one line at a time; partial lines may not match the mask.
  • You transform it. echo $TOKEN | base64 produces a new string GitHub has never seen, so it is printed in full.
  • You place it in structured output like JSON that a later step parses and re-emits.

Treat the mask as a seatbelt, not a reason to drive into a wall. The only reliable rule is: never deliberately print a secret. If you must confirm one is set, print its length, not its value.

The forked-PR trap

This is the one that has burned real companies. There are two triggers that run on pull requests, and they are not interchangeable.

pull_request runs the workflow in a restricted context. For PRs from forks, secrets are not available and the GITHUB_TOKEN is read-only. This is exactly what you want when a stranger opens a PR: their code runs, but it cannot touch your credentials.

pull_request_target runs in the context of the base repository's default branch, with secret access and a writable token, and by default it checks out that default branch rather than the PR code. GitHub's docs warn that running untrusted code on this trigger can lead to cache poisoning and unintended secret access. Teams reach for it to get secrets into PR workflows, then add actions/checkout with the PR's head ref, and now untrusted fork code is executing with your production secrets in scope. That is a full credential-theft primitive, handed to anyone on the internet who can open a PR.

The anti-pattern to never ship:

YAML
# .github/workflows/pr-build.yml  (DO NOT DO THIS)
on: pull_request_target
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
        with:
          ref: ${{ github.event.pull_request.head.sha }}  # untrusted code
      - env:
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        run: ./scripts/build.sh  # attacker controls build.sh

For untrusted contributions, use pull_request, keep secrets out of that path, and if a fork PR genuinely needs a privileged step, split it: run the untrusted build on pull_request, then run the privileged part in a separate workflow_run job that never checks out fork code. The broader set of controls (pinning actions to SHAs, least-privilege permissions:) lives in our GitHub Actions security hardening checklist.

Environment protection rules

Environment secrets earn their keep because they can require human approval. Put production credentials behind an environment with required reviewers, and a deploy job pauses until someone signs off before the secret is ever exposed.

YAML
# .github/workflows/release.yml
# Repo Settings > Environments > production:
#   required reviewers: platform-team
#   deployment branches: main only
jobs:
  release:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v7
      - name: Deploy
        env:
          DEPLOY_KEY: ${{ secrets.DEPLOY_KEY }}
        run: ./deploy.sh

Because the job declares environment: production, the run stops at that job until a listed reviewer approves it (you can list up to six users or teams, and one approval is enough), and the job cannot read the environment's secrets until then. The branch filter stops the environment from being used off a random feature branch. That single gate turns an accidental push into a blocked deploy instead of a live one. Check your plan first: as of October 2026, required reviewers on private repositories need GitHub Enterprise; on Free, Pro and Team they work only on public repos.

The bigger move: stop storing keys at all

Everything above manages the risk of a stored secret. The better play is to not store long-lived cloud credentials in the first place. GitHub can present a signed OIDC token that AWS, GCP, or Azure will exchange for short-lived credentials at runtime. Nothing static sits in your secrets store, so there is nothing to leak, rotate, or accidentally echo into a fork's log.

We walk through the full setup, including the trust-policy scoping that stops other repos from assuming your role, in keyless deploys to AWS via OIDC. If you are still copying access keys into GitHub Secrets, that post is the upgrade to make first.

For the wider set of patterns this fits into, see our GitHub Actions recipes.

Questions people ask

Can a pull request from a fork read my GitHub Actions secrets?

Not under the pull_request trigger. GitHub does not pass secrets to runners for workflows triggered from a fork, except GITHUB_TOKEN, and that token is read-only. The risk is pull_request_target, which runs in the base repository's context with secrets available. If that workflow checks out and executes the PR's head commit, the fork's code runs with your secrets.

Does GitHub mask secrets in Actions logs?

Yes, any registered secret value that appears verbatim in a log is replaced with ***. Masking only matches the exact stored string, so a base64-encoded, split, or otherwise transformed value prints in full. For values that are sensitive but not stored as secrets, register them with ::add-mask::VALUE. Treat the mask as a backstop and never print secrets deliberately.

Should I use environment secrets or repository secrets?

Use environment secrets for anything that deploys or touches production. Only a job that declares the environment can read them, and with required reviewers the job cannot access them until someone approves. Repository secrets suit low-risk values every workflow in that repo needs, such as a linter token. If a credential is shared across many repos, compare dedicated stores in Vault vs Secrets Manager vs Doppler.

The call we'd make

Scope every secret as narrowly as it will go, and pin org-secret visibility to an explicit list. Pass secrets through env, never into run interpolation, and never print them. Use pull_request, not pull_request_target, for anything touching untrusted forks. Gate production secrets behind an environment with required reviewers. And for cloud access, skip stored keys entirely and go OIDC. The safest secret is the one that does not exist to be stolen.

Sources

React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). Using Secrets in GitHub Actions Safely. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/github-actions-secrets-guide

Markdown

[Using Secrets in GitHub Actions Safely](https://www.devopsness.com/blog/github-actions-secrets-guide) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

586 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.