Production-ready FastAPI application with automated CI/CD pipeline, Docker containerization, and future AWS EC2 deployment support.
golden-cicd-python/
βββ app/
β βββ __init__.py
β βββ main.py # FastAPI application
βββ tests/
β βββ __init__.py
β βββ test_main.py # Pytest test suite
βββ scripts/
β βββ deploy.sh # Deployment script (local + EC2 ready)
βββ .github/
β βββ workflows/
β βββ ci-cd.yml # GitHub Actions pipeline
βββ Dockerfile # Multi-stage Docker build
βββ requirements.txt # Python dependencies
βββ .env.example # Environment variables template
βββ README.md
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β PUSH TO MAIN β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 1: Checkout Code β
β β Clone repository β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 2: Setup Python 3.11 β
β β Install Python environment β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 3: Install Dependencies β
β β pip install -r requirements.txt β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 4: Run Tests (pytest) β
β β Test / endpoint (200 OK) β
β β Test /health endpoint (status: OK) β
β β FAIL β Pipeline stops here β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 5: Build Docker Image β
β β docker build -t golden-cicd-python:latest . β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 6: Run Container & Verify β
β β docker run -d -p 8000:8000 golden-cicd-python β
β β curl http://localhost:8000/health β
β β Stop container after validation β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β STEP 7: Save Docker Image (Simulated Deployment) β
β β docker save | gzip > golden-cicd-python.tar.gz β
β β Upload as GitHub artifact β
ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
PIPELINE COMPLETE β
β Ready for EC2 deployment when configured β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Python 3.11+
- pip
- Clone the repository:
git clone https://github.com/YOUR_USERNAME/golden-cicd-python.git
cd golden-cicd-python- Create virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Set environment variables:
cp .env.example .env
export APP_ENV=local # On Windows: set APP_ENV=local- Run the application:
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000- Test endpoints:
curl http://localhost:8000/
curl http://localhost:8000/health- Run tests:
pytest tests/ -vdocker build -t golden-cicd-python .
docker run -d --name golden-app -p 8000:8000 -e APP_ENV=local golden-cicd-pythoncurl http://localhost:8000/healthdocker stop golden-app
docker rm golden-appchmod +x scripts/deploy.sh
./scripts/deploy.shCurrently, the pipeline validates Docker containers locally. To enable AWS EC2 deployment:
# SSH into your EC2 instance
ssh -i your-key.pem ec2-user@your-ec2-ip
# Install Docker
sudo yum update -y
sudo yum install docker -y
sudo service docker start
sudo usermod -a -G docker ec2-userGo to: Settings β Secrets and variables β Actions β New repository secret
Add these secrets:
EC2_HOST: Your EC2 public IP or hostnameEC2_USER: SSH username (usuallyec2-userorubuntu)EC2_SSH_KEY: Private SSH key content (entire .pem file)
In .github/workflows/ci-cd.yml, uncomment the deploy-to-ec2 job (lines marked with comments).
Ensure your EC2 security group allows:
- Port 22 (SSH) from GitHub Actions IPs
- Port 8000 (Application) from your desired sources
git add .
git commit -m "Enable EC2 deployment"
git push origin mainThe pipeline will now:
- Run tests
- Build Docker image
- Deploy to EC2
- Verify health endpoint on EC2
| Variable | Default | Description |
|---|---|---|
APP_ENV |
local |
Environment name (local/ci/production) |
PORT |
8000 |
Application port |
| Endpoint | Method | Description | Response |
|---|---|---|---|
/ |
GET | Root endpoint | {"message": "...", "environment": "..."} |
/health |
GET | Health check | {"status": "OK", "environment": "..."} |
# Run all tests
pytest tests/ -v
# Run with coverage
pytest tests/ --cov=app --cov-report=html- Base:
python:3.11-slim - Size: ~150MB (optimized)
- Port: 8000
- Runtime: uvicorn with auto-reload disabled
# Run tests locally to debug
pytest tests/ -v# Check logs
docker logs golden-app
# Verify port availability
netstat -an | grep 8000# Verify SSH access
ssh -i your-key.pem ec2-user@your-ec2-ip
# Check Docker on EC2
docker ps
docker logs golden-cicd-appMIT
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
Built with β€οΈ using FastAPI, Docker, and GitHub Actions