CI/CD Pipeline for Python Applications

Jan 15, 2025 min read

Overview

A complete CI/CD pipeline implementation for Python web applications, demonstrating modern DevOps practices. This pipeline automates testing, linting, Docker image building, and deployment to production servers.

Pipeline Features

  • Automated Testing: Runs pytest on every push and pull request
  • Code Quality: Flake8 linting and code style enforcement
  • Matrix Testing: Tests across Python 3.10, 3.11, and 3.12
  • Docker Build: Automated container image builds with caching
  • Deployment: Automated SSH-based deployment to production servers
  • Secrets Management: Secure handling of credentials and API keys

Workflow Stages

1. Test Stage

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ['3.10', '3.11', '3.12']
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run tests
        run: pytest --cov=./

2. Build Stage

  build:
    needs: test
    steps:
      - name: Build Docker image
        run: docker build -t app:${{ github.sha }} .
      - name: Push to registry
        run: docker push app:${{ github.sha }}

3. Deploy Stage

  deploy:
    needs: build
    if: github.ref == 'refs/heads/main'
    steps:
      - name: Deploy via SSH
        uses: appleboy/ssh-action@v1.0.0
        with:
          host: ${{ secrets.HOST }}
          script: docker-compose up -d

Technologies Used

ComponentTechnology
CI/CD PlatformGitHub Actions
ContainerizationDocker, Docker Compose
Testingpytest, pytest-cov
Lintingflake8, black
DeploymentSSH, systemd
SecretsGitHub Secrets

Key Achievements

  • Zero-downtime deployments using Docker container orchestration
  • 90%+ test coverage enforced through CI pipeline
  • Automated rollback capability on failed deployments
  • Build time optimization with Docker layer caching

Real-World Application

This pipeline architecture is used in my projects: