cicdtest3 #5
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 | |
| # 가상환경 경로 설정 | |
| VENV_PATH="/home/ubuntu/app/void-Filter/venv" | |
| APP_PATH="/home/ubuntu/app/void-Filter" | |
| echo "📦 Pulling latest code..." | |
| cd ${APP_PATH} | |
| git pull origin main | |
| echo "🔧 Installing dependencies in virtual environment..." | |
| # 가상환경의 pip을 직접 실행 | |
| ${VENV_PATH}/bin/pip install --upgrade pip | |
| ${VENV_PATH}/bin/pip install -r requirements.txt | |
| echo "🧪 Running health check..." | |
| # 가상환경의 python을 직접 실행 | |
| ${VENV_PATH}/bin/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 |