Merge pull request #13 from VitaCheck/develop #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: 🚀 Deploy FastAPI to EC2 | |
| on: | |
| push: | |
| branches: | |
| - main # main 브랜치 푸시 시 자동 배포 | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: ✅ Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: ⚙️ Deploy to EC2 via SSH | |
| uses: appleboy/ssh-action@v0.1.10 | |
| with: | |
| host: ${{ secrets.HOST }} | |
| username: ${{ secrets.USERNAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| timeout: 120s | |
| command_timeout: 10m | |
| script: | | |
| echo "====== DEPLOY START ======" | |
| mkdir -p /home/ubuntu | |
| LOG_FILE="/home/ubuntu/ai-deploy.log" | |
| echo "🪶 [INFO] Logging full output to $LOG_FILE" | |
| { | |
| echo "📦 [STEP 1] Clean old repository" | |
| cd /home/ubuntu || exit 1 | |
| rm -rf vitacheck-ai | |
| echo "📥 [STEP 2] Clone latest code" | |
| git clone --depth 1 https://github.com/VitaCheck/vitacheck-ai.git | |
| cd vitacheck-ai/fastapi-ocr || exit 1 | |
| echo "🐍 [STEP 3] Setup Python venv" | |
| if [ ! -d "venv_vc" ]; then | |
| python3 -m venv venv_vc | |
| fi | |
| source venv_vc/bin/activate | |
| echo "📦 [STEP 4] Install dependencies" | |
| pip install --upgrade pip -q | |
| pip install -r requirements.txt -q | |
| echo "🔑 [STEP 5] Generate .env" | |
| if [ ! -f ".env" ]; then | |
| echo "GEMINI_API_KEY=${{ secrets.GEMINI_API_KEY }}" > .env | |
| fi | |
| echo "⚙️ [STEP 6] Setup PM2" | |
| if ! command -v pm2 &> /dev/null; then | |
| echo "Installing PM2..." | |
| sudo apt update -y > /dev/null 2>&1 | |
| sudo apt install -y nodejs npm > /dev/null 2>&1 | |
| sudo npm install -g pm2 > /dev/null 2>&1 | |
| fi | |
| echo "🧹 [STEP 7] Stop existing FastAPI process (if any)" | |
| if pm2 list | grep -q "fastapi-ocr"; then | |
| echo "Existing process found. Stopping it..." | |
| pm2 stop fastapi-ocr || true | |
| pm2 delete fastapi-ocr || true | |
| else | |
| echo "No existing FastAPI process found." | |
| fi | |
| echo "🚀 [STEP 8] Start FastAPI (via venv Python)" | |
| pm2 start "venv_vc/bin/python -m uvicorn main:app --host 0.0.0.0 --port 8000" \ | |
| --name fastapi-ocr \ | |
| --cwd /home/ubuntu/vitacheck-ai/fastapi-ocr \ | |
| --no-autorestart --silent | |
| sleep 5 | |
| STATUS=$(pm2 info fastapi-ocr | grep status | awk '{print $4}') | |
| if [ "$STATUS" != "online" ]; then | |
| echo "❌ FastAPI failed to start properly (status=$STATUS)" | |
| echo "📜 --- PM2 Logs ---" | |
| pm2 logs fastapi-ocr --lines 20 >> $LOG_FILE 2>&1 | |
| exit 1 | |
| fi | |
| pm2 save --force > /dev/null 2>&1 | |
| echo "✅ [DONE] Deployment finished successfully" | |
| } >> $LOG_FILE 2>&1 | |
| echo "====== DEPLOY SUMMARY ======" | |
| echo "📄 Logs saved to: $LOG_FILE" | |
| echo "🧾 --- Last 10 lines ---" | |
| tail -n 10 $LOG_FILE || echo "No log file found." | |
| echo "====== DEPLOY COMPLETE ======" |