Skip to content

MichAdebayo/CI-CD-semantic-release

Repository files navigation

πŸš€ CI/CD + Semantic Release β€” FastAPI CRUD App

GitHub Actions Semantic Release Python FastAPI Docker License: MIT

Project Architecture

A production-ready FastAPI CRUD application with automated CI/CD pipelines


πŸ“‹ Table of Contents


✨ Overview

This repository showcases a minimal, production-like FastAPI application with a complete Items CRUD system. It serves as a comprehensive reference for implementing robust CI/CD pipelines with semantic versioning and automation.

What's Inside?

  • 🎯 FastAPI service with SQLModel models and PostgreSQL configuration
  • πŸ—οΈ Clean architecture with separated business logic layer
  • πŸ€– Automated releases using python-semantic-release
  • βœ… Comprehensive CI checks: linting (ruff), testing (pytest), type checking (mypy)
  • πŸ“š Auto-deployed documentation via MkDocs
  • 🐳 Docker support with helper scripts for builds

Perfect for: Learning semantic-release integration with Python and building reliable CI/CD pipelines that automate versioning, changelogs, and publishing.


🎯 Key Features

πŸ›οΈ Architecture

  • Clean FastAPI app with SQLModel + SQLAlchemy
  • Well-separated service layer
  • PostgreSQL for production, SQLite for tests

πŸ”¬ Quality Assurance

  • Full test coverage with pytest
  • Unit and integration tests
  • Type checking with mypy

πŸ€– Automation

  • Semantic-release for versioning
  • Automated changelog generation
  • GitHub Actions CI/CD pipeline

🐳 Deployment

  • Docker containerization
  • GHCR image publishing
  • Trivy security scanning
  • Automated Render deployment

πŸ“ Project Structure

.
β”œβ”€β”€ πŸ”§ .github/
β”‚   └── workflows/
β”‚       β”œβ”€β”€ ci.yml            # CI checks β€” lint, tests, docs
β”‚       β”œβ”€β”€ release.yml       # Automated releases
β”‚       └── cd.yml            # Container builds & deployment
β”‚
β”œβ”€β”€ πŸ“¦ app/
β”‚   β”œβ”€β”€ main.py               # FastAPI application entry point
β”‚   β”œβ”€β”€ database.py           # Database configuration
β”‚   β”œβ”€β”€ logging_config.py     # Centralized logging
β”‚   β”œβ”€β”€ models/               # SQLModel data models
β”‚   β”œβ”€β”€ routes/               # API route handlers
β”‚   β”œβ”€β”€ schemas/              # Pydantic schemas
β”‚   └── services/             # Business logic layer
β”‚
β”œβ”€β”€ πŸ“š docs/                  # MkDocs documentation
β”œβ”€β”€ πŸ§ͺ tests/                 # Test suite (pytest)
β”œβ”€β”€ πŸ“œ scripts/               # Helper scripts
β”œβ”€β”€ 🐳 Dockerfile             # Container definition
β”œβ”€β”€ πŸ“ CHANGELOG.md           # Auto-generated changelog
β”œβ”€β”€ βš™οΈ pyproject.toml         # Project configuration
└── πŸ“– README.md              # You are here!

πŸ› οΈ Tech Stack

Category Technologies
Language Python
Framework FastAPI
Database PostgreSQL SQLite
ORM SQLModel
CI/CD GitHub Actions Semantic Release
Container Docker GHCR
Docs MkDocs
Tools uv Ruff

βš™οΈ Installation

Prerequisites: macOS/Linux with zsh. This project uses uv (Astral) for fast dependency management.

πŸ“₯ Step 1: Clone the Repository

git clone https://github.com/MichAdebayo/CI-CD-semantic-release.git
cd CI-CD-semantic-release

πŸ”¨ Step 2: Install uv (Astral)

curl -LsSf https://astral.sh/uv/install.sh | sh

πŸ“¦ Step 3: Install Dependencies

# Sync all dependencies to virtual environment
uv sync --dev

πŸ” Step 4: Configure Environment (Optional)

# Copy example environment file
cp .env.example .env

# Edit with your values
# DATABASE_URL="SET_YOUR_DB_URL"
# DEBUG_MODE="SET_DEBUG_MODE"

πŸ”§ Configuration

🌍 Environment Variables

Variable Description Required
DATABASE_URL PostgreSQL connection string βœ… Production
DEBUG_MODE Enable FastAPI debug mode ❌ Optional
GITHUB_TOKEN Token for semantic-release βœ… CI/CD

πŸ”’ CI/CD Secrets

Configure these in your GitHub repository settings:

πŸ€– GitHub App Configuration (Click to expand)

Required Secrets:

  • APP_ID β€” GitHub App ID for release workflow
  • APP_PRIVATE_KEY_B64 β€” Base64-encoded private key
  • DEPLOY_URL β€” Render webhook URL
  • GHCR_PAT / GHCR_USER β€” Optional: manual GHCR credentials

Setup Steps:

  1. Navigate to GitHub β†’ Settings β†’ Developer settings β†’ GitHub Apps
  2. Click New GitHub App
  3. Configure permissions: contents: write, packages: write, actions: write
  4. Generate a private key and encode it:
    base64 -i private-key.pem | pbcopy
  5. Add secrets to your repository

⚠️ Security Note: Never commit secrets to the repository. Use .env.example with placeholders only.


▢️ Running the App

πŸš€ Development Server (Hot Reload)

# FastAPI dev mode
uv run fastapi dev app/main.py

# Or with uvicorn directly
uv run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

🐳 Production (Docker)

# Build image
./scripts/docker-build.sh myorg/items:latest

# Run container
docker run -p 8000:8000 \
  --env DATABASE_URL="postgresql://..." \
  myorg/items:latest

🌐 Access the Application


πŸ“‘ API Usage

πŸ“Œ Endpoints Overview

Method Endpoint Description
GET / Root endpoint
GET /health Health check
POST /items Create new item
GET /items List all items
GET /items/{id} Get item by ID
PUT /items/{id} Update item
DELETE /items/{id} Delete item

πŸ’‘ Example Requests

Create Item
curl -X POST http://127.0.0.1:8000/items \
  -H "Content-Type: application/json" \
  -d '{"nom":"Keyboard","prix":49.99}'
Get All Items
curl http://127.0.0.1:8000/items
Get Single Item
curl http://127.0.0.1:8000/items/1
Update Item
curl -X PUT http://127.0.0.1:8000/items/1 \
  -H "Content-Type: application/json" \
  -d '{"prix":59.99}'
Delete Item
curl -X DELETE http://127.0.0.1:8000/items/1

πŸ§ͺ Tests

πŸƒ Running Tests

# Run all tests
uv run pytest -q

# With coverage
uv run pytest --cov=app tests/

# Verbose output
uv run pytest -v

πŸ“Š Test Coverage

The test suite includes:

  • βœ… Unit tests for models and service layer
  • βœ… Integration tests for API endpoints
  • βœ… Edge case handling and error scenarios
  • βœ… In-memory SQLite for fast test execution

πŸ”„ CI/CD Pipeline

πŸ—οΈ Workflow Architecture

graph LR
    A[Push/PR] --> B[CI Workflow]
    B --> C{Tests Pass?}
    C -->|Yes| D[Release Workflow]
    C -->|No| E[Fail]
    D --> F{Main Branch?}
    F -->|Yes| G[CD Workflow]
    F -->|No| H[Dry Run]
    G --> I[Build Docker]
    I --> J[Push to GHCR]
    J --> K[Security Scan]
    K --> L[Deploy to Render]
Loading

πŸ“‹ Workflow Details

Workflow Trigger Actions
CI
ci.yml
Push/PR to
main, develop
β€’ Install dependencies
β€’ Run linting (ruff)
β€’ Execute tests (pytest)
β€’ Type check (mypy)
β€’ Deploy docs (MkDocs)
β€’ Trigger release workflow
Release
release.yml
Called by CI β€’ Authenticate via GitHub App
β€’ Run semantic-release
β€’ Generate changelog
β€’ Create GitHub Release
β€’ Trigger CD workflow
CD
cd.yml
Successful release β€’ Prepare Docker tags
β€’ Build & push to GHCR
β€’ Run Trivy security scan
β€’ Trigger Render deployment

🌿 Branch Strategy

Branch Purpose Release Type
main πŸš€ Production Stable releases + Deploy
develop πŸ§ͺ Development Prereleases
deploy/ci_cd πŸ”§ CI/CD Testing CI/CD prereleases

πŸ” Security & Authentication

  • GitHub App Token: Short-lived, scoped tokens for releases
  • Least Privilege: Minimal permissions for each workflow
  • Secret Management: All credentials in GitHub Secrets
  • Vulnerability Scanning: Trivy scans on every image build

πŸ› οΈ Local Release Testing

# Dry run (shows next version)
./scripts/release.sh

# Publish release (requires GITHUB_TOKEN)
GITHUB_TOKEN=ghp_... ./scripts/release.sh --publish --yes

πŸ—ΊοΈ Roadmap

  • πŸ”— Add contract tests for API endpoints
  • πŸ’¬ Add CI notifications (Slack/MS Teams)
  • πŸ” Implement API authentication
  • πŸ“Š Add monitoring and observability

🀝 Contributing

Contributions are welcome! Here's how to get started:

πŸ“ Guidelines

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Use conventional commits for semantic versioning
  4. Add tests for new functionality
  5. Run linting and formatting
  6. Submit a pull request with clear description

πŸ” Pre-commit Checklist

# Lint code
uv run ruff check app tests

# Format code
uv run ruff format app tests

# Run tests
uv run pytest -q

# Type check
uv run mypy app

πŸ’¬ Commit Convention

feat: add new feature
fix: resolve bug
docs: update documentation
test: add test coverage
chore: maintenance tasks

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

Michael Adebayo

GitHub LinkedIn


⭐ If you find this project helpful, please give it a star! ⭐