1+ name : CI-CD Pipeline
2+
3+ on :
4+ pull_request :
5+ branches :
6+ - develop
7+ - main
8+
9+ push :
10+ branches :
11+ - develop
12+ - main
13+
14+ jobs :
15+ build :
16+ runs-on : ubuntu-latest
17+
18+ steps :
19+ - name : Checkout Code
20+ uses : actions/checkout@v4
21+
22+ - name : Setup Python
23+ uses : actions/setup-python@v5
24+ with :
25+ python-version : " 3.11"
26+
27+ - name : Install Dependencies
28+ run : |
29+ python -m pip install --upgrade pip
30+ pip install -r requirements.txt
31+
32+ - name : Syntax Check
33+ run : |
34+ python -m py_compile main.py
35+
36+ - name : Docker Build Test
37+ run : |
38+ docker build -t test-image .
39+
40+ - name : Run Container Health Test
41+ run : |
42+ docker run -d -p 9000:8000 --name test-container test-image
43+ sleep 8
44+ curl --fail http://localhost:9000/health
45+
46+ deploy-staging :
47+ needs : build
48+ runs-on : ubuntu-latest
49+ if : github.ref == 'refs/heads/develop'
50+
51+ steps :
52+ - name : Deploy Staging to Azure VM
53+ uses : appleboy/ssh-action@v1
54+ with :
55+ host : ${{ secrets.VM_IP }}
56+ username : azureuser
57+ key : ${{ secrets.SSH_KEY }}
58+ script : |
59+ cd staging
60+ git pull origin develop
61+ docker stop fastapi-staging || true
62+ docker rm fastapi-staging || true
63+ docker build -t fastapi-staging .
64+ docker run -d -p 8001:8000 --name fastapi-staging fastapi-staging
65+
66+ deploy-prod :
67+ needs : build
68+ runs-on : ubuntu-latest
69+ if : github.ref == 'refs/heads/main'
70+
71+ steps :
72+ - name : Deploy Production to Azure VM
73+ uses : appleboy/ssh-action@v1
74+ with :
75+ host : ${{ secrets.VM_IP }}
76+ username : azureuser
77+ key : ${{ secrets.SSH_KEY }}
78+ script : |
79+ cd prod
80+ git pull origin main
81+ docker stop fastapi-prod || true
82+ docker rm fastapi-prod || true
83+ docker build -t fastapi-prod .
84+ docker run -d -p 8000:8000 --name fastapi-prod fastapi-prod
0 commit comments