This repository contains a simplified DevOps pipeline implementation for a Flask web application, created as part of a DevOps course midterm project. The pipeline demonstrates key DevOps principles including version control, automated testing, CI/CD, and Infrastructure as Code (IaC).
The project implements a complete DevOps pipeline for a simple Flask web application that calculates the area of rectangles. The application features:
- A web interface for user input
- API endpoints for calculations
- Health check functionality
- Blue/Green deployment strategy
- Automated testing
- Web Framework: Flask
- Version Control: Git, GitHub
- CI/CD: GitHub Actions
- Infrastructure as Code: Terraform
- Containerization: Docker
- Testing: pytest
- Deployment Strategy: Blue/Green deployment
A Flask application that provides:
- Web interface for calculating rectangle areas
- REST API endpoint for square calculation (
/square) - Health check endpoint (
/health)
The project uses Git for version control with two main branches:
master: Production-ready codedev: Development and feature integration
GitHub Actions is configured to automatically:
- Run tests on every push or PR to master
- Report test results
- Archive test artifacts
Configuration file: .github/workflows/ci.yml
Terraform is used to manage the application's infrastructure:
- Creates separate deployment directories (blue/green)
- Builds Docker images
- Manages container deployment
- Sets up health checks
- Handles port allocation
Configuration files:
terraform-blue/main-blue.tfterraform-green/main-green.tf
The project implements a Blue/Green deployment strategy:
- Two identical environments (blue and green)
- Port 5000 for green deployment
- Port 5001 for blue deployment
- Switching between environments involves running the corresponding Terraform script
A Bash script (health_check.sh) performs:
- Regular health checks of the application
- Logs results to a log file
- Configured to run every 60 seconds
- Developer commits code to repository (dev branch)
- Pull request is created to merge into master
- GitHub Actions runs automated tests
- If tests pass, code can be merged to master
- Deployment is triggered using Terraform
- Terraform provisions the environment (blue or green)
- Application is deployed in Docker container
- Health check script monitors application status
- Git
- Docker
- Terraform (>= 0.12)
- Python 3.9 or higher
-
Clone the repository:
git clone https://github.com/Googli714/DevOps_Midterm cd DevOps-Midterm -
Create a virtual environment and install dependencies:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -r requirements.txt -
Run the application locally:
python -m flask --app src/app run -
Run tests:
pytest tests/
cd terraform-green
terraform init
terraform plan -out=tfplan
terraform apply tfplan
cd terraform-blue
terraform init
terraform plan -out=tfplan
terraform apply tfplan
To perform a Blue/Green deployment switch:
-
Verify the new environment is healthy:
curl http://localhost:5001/health # For blue deployment -
Update your load balancer or proxy to point to the new environment.
-
Once traffic is directed to the new environment, you can tear down the old one if needed.
To rollback to the previous deployment:
-
Verify the previous environment is still available:
curl http://localhost:5000/health # For green deployment -
Redirect traffic back to the previous environment.
The CI pipeline in GitHub Actions:
- Triggers on push to master or PRs targeting master
- Sets up Python environment
- Installs dependencies
- Runs pytest tests
- Uploads test results as artifacts
- Publishes test results report
.
├── .github/workflows # CI/CD configuration
├── blue/ # Blue deployment directory
├── green/ # Green deployment directory
├── src/ # Application source code
│ ├── templates/ # HTML templates
│ └── app.py # Main Flask application
├── terraform-blue/ # Terraform config for blue deployment
├── terraform-green/ # Terraform config for green deployment
├── tests/ # Test files
├── .gitignore # Git ignore file
├── Dockerfile # Docker configuration
├── health_check.sh # Health monitoring script
└── requirements.txt # Python dependencies
Health check logs are stored in:
- Blue deployment:
blue/logs/LOGS.txt - Green deployment:
green/logs/LOGS.txt