Course contentsShow
← Python
Lesson 1040 of 1,06016. Capstone and AutomationFree lesson

Managing Secrets and Environment Variables

Configure secure storage for API keys and tokens within CI/CD pipelines to protect sensitive credentials during automated deployments.

Managing Secrets in CI/CD Pipelines

In this lesson, you will learn how to inject sensitive credentials into your automated deployment pipelines without hardcoding them into your source control.

When your Python applications are deployed via CI/CD tools like GitHub Actions, they often require access to production databases, cloud APIs, or payment gateways. Storing these credentials as plain text in your code—even in private repositories—is a security risk. Instead, you must use Secret Stores provided by your CI/CD platform.

Configuring Secure Storage

Modern CI/CD platforms provide "Secret" management interfaces where you define key-value pairs (e.g., DATABASE_PASSWORD). These values are encrypted at rest and masked in logs.

To use these in your automation, you map them to environment variables within your pipeline configuration file. For example, in a workflow.yml file, you map a secret to an environment variable:

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Deploy application
        env:
          # Mapping the secret to an environment variable
          DATABASE_PASSWORD: ${{ secrets.PROD_DB_PASSWORD }}
        run: python deploy_script.py

Accessing Secrets in Python

Once the environment variable is injected into the runner's shell, your Python code accesses it using the os module. By centralizing this access, your code remains portable and secure across different environments (development vs. production):

import os

def connect_to_database():
    # Fetch the secret via the environment variable
    password = os.getenv("DATABASE_PASSWORD")
    if not password:
        raise EnvironmentError("Missing required DATABASE_PASSWORD")
    # Proceed with connection logic...

By keeping secrets out of your repository and injecting them as environment variables only during the execution phase, you ensure that even if your codebase is compromised, your actual credentials remain protected. Always treat environment variables as transient; never log the values of your secrets, as the CI/CD platform will mask them automatically only if they are correctly designated as "secrets" in the UI.

Key Takeaway: Never commit credentials to version control; instead, store them in your CI/CD provider’s secret vault and expose them as environment variables during your automated runtime.