Guide

Secrets in CI/CD pipelines: how to handle them safely

Handle CI/CD secrets by giving each pipeline the least access it needs, preferring short-lived credentials over stored keys, exposing a secret only to the step that uses it, and pinning the third-party code that runs beside it. A CI job is a place where untrusted code and valuable credentials meet, so assume anything in the job’s environment can be read.

The examples use GitHub Actions because its documentation is explicit and widely used. The principles apply to any CI system; check your platform’s documentation for the equivalents.

Why are CI pipelines a favorite target?

A runner holds your deploy keys, package-publishing tokens and cloud credentials, and it executes code from many sources: your repository, dependencies, and third-party actions. The recorded incidents show both halves of that risk.

  • In March 2025 an attacker repointed version tags of tj-actions/changed-files to a malicious commit that read secrets from the runner’s memory and printed them into workflow logs. Any workflow that ran the action in the window needed to treat every secret available to that job as exposed.
  • In August 2025 the Nx compromise began when a stolen npm token was used to publish malicious packages. The token had been stolen through a workflow that interpolated pull request titles into a shell step under pull_request_target. Nx later said it moved publishing to npm trusted publishing with OIDC and manual 2FA approval.

Details and affected versions are on the incidents page.

How should you store secrets in the CI platform?

  1. Use the platform’s encrypted secrets, not values in the repository, the workflow file or plain variables.
  2. Scope them narrowly. In GitHub, create secrets at repository, environment or organization level; use environment secrets with required reviewers for production.
  3. Pass a secret to a step through env: only where it is needed, not to the whole job:
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Call the API
        env:
          STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_KEY }}
        run: ./scripts/deploy.sh
  1. Do not interpolate a secret directly into a run: script. GitHub’s guidance is to pass it through env:, because inline interpolation can end up in shell history or error output.
  2. Never echo secrets or print structured data containing them, such as toJSON(secrets).
  3. Do not transform secrets in ways masking cannot detect. GitHub masks the stored value in logs, but notes that a base64-encoded copy is not masked.

Why use OIDC instead of stored cloud keys?

A stored cloud key is a long-lived credential that sits in the CI system until someone rotates it. With OpenID Connect, the job asks the cloud provider for a short-lived token, proving who it is with a signed identity from the CI platform. There is nothing durable to steal from the secrets store. GitHub documents this under security hardening with OpenID Connect, and recommends it where your cloud supports it.

AWS makes the same point from its side: where you can, use IAM roles for temporary credentials instead of access keys on a user (/keys/aws). If you must keep a static key, follow how to rotate API keys.

How do you reduce what a compromised step can do?

  1. Limit the built-in token. Set an explicit permissions: block on the workflow so GITHUB_TOKEN has only the access the job needs. GitHub recommends the built-in token with explicit permissions for automation inside GitHub (/keys/github).
  2. Pin third-party actions to a full commit SHA, not a tag. A tag can be moved; a SHA names fixed code. This is exactly what failed in the tj-actions case.
  3. Be careful with triggers. GitHub does not pass secrets to workflows triggered from forks, except a read-only GITHUB_TOKEN, but pull_request_target runs with secrets. Avoid checking out and running untrusted pull request code under it, and the same applies to workflow_run.
  4. Gate production behind approvals. Environment secrets with required reviewers stop an unreviewed workflow from reaching production keys.
  5. Restrict network egress from runners if your platform allows it. A step that cannot make unexpected outbound calls cannot easily send secrets out. The tj-actions compromise was first noticed through an unexpected outbound call from a workflow.
  6. Use separate credentials per pipeline and per environment. A staging pipeline should never hold production keys.

How do you handle publishing tokens?

Package registries are high-value targets. For npm, the dataset records the guidance to prefer trusted publishing over bypass-2FA tokens for CI publishing (/keys/npm). Trusted publishing replaces a stored token with an identity the registry verifies at publish time. Where that is unavailable, use a granular token limited to the packages it publishes and a short lifetime.

How do you test workflows locally without leaking secrets?

Tools that run workflows locally, such as act, can take secrets from a git-ignored file. Keep that file out of the repository and out of reach of coding agents; see keeping env files away from AI agents. Use test or sandbox keys locally, and keep production keys for the platform.

How do you know when a pipeline secret is compromised?

Look for signs: a deploy you did not run, unfamiliar package versions, new tokens or users, logs containing long encoded strings, or provider alerts. Enable your host’s secret scanning (secret scanning). If you suspect exposure, follow what to do when an API key leaks and rotate every secret the affected job could read, not only the one you noticed.

What is a sensible checklist?

  1. No secrets in the repository or the workflow file.
  2. OIDC or other short-lived credentials wherever the provider allows.
  3. Secrets scoped to environment, with approvals for production.
  4. An explicit permissions: block.
  5. Third-party actions pinned to commit SHAs.
  6. No untrusted code under triggers that carry secrets.
  7. Secret scanning on, plus a scan in CI.
  8. A written, practiced rotation path.

Where does BotPassword fit?

For the developer side of the pipeline, BotPassword keeps keys encrypted and injects a secret into only the command that needs it, so that other steps and tools cannot read the rest. For CI itself, rely on your platform’s secrets and OIDC as described above; BotPassword is not a replacement for them. See the env to BotPassword tool for moving local values out of .env files.

Frequently asked questions

Are GitHub Actions secrets available to pull requests from forks?

Not by default. Secrets are not passed to workflows triggered by pull requests from forks, except a read-only GITHUB_TOKEN, but pull_request_target runs with secrets, so avoid checking out untrusted code there.

Does CI log masking protect my secrets?

Masking helps but is not a guarantee. GitHub notes that transformed values, such as a base64-encoded copy, are not masked, so never print or derive new values from a secret.

What is better than storing a cloud key in CI?

Short-lived credentials obtained through OpenID Connect, where your cloud provider supports it. The workflow requests a token for each run, so there is no long-lived key to leak or rotate.

Why pin third-party actions to a commit SHA?

A tag can be moved to different code. In the 2025 tj-actions/changed-files compromise, version tags were repointed to a malicious commit, and workflows referencing the tags ran it.

More guides

Give agents access. Keep the keys.

BotPassword is in early access. Request an invite and we'll email you when your spot opens.