Secrets in CI/CD Pipelines
What you'll learn: How to safely provide secrets to automated build and deployment processes without leaking them in logs or build artifacts.
The Challenge
Your CI/CD pipeline needs secrets—API keys, database passwords, signing certificates—to build, test, and deploy your application. But pipelines run automatically, often on shared infrastructure, and generate logs viewed by many team members. One careless echo $DATABASE_PASSWORD in a build script could expose credentials to anyone with pipeline access or turn your build logs into a security disaster.
Safe Secret Injection
Modern CI/CD platforms provide secure ways to inject secrets into pipeline runs:
- Masked variables: The platform stores secrets encrypted and injects them as environment variables at runtime, automatically hiding their values in logs (showing
***instead) - Temporary credentials: Generate short-lived tokens specifically for each pipeline run
- Secret management integration: Pull secrets directly from dedicated services (like those covered in lesson 744) during pipeline execution
The key principle: secrets enter the pipeline at the last possible moment and are never written to disk, committed to repositories, or displayed in output.
Best Practices
Never write secrets to files that become build artifacts. Never pass secrets as command-line arguments (they appear in process listings). Always use your CI platform's secret storage features. Configure your pipeline to fail if it detects secret patterns in logs. Limit which pipeline stages can access which secrets—your test stage doesn't need production database credentials.
Think of it like a bank teller accessing a vault: they get temporary access only when needed, for only what they need, and every access is logged.
Key Takeaway: Secrets must flow through CI/CD pipelines using platform-provided secure injection mechanisms that prevent exposure in logs, artifacts, or command history—treat your build environment as semi-trusted.