A lightweight, production-ready Python FastAPI microservice featuring automated testing with pytest, containerization, robust health checking, and a fully automated CI/CD deployment pipeline powered by Jenkins and Docker.
- Backend: Python 3.11, FastAPI, Uvicorn
- Testing: Pytest, HTTPX (TestClient)
- Containerization: Docker, Docker Compose
- CI/CD Automation: Jenkins (Declarative Pipeline)
- Version Control: Git & GitHub
├── main.py # FastAPI application entry point
├── test_main.py # Automated API tests using pytest
├── requirements.txt # Python project dependencies
├── Dockerfile # Docker image configuration for the API
└── jenkins/
└── Jenkinsfile # CI/CD pipeline definition
To run and deploy this project locally using Jenkins and Docker, ensure you have the following installed and configured:
- Docker & Docker Compose installed on your host machine.
- Linux/Debian-based environment (recommended for Docker socket mapping).
Your Jenkins container must have access to the host's Docker daemon to build and deploy containers from the pipeline.
- Docker-in-Docker / Docker-outside-of-Docker (DooD): Configured via volume mounting (
/var/run/docker.sock). - Jenkins Plugins:
- Git Plugin (for repository checkout)
- Pipeline Plugin (for declarative pipeline support)
- Docker Pipeline Plugin (optional, if using agent-level docker blocks)
git clone https://github.com/your-username/your-repo-name.git
cd your-repo-nameStart your local Jenkins server with Docker socket access enabled:
docker compose up --build -dAccess Jenkins at http://localhost:8080.
If you want to run the application manually without Jenkins:
# Install dependencies
pip install -r requirements.txt
# Run the development server
uvicorn main:app --reload --port 8000Open your browser and navigate to http://localhost:8000/docs to view the interactive Swagger documentation.
The pipeline defined in the Jenkinsfile automates the entire software delivery lifecycle through the following stages:
- Checkout SCM: Pulls the latest source code from the GitHub repository.
- Run Tests: Executes automated
pytestsuites to validate application logic. - Build Docker Image: Packages the FastAPI application into a lightweight, optimized Docker container.
- Deploy Container: Automatically stops any legacy container instances and boots up the fresh build on port
8000. - Health Check: Executes an automated internal validation probe (
docker exec) targeting the/healthendpoint to guarantee the application initialised successfully and is ready to accept production traffic. - Post Execution (always): Automatically triggers a clean-up step (
docker image prune) to keep the host disk free of dangling build cache images.
