diff --git a/.forgejo/workflows/deploy_bot.yaml b/.forgejo/workflows/deploy_bot.yaml new file mode 100644 index 0000000..97e7bba --- /dev/null +++ b/.forgejo/workflows/deploy_bot.yaml @@ -0,0 +1,140 @@ +name: Deploy Telegram Bot + +on: + workflow_dispatch: + push: + branches: + - master + paths: + - 'mnemo_cards_telegram_bot/**' + - 'mnemo_cards_common/**' + - 'mnemo_cards_common_backend/**' + +jobs: + deploy-bot: + name: Deploy Telegram Bot + runs-on: ubuntu-latest + steps: + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Checkout code + uses: actions/checkout@v3 + + - name: Install Dependencies + run: | + apt-get update && apt-get install -y rsync openssh-client + + - name: Setup SSH + run: | + echo "🔍 Debugging SSH connection..." + echo "Target host: ${{ secrets.SSH_HOST }}" + ping -c 3 ${{ secrets.SSH_HOST }} || echo "⚠️ Ping failed" + nslookup ${{ secrets.SSH_HOST }} || echo "⚠️ DNS lookup failed" + nc -zv ${{ secrets.SSH_HOST }} 22 || echo "⚠️ Port 22 not reachable" + + mkdir -p ~/.ssh + echo "${{ secrets.SSH_KEY }}" > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts 2>/dev/null || echo "⚠️ SSH keyscan failed" + + - name: Sync Source Code + run: | + # Sync common packages + rsync -avz --delete --exclude '.*' --exclude 'build' --exclude 'doc' \ + mnemo_cards_common \ + mnemo_cards_common_backend \ + ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:~/ + + # Sync telegram bot project + rsync -avz --delete --exclude '.*' --exclude 'build' --exclude 'bot.exe' --exclude 'bot_build.exe' \ + mnemo_cards_telegram_bot \ + ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }}:~/ + + - name: Get Dependencies + run: | + ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'ENDSSH' + set -e + cd ~/mnemo_cards_common + echo "📦 Getting common dependencies..." + dart pub get + + cd ~/mnemo_cards_common_backend + echo "📦 Getting common backend dependencies..." + dart pub get + + cd ~/mnemo_cards_telegram_bot + echo "📦 Getting telegram bot dependencies..." + dart pub get + ENDSSH + + - name: Compile Bot + run: | + ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'ENDSSH' + set -e + cd ~/mnemo_cards_telegram_bot + echo "🔨 Compiling telegram bot..." + dart compile exe --verbose bin/main.dart -o bot_build.exe + ENDSSH + + - name: Stop Service + run: | + ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'ENDSSH' + set -e + echo "🛑 Stopping telegram bot service..." + sudo systemctl stop mnemo_cards_telegram_bot || true + echo "✅ Telegram bot service stopped" + ENDSSH + + - name: Update Service Configuration + run: | + ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'ENDSSH' + set -e + cd ~/mnemo_cards_telegram_bot + echo "⚙️ Updating service configuration..." + sudo chmod 777 mnemo_cards_telegram_bot.service + sudo cp mnemo_cards_telegram_bot.service /etc/systemd/system/ + ENDSSH + + - name: Update Executable + run: | + ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'ENDSSH' + set -e + cd ~/mnemo_cards_telegram_bot + echo "🔄 Replacing executable..." + sudo cp bot_build.exe bot.exe + rm bot_build.exe + ENDSSH + + - name: Restart Service + run: | + ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'ENDSSH' + set -e + echo "🚀 Restarting telegram bot service..." + sudo systemctl daemon-reload + sudo systemctl start mnemo_cards_telegram_bot + sleep 2 + sudo systemctl status mnemo_cards_telegram_bot --no-pager --lines=5 || echo "Could not get status" + ENDSSH + + - name: Final Verification + run: | + ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 -o ConnectTimeout=30 -o StrictHostKeyChecking=no ${{ secrets.SSH_USER }}@${{ secrets.SSH_HOST }} << 'ENDSSH' + set -e + echo "🔍 Final verification..." + + # Check telegram bot service status + if sudo systemctl is-active --quiet mnemo_cards_telegram_bot; then + echo "✅ Telegram bot service is running" + echo "📋 Recent logs:" + journalctl -u mnemo_cards_telegram_bot --since "2min ago" --no-pager -n 10 || true + else + echo "❌ Telegram bot service is not running!" + sudo systemctl status mnemo_cards_telegram_bot --no-pager || true + exit 1 + fi + + echo "✅ Telegram bot deployment completed successfully!" + ENDSSH