
CI/CD: Automating Testing and Deployment
Ship with confidence. Learn how to use GitHub Actions to automatically run your tests and deploy your code every time you push to Git.
CI/CD: Automating Testing and Deployment
The goal of a modern developer is to automate everything. You should never manually upload code to a server. Instead, you use a CI/CD Pipeline.
- CI (Continuous Integration): Every time you push code, a server automatically runs your tests (Module 14).
- CD (Continuous Deployment): If the tests pass, the server automatically deploys the new version to the cloud.
1. The GitHub Actions Workflow
GitHub Actions is the standard for CI/CD in the Python world. You define your pipeline in a YAML file in .github/workflows/.
name: Python CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: pip install -r requirements.txt
- name: Run tests
run: pytest
2. Secrets in CI/CD
Just like in your app, your CI/CD pipeline needs secrets (like your AWS keys or Docker Hub password). You store these in the GitHub Settings > Secrets page, and they are injected into the workflow at runtime.
3. The "Stage before Production" Pattern
Never deploy directly to your main website.
- Stage 1: Run Tests.
- Stage 2: Deploy to a Staging Environment (a hidden version of your site for testing).
- Stage 3: If Staging looks good, "Promote" it to Production.
4. Zero-Downtime Deployment
Using tools like Blue-Green Deployment or Rolling Updates (standard in Kubernetes), you can update your API without a single second of downtime for your users. The old version stays running until the new version is confirmed healthy.
Visualizing the Pipeline
graph LR
A["Push to Main"] --> B["GitHub Action Starts"]
B --> C["Install Deps"]
C --> D["Run Pytest"]
D -- "Fail" --> E["Email Developer (Fix manual)"]
D -- "Pass" --> F["Build Docker Image"]
F --> G["Deploy to AWS/GCP"]
Summary
- Automation: Removes human error from the deployment process.
- CI: Keeps your main branch always "Healthy."
- Secrets Manager: Securely handles cloud credentials.
- Zero-Downtime: The hallmark of a professional infrastructure.
In the next lesson, we wrap up Module 17 with Exercises on deployment architecture.
Exercise: The Pipeline Guard
You just pushed a piece of code with a major bug.
- If your CI/CD pipeline is set up correctly, will the bug make it to your live website?
- Which part of the pipeline (Install, Test, Build, or Deploy) will catch the error?