Course contentsShow
← AI Engineering
Lesson 1482 of 1,88636. Security in AI SystemsFree lesson

Secrets in CI/CD Pipelines

Securely injecting API keys into GitHub Actions, GitLab CI, and other automation workflows.

Secrets in CI/CD Pipelines

You'll learn how to securely inject API keys and credentials into GitHub Actions, GitLab CI, and other automation workflows without exposing them in code or logs.

Why This Matters

CI/CD pipelines automate your testing, building, and deployment—but they often need access to the same sensitive API keys your application uses. Think of CI/CD as a robot assistant that needs house keys to do its job, but you don't want those keys lying around where anyone can find them.

The Core Problem

Hard-coding secrets in .github/workflows/deploy.yml or .gitlab-ci.yml files exposes them in version control forever. Even deleted commits remain in Git history. Similarly, printing secrets in logs during builds makes them visible to anyone with pipeline access.

Secure Injection Methods

GitHub Actions uses encrypted secrets stored in repository or organization settings. Reference them as environment variables:

env:
  OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

GitLab CI/CD provides masked and protected variables in project settings:

deploy:
  script:
    - export API_KEY=$OPENAI_API_KEY

Best practices:

  • Store secrets in platform-specific secret stores, never in code
  • Use masked secrets to prevent accidental logging
  • Limit secret access to specific branches (e.g., only main)
  • Rotate keys periodically and update CI/CD secrets accordingly
  • Grant minimum necessary permissions per environment (staging vs production keys)

Pipeline-Specific Considerations

Many platforms automatically redact recognized patterns in logs, but don't rely solely on this. Avoid echo $SECRET or similar debugging commands. Use scoped keys (as covered in "Scoped and Limited-Privilege Keys") so compromised CI secrets have limited blast radius.

Key Takeaway: Treat CI/CD pipelines as privileged environments—use platform secret managers to inject credentials dynamically, never commit them to version control, and configure masking to prevent accidental exposure in build logs.