In this lesson, you will learn how to securely pass sensitive credentials like API keys or database passwords into your Python application during an automated CI/CD build process without hardcoding them.
The Problem with Local Configuration
Previously, we learned how to use .env files and Pydantic Settings to keep secrets out of version control. However, when your code runs on a CI/CD server (like GitHub Actions or GitLab CI), those local files don't exist. You need a way to populate those environment variables dynamically during the automated pipeline execution.
Using CI/CD Secrets Stores
Modern CI/CD platforms provide "Secret Stores"—encrypted vaults built into the platform interface. You save your sensitive keys in the platform's settings dashboard, labeling them with a name like API_KEY_SECRET. These secrets remain encrypted and are masked in logs, ensuring they are never visible in plain text.
Injecting Secrets into the Environment
When your CI/CD workflow executes your test or deployment scripts, you map these stored secrets to environment variables that your Python application expects. Here is an example of how this looks in a common CI workflow file:
# Example: GitHub Actions configuration
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Run Tests
env:
# Mapping the stored secret to an env var your code uses
DATABASE_URL: ${{ secrets.DATABASE_URL }}
API_KEY: ${{ secrets.MY_APP_API_KEY }}
run: pytest
When pytest runs, your Python code—specifically your Pydantic Settings class—will automatically detect these variables in the process environment. Your application code remains identical to how it runs locally, because it simply looks for os.environ or Settings fields, completely unaware of whether the values came from a local .env file or the CI platform's secure memory.
By leveraging these built-in runners, you ensure that secrets are only injected into the runtime environment for the duration of the job, and they are never written to the disk or committed to your repository.
Key Takeaway: Never store secrets in code; instead, use your CI/CD provider's secret vault to inject credentials as environment variables at runtime, where your application can access them via Pydantic or os.environ.