From a94127d299a08dd991de5c46fdb321cb7135ec82 Mon Sep 17 00:00:00 2001 From: romanskijdev Date: Thu, 18 Jun 2026 10:39:39 +0300 Subject: [PATCH] update deploy compiling dir --- deploy.sh | 59 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/deploy.sh b/deploy.sh index 87daf2d..5f3da85 100644 --- a/deploy.sh +++ b/deploy.sh @@ -1,6 +1,7 @@ #!/bin/bash set -e +set -E # Гарантирует, что trap ERR будет работать внутри всех подфункций trap 'echo "🔴 Deploy failed at line $LINENO"; exit 1' ERR THIS_PATH=$(dirname "$(realpath "$0")") @@ -15,6 +16,7 @@ BLUE='\033[0;34m' MAGENTA='\033[0;35m' NC='\033[0m' +# Окружение Go для сборки под sudo / root export GOPATH=/home/git/.go export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin @@ -50,7 +52,6 @@ if [ "$SERVICE_TYPE" = "all" ]; then log_info "Deploying all services..." echo "" - # ✅ Используем абсолютный путь к скрипту SELF="$THIS_PATH/deploy.sh" log_service "Step 1/4: Database migrations" @@ -153,7 +154,6 @@ echo "" log_info "Building $SERVICE_NAME..." cd "$SERVICE_PATH" -# Проверяем, лежит ли исходный код в подпапке cmd или в корне сервиса BUILD_TARGET="." if [ -d "./cmd" ]; then log_info "Found 'cmd' directory, building from ./cmd" @@ -163,16 +163,13 @@ fi gofmt -w . go vet ./... -# Передаем BUILD_TARGET вместо точки go build -ldflags="-s -w" -o "$BINARY_PATH" "$BUILD_TARGET" - log_success "Binary deployed to $BINARY_PATH" # ═══════════════════════════════════════════════════════════════ # Управление сервисом # ═══════════════════════════════════════════════════════════════ if [ "$SERVICE_KIND" = "daemon" ]; then - # Проверяем существует ли systemd unit if [ ! -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then log_warning "Systemd unit not found: /etc/systemd/system/$SERVICE_NAME.service" log_info "Creating systemd unit..." @@ -205,9 +202,9 @@ EOF sudo systemctl restart "$SERVICE_NAME" sleep 3 - if systemctl is-active --quiet "$SERVICE_NAME"; then + if sudo systemctl is-active --quiet "$SERVICE_NAME"; then log_success "Service is running" - SERVICE_STATUS=$(systemctl is-active "$SERVICE_NAME") + SERVICE_STATUS=$(sudo systemctl is-active "$SERVICE_NAME") log_info "Status: $SERVICE_STATUS" else log_error "Service failed to start" @@ -216,7 +213,6 @@ EOF fi elif [ "$SERVICE_KIND" = "oneshot" ]; then - # Миграции — проверяем существует ли unit if [ ! -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then log_warning "Systemd unit not found: /etc/systemd/system/$SERVICE_NAME.service" log_info "Creating systemd unit for migrations..." @@ -244,39 +240,48 @@ EOF fi log_info "Running migrations..." - sudo systemctl start "$SERVICE_NAME" + + # Исправлено: Запуск в неблокирующем режиме для systemd, управление возвращается в скрипт мгновенно + sudo systemctl start "$SERVICE_NAME" --no-block TIMEOUT=300 ELAPSED=0 - while systemctl is-active --quiet "$SERVICE_NAME"; do - sleep 2 - ELAPSED=$((ELAPSED + 2)) + log_info "Waiting for migrations to complete successfully..." + while true; do + # Анализируем логи на успешное окончание + if sudo journalctl -u "$SERVICE_NAME" --since "2 minutes ago" --no-pager | grep -q "Database migration completed successfully"; then + echo "" + log_success "Migrations completed successfully (detected via logs)!" - if [ $ELAPSED -ge $TIMEOUT ]; then - log_error "Migrations timed out after ${TIMEOUT}s" + # Принудительно гасим юнит, если процесс "завис" в памяти после выполнения логики + sudo systemctl stop "$SERVICE_NAME" --quiet || true + break + fi + + # Исправлено: Добавлен sudo для проверки стейта + if sudo systemctl show "$SERVICE_NAME" --property=ActiveState --value | grep -q "failed"; then + echo "" + RESULT=$(sudo systemctl show "$SERVICE_NAME" --property=ExecMainStatus --value) + log_error "Migrations failed with exit code: $RESULT" sudo journalctl -u "$SERVICE_NAME" -n 50 --no-pager exit 1 fi + sleep 5 + ELAPSED=$((ELAPSED + 5)) printf "." + + if [ $ELAPSED -ge $TIMEOUT ]; then + echo "" + log_error "Migrations timed out after ${TIMEOUT}s" + sudo journalctl -u "$SERVICE_NAME" -n 50 --no-pager + exit 1 + fi done echo "" - - RESULT=$(systemctl show "$SERVICE_NAME" --property=ExecMainStatus --value) - - if [ "$RESULT" = "0" ]; then - log_success "Migrations completed successfully" - log_info "Migration log:" - sudo journalctl -u "$SERVICE_NAME" -n 20 --no-pager - else - log_error "Migrations failed with exit code: $RESULT" - sudo journalctl -u "$SERVICE_NAME" -n 50 --no-pager - exit 1 - fi fi -echo "" echo "════════════════════════════════════════" log_success "Deployment of $SERVICE_NAME completed!" echo "════════════════════════════════════════" -- 2.43.0