CI/CD Pipeline for Microservices

Dynamic multi-service pipeline with GitLab CI, Docker containerization, and Kubernetes orchestration

GitLab CI Docker Python Kubernetes Helm
View on GitHub

Project Overview

This project implements a comprehensive CI/CD pipeline for a microservices architecture, featuring automated building, testing, security scanning, and multi-environment deployments. The pipeline handles multiple Python-based microservices with Docker containerization and Kubernetes orchestration, implementing industry best practices for DevOps automation.

4
Microservices
6
Pipeline Stages
<10min
Build Time
99.9%
Deployment Success

Key Features

Multi-Stage Pipeline

  • Parallel microservice builds
  • Automated testing suites
  • Security vulnerability scanning
  • Container registry publishing
  • Progressive deployments

Security Integration

  • Container image scanning with Trivy
  • SAST with Semgrep
  • Dependency vulnerability checks
  • SBOM generation
  • Secret management

Deployment Strategies

  • Blue-Green deployments
  • Canary releases with Flagger
  • Automated rollback capability
  • Multi-environment support
  • GitOps workflow

Monitoring & Testing

  • Unit & integration tests
  • Contract testing with Pact
  • Performance testing with k6
  • Health checks & readiness probes
  • Coverage reporting

Architecture

GitLab CI → Docker Build → Test Suite → Security Scan → Container Registry → Kubernetes Deployment

Microservices Components

Auth Service

JWT authentication, OAuth integration, session management, and rate limiting

User Service

User profile management, CRUD operations, and file upload handling

Payment Service

Stripe integration, transaction processing, and PCI compliance

Notification Service

Email/SMS notifications, webhook handlers, and message queuing

Pipeline Configuration

GitLab CI Pipeline Stages

# .gitlab-ci.yml
stages:
  - build
  - test  
  - security
  - publish
  - deploy
  - rollback

variables:
  DOCKER_REGISTRY: registry.gitlab.com
  DOCKER_DRIVER: overlay2
  DOCKER_BUILDKIT: 1
  KUBE_NAMESPACE: production

# Build stage with caching
build:auth-service:
  stage: build
  script:
    - docker build --cache-from $LATEST_TAG 
        --tag $IMAGE_TAG 
        --build-arg BUILDKIT_INLINE_CACHE=1 .
    - docker push $IMAGE_TAG

# Comprehensive testing
test:auth-service:
  stage: test
  script:
    - pytest tests/unit -v --cov=app --cov-report=xml
    - pytest tests/integration -v
  coverage: '/TOTAL.*\s+(\d+%)$/'

# Security scanning
security-scan:
  stage: security
  script:
    - trivy image --severity HIGH,CRITICAL $IMAGE_TAG
    - safety check -r requirements.txt
    - bandit -r app/ -f json

# Blue-Green Deployment
deploy-production:
  stage: deploy
  script:
    - helm upgrade --install microservices-green charts/
        --set global.image.tag=$CI_COMMIT_SHORT_SHA
        --wait --timeout 10m
    - kubectl patch service microservices 
        -p '{"spec":{"selector":{"deployment":"green"}}}'

Docker Multi-Stage Build

# Optimized Dockerfile for Python microservices
FROM python:3.11-slim as builder

ENV PYTHONDONTWRITEBYTECODE=1 \
    PYTHONUNBUFFERED=1

RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

COPY requirements.txt .
RUN pip install -r requirements.txt

FROM python:3.11-slim as runtime

COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

WORKDIR /app
COPY . .

USER appuser
EXPOSE 8000

CMD ["gunicorn", "app.main:app", "--workers", "4", 
     "--worker-class", "uvicorn.workers.UvicornWorker"]

Technologies & Tools

GitLab CI/CD
Docker
Kubernetes
Helm Charts
Python/FastAPI
PostgreSQL
Redis
RabbitMQ
Prometheus
Grafana
Elasticsearch
Jaeger

Implementation Steps

1

Repository Structure

Organize microservices in a monorepo with shared configurations and independent service directories

2

Pipeline Configuration

Set up GitLab CI with multi-stage pipeline, environment variables, and secret management

3

Container Registry

Configure GitLab Container Registry for storing Docker images with proper tagging strategy

4

Kubernetes Integration

Connect GitLab to Kubernetes cluster and configure RBAC for automated deployments

5

Monitoring Setup

Deploy Prometheus, Grafana, and Jaeger for comprehensive observability

6

Security Hardening

Implement security scanning, SBOM generation, and compliance checks

Achievements & Benefits

Quick Start

# Clone the repository
git clone https://github.com/jconover/microservices-cicd
cd microservices-cicd

# Start local development environment
docker-compose up -d

# Run tests locally
./scripts/run-tests.sh

# Deploy to staging
git push origin develop

# Deploy to production (requires manual approval)
git push origin main