Skip to content

Commit 0775ffd

Browse files
committed
deploy test
1 parent d15e2a8 commit 0775ffd

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
name: Python Server CI/CD Pipeline (EC2)
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
env:
10+
AWS_REGION: ap-northeast-2 # Seoul region
11+
PYTHON_VERSION: '3.11'
12+
13+
jobs:
14+
# 통합 테스트 단계
15+
integration-test:
16+
runs-on: ubuntu-latest
17+
18+
19+
steps:
20+
- name: Checkout code
21+
uses: actions/checkout@v4
22+
23+
- name: Set up Python
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: ${{ env.PYTHON_VERSION }}
27+
cache: 'pip'
28+
29+
- name: Install dependencies
30+
run: |
31+
python -m pip install --upgrade pip
32+
pip install -r requirements.txt
33+
pip install pytest pytest-cov pytest-asyncio httpx
34+
35+
36+
- name: Upload coverage reports
37+
uses: codecov/codecov-action@v4
38+
with:
39+
files: ./coverage.xml
40+
flags: integration-tests
41+
fail_ci_if_error: false
42+
43+
# EC2 배포
44+
deploy-ec2:
45+
needs: integration-test
46+
runs-on: ubuntu-latest
47+
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
48+
49+
steps:
50+
- name: Checkout code
51+
uses: actions/checkout@v4
52+
53+
- name: Deploy to EC2
54+
env:
55+
PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
56+
HOST: ${{ secrets.EC2_HOST }}
57+
USER: ${{ secrets.EC2_USER }}
58+
run: |
59+
echo "$PRIVATE_KEY" > private_key.pem
60+
chmod 600 private_key.pem
61+
62+
# 서버에 코드 전송 및 배포
63+
ssh -o StrictHostKeyChecking=no -i private_key.pem ${USER}@${HOST} << 'EOF'
64+
set -e
65+
66+
echo "📦 Pulling latest code..."
67+
cd /home/ubuntu/app
68+
git pull origin main
69+
70+
echo "🔧 Installing dependencies..."
71+
source venv/bin/activate
72+
pip install -r requirements.txt
73+
74+
echo "🧪 Running health check..."
75+
python -c "from app.main import app; print('✅ App imports successfully')"
76+
77+
echo "🔄 Restarting service..."
78+
sudo systemctl restart myapp
79+
80+
echo "⏳ Waiting for service to start..."
81+
sleep 5
82+
83+
echo "✅ Checking service status..."
84+
sudo systemctl status myapp --no-pager
85+
86+
echo "🎉 Deployment completed!"
87+
EOF
88+
89+
rm -f private_key.pem
90+
91+
- name: Verify deployment
92+
env:
93+
HOST: ${{ secrets.EC2_HOST }}
94+
run: |
95+
echo "🔍 Verifying deployment..."
96+
response=$(curl -s -o /dev/null -w "%{http_code}" http://${HOST}:8000/health || echo "000")
97+
if [ "$response" = "200" ]; then
98+
echo "✅ Health check passed!"
99+
else
100+
echo "❌ Health check failed with status: $response"
101+
exit 1
102+
fi

0 commit comments

Comments
 (0)