Skip to content

Commit 6072d1e

Browse files
committed
coding part completed
1 parent e67188b commit 6072d1e

File tree

11 files changed

+115
-0
lines changed

11 files changed

+115
-0
lines changed

.github/workflows/ci-cd.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: CI/CD Pipeline
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
7+
jobs:
8+
build-test-deploy:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v2
13+
14+
- name: Set up Python
15+
uses: actions/setup-python@v2
16+
with:
17+
python-version: '3.9'
18+
19+
- name: Install dependencies
20+
run: |
21+
pip install -r app/requirements.txt
22+
23+
- name: Run Tests
24+
run: |
25+
python app/test_main.py
26+
27+
- name: Build Docker Image
28+
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/fastapi-app .
29+
30+
- name: Push Docker Image
31+
run: |
32+
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin
33+
docker push ${{ secrets.DOCKERHUB_USERNAME }}/fastapi-app
34+
35+
- name: Deploy with Terraform
36+
run: |
37+
cd infra
38+
terraform init
39+
terraform apply -auto-approve -var access_key=${{ secrets.AWS_ACCESS_KEY }} -var secret_key=${{ secrets.AWS_SECRET_KEY }}
40+
41+
- name: Notify Success
42+
run: echo "Deployment completed"

Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM python:3.9-slim
2+
3+
WORKDIR /app
4+
COPY app/ /app
5+
6+
RUN pip install --no-cache-dir -r requirements.txt
7+
8+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]

app/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from fastapi import FastAPI
2+
3+
app = FastAPI()
4+
5+
@app.get("/")
6+
def read_root():
7+
return {"status":"ok"}
8+
9+
@app.get("/health")
10+
def health_check():
11+
return {"status": "ok"}
12+

app/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fastapi
2+
uvicorn
3+

app/test-main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from fastapi.testclient import TestClient
2+
from main import app
3+
4+
client = TestClient(app)
5+
6+
def test_root():
7+
response = client.get("/")
8+
assert response.status_code == 200
9+
10+

infra/main.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
access_key = var.access_key
4+
secret_key = var.secret_key
5+
}
6+
7+
resource "aws_instance" "app_server" {
8+
ami = "ami-0c02fb55956c7d316" # Ubuntu 20.04 LTS
9+
instance_type = "t2.micro"
10+
11+
tags = {
12+
Name = "fastapi-server"
13+
}
14+
15+
user_data = file("scripts/deploy.sh")
16+
}

infra/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "public_ip" {
2+
value = aws_instance.app_server.public_ip
3+
}

infra/variables.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
variable "access_key" {}
2+
variable "secret_key" {}

qa/manual_test_cases.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Test Case: Home Page
2+
- Step: Hit `<public-ip>`
3+
- Expected: {"status": "OK"}
4+
5+
# Test Case: Health Check
6+
- Step: Hit `http://<public-ip>/health`
7+
- Expected: {"status": "healthy" }

scripts/deploy.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
sudo apt update
4+
sudo apt install docker.io -y
5+
sudo systemctl start docker
6+
sudo docker pull
7+
sudo docker run -d -p 80:80

0 commit comments

Comments
 (0)