Files

123 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
# ==============================================================================
# Script de Deploy - Table Expiration Watcher
# Commits, push para Gitea e rebuild dos containers no AJAX
# Conexão: Rede Local (192.168.15.200) com fallback para Tailscale (100.102.147.7)
# ==============================================================================
# Cores para logs
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
LOCAL_HOST="andrerds@192.168.15.200"
TAILSCALE_HOST="andrerds@100.102.147.7"
REMOTE_DIR="/home/andrerds/table-expiration-watcher"
GITEA_REPO="ssh://git@gitea.rdsnexus.vix.br:22222/Rds/TableExpirationWatcher.git"
SSH_OPTS="-o ConnectTimeout=4 -o BatchMode=yes -o StrictHostKeyChecking=accept-new"
echo -e "${BLUE}=====================================================${NC}"
echo -e "${BLUE} 🚀 Table Expiration Watcher - Pipeline de Deploy ${NC}"
echo -e "${BLUE}=====================================================${NC}"
# 1. Validação local (testes)
if [[ "$*" != *"--skip-tests"* ]]; then
echo -e "\n${YELLOW}🧪 [1/4] Executando testes locais...${NC}"
if command -v bun >/dev/null 2>&1; then
bun test
echo -e "${GREEN}✓ Testes passaram com sucesso!${NC}"
else
echo -e "${YELLOW}Bun não encontrado localmente, pulando testes.${NC}"
fi
else
echo -e "\n${YELLOW}🧪 [1/4] Flag --skip-tests detectada. Pulando testes.${NC}"
fi
# 2. Git Commit & Push
echo -e "\n${YELLOW}📦 [2/4] Verificando alterações no repositório...${NC}"
COMMIT_MSG="${1:-}"
# Remove flag --skip-tests da mensagem se tiver
COMMIT_MSG=$(echo "$COMMIT_MSG" | sed 's/--skip-tests//g' | xargs)
if [ -z "$COMMIT_MSG" ]; then
COMMIT_MSG="feat: deploy automático $(date +'%Y-%m-%d %H:%M')"
fi
# Verifica se há alterações para commitar
if [ -n "$(git status --porcelain)" ]; then
echo -e "${BLUE}Alterações detectadas. Criando commit: \"${COMMIT_MSG}\"${NC}"
git add .
git commit -m "$COMMIT_MSG"
else
echo -e "${GREEN}Nenhuma nova alteração pendente para commit.${NC}"
fi
echo -e "${BLUE}Enviando alterações para o Gitea (origin/main)...${NC}"
git push origin main || git push -u origin main
# 3. Resolução do Host SSH (Local vs Tailscale)
echo -e "\n${YELLOW}🌐 [3/4] Determinando rota de conexão com o AJAX...${NC}"
TARGET_HOST=""
echo -e "Testando conexão na rede local (${LOCAL_HOST})..."
if ssh $SSH_OPTS "$LOCAL_HOST" "echo ok" >/dev/null 2>&1; then
TARGET_HOST="$LOCAL_HOST"
echo -e "${GREEN}✓ Conectado via REDE LOCAL (${LOCAL_HOST})${NC}"
else
echo -e "${YELLOW}⚠️ Rede local indisponível. Tentando via TAILSCALE (${TAILSCALE_HOST})...${NC}"
if ssh $SSH_OPTS "$TAILSCALE_HOST" "echo ok" >/dev/null 2>&1; then
TARGET_HOST="$TAILSCALE_HOST"
echo -e "${GREEN}✓ Conectado via TAILSCALE (${TAILSCALE_HOST})${NC}"
else
echo -e "${RED}❌ Erro: Não foi possível alcançar o AJAX via Rede Local nem via Tailscale.${NC}"
exit 1
fi
fi
# 4. Atualização e Rebuild no AJAX
echo -e "\n${YELLOW}🐳 [4/4] Atualizando código e reconstruindo container no AJAX...${NC}"
ssh $SSH_OPTS "$TARGET_HOST" bash << EOF
set -e
mkdir -p "$REMOTE_DIR"
cd "$REMOTE_DIR"
# Configura remote origin caso não exista
if ! git remote get-url origin >/dev/null 2>&1; then
git remote add origin "$GITEA_REPO"
else
git remote set-url origin "$GITEA_REPO"
fi
echo "📥 Puxando versão mais recente do repositório..."
git fetch origin main
git reset --hard origin/main
echo "🔨 Reconstruindo e reiniciando container com Docker Compose..."
docker compose up -d --build
echo "⏳ Aguardando subida do container..."
sleep 4
echo "🩺 Verificando healthcheck interno..."
curl -s -f http://127.0.0.1:3002/health || (sleep 2; curl -s http://127.0.0.1:3002/health)
EOF
# 5. Validação Externa (HTTPS Cloudflare)
echo -e "\n${YELLOW}🌐 Validando endpoint público HTTPS...${NC}"
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://table-watcher.rdsnexus.com/)
if [ "$HTTP_STATUS" = "302" ] || [ "$HTTP_STATUS" = "200" ]; then
echo -e "${GREEN}✓ Endpoint público HTTPS respondendo OK (HTTP $HTTP_STATUS): https://table-watcher.rdsnexus.com${NC}"
else
echo -e "${YELLOW}Aviso: Resposta HTTPS retornou HTTP $HTTP_STATUS. Verifique o status do Cloudflared.${NC}"
fi
echo -e "\n${GREEN}=====================================================${NC}"
echo -e "${GREEN} 🎉 Deploy concluído com sucesso no AJAX! ${NC}"
echo -e "${GREEN}=====================================================${NC}"