Skip to content

Commit 5448a20

Browse files
committed
Add CI/CD pipeline configuration and update Dockerfile for health check
1 parent 56cf1a7 commit 5448a20

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ENV PYTHONUNBUFFERED=1
55

66
WORKDIR /app
77

8+
# create non-root user
89
RUN addgroup --system app && adduser --system --ingroup app app
910

1011
COPY requirements.txt .
@@ -19,6 +20,7 @@ USER app
1920

2021
EXPOSE 8000
2122

22-
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health')"
23+
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
24+
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health')"
2325

24-
CMD ["fastapi", "run", "main.py", "--host", "0.0.0.0", "--port", "8000"]
26+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

0 commit comments

Comments
 (0)