deploy test #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Python Server CI/CD Pipeline (EC2) | |
| on: | |
| push: | |
| branches: [ main ] | |
| pull_request: | |
| branches: [ main ] | |
| env: | |
| AWS_REGION: ap-northeast-2 # Seoul region | |
| PYTHON_VERSION: '3.11' | |
| jobs: | |
| # 통합 테스트 단계 | |
| integration-test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: ${{ env.PYTHON_VERSION }} | |
| cache: 'pip' | |
| - name: Install dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt | |
| pip install pytest pytest-cov pytest-asyncio httpx | |
| - name: Upload coverage reports | |
| uses: codecov/codecov-action@v4 | |
| with: | |
| files: ./coverage.xml | |
| flags: integration-tests | |
| fail_ci_if_error: false | |
| # EC2 배포 | |
| deploy-ec2: | |
| needs: integration-test | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Deploy to EC2 | |
| env: | |
| PRIVATE_KEY: ${{ secrets.EC2_SSH_PRIVATE_KEY }} | |
| HOST: ${{ secrets.EC2_HOST }} | |
| USER: ${{ secrets.EC2_USER }} | |
| run: | | |
| echo "$PRIVATE_KEY" > private_key.pem | |
| chmod 600 private_key.pem | |
| # 서버에 코드 전송 및 배포 | |
| ssh -o StrictHostKeyChecking=no -i private_key.pem ${USER}@${HOST} << 'EOF' | |
| set -e | |
| echo "📦 Pulling latest code..." | |
| cd /home/ubuntu/app | |
| git pull origin main | |
| echo "🔧 Installing dependencies..." | |
| source venv/bin/activate | |
| pip install -r requirements.txt | |
| echo "🧪 Running health check..." | |
| python -c "from app.main import app; print('✅ App imports successfully')" | |
| echo "🔄 Restarting service..." | |
| sudo systemctl restart myapp | |
| echo "⏳ Waiting for service to start..." | |
| sleep 5 | |
| echo "✅ Checking service status..." | |
| sudo systemctl status myapp --no-pager | |
| echo "🎉 Deployment completed!" | |
| EOF | |
| rm -f private_key.pem | |
| - name: Verify deployment | |
| env: | |
| HOST: ${{ secrets.EC2_HOST }} | |
| run: | | |
| echo "🔍 Verifying deployment..." | |
| response=$(curl -s -o /dev/null -w "%{http_code}" http://${HOST}:8000/health || echo "000") | |
| if [ "$response" = "200" ]; then | |
| echo "✅ Health check passed!" | |
| else | |
| echo "❌ Health check failed with status: $response" | |
| exit 1 | |
| fi |