flea/_deploy/scripts/deploy.sh
ruberoid 1be50c3eeb
Some checks failed
continuous-integration/drone/tag Build is failing
deploy_only: 0.7.35
2025-10-17 10:56:40 +04:00

107 lines
5.0 KiB
Bash
Executable File

#!/bin/bash
set -e
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 🚀 Nocr Services Deployment Script
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Usage: ./deploy.sh <tag> [commit-sha] [image-tag-override]
# Examples:
# Full release: ./deploy.sh v1.2.3 abc1234
# Deploy only: ./deploy.sh deploy-v1.2.3 def5678 v1.2.3
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
TAG=${1:-latest}
COMMIT_SHA=${2:-}
NAMESPACE="nocr"
DEPLOYMENT_FILE="../k8s/deployment.yaml"
IMAGE_TAG_OVERRIDE=${3:-}
# Determine image tag to use
# Priority: 1) IMAGE_TAG_OVERRIDE (from command line), 2) TAG
if [ -n "$IMAGE_TAG_OVERRIDE" ]; then
IMAGE_TAG=$IMAGE_TAG_OVERRIDE
else
IMAGE_TAG=${TAG}
fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🚀 Starting deployment of Nocr services"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📦 Tag: $TAG"
echo "🏷️ Image Tag: $IMAGE_TAG"
if [ -n "$COMMIT_SHA" ]; then
echo "📝 Commit SHA: $COMMIT_SHA"
fi
echo "🎯 Namespace: $NAMESPACE"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
echo "❌ kubectl not found. Please install kubectl."
exit 1
fi
# Check cluster connection
echo "🔍 Checking connection to Kubernetes cluster..."
if ! kubectl cluster-info &> /dev/null; then
echo "❌ Cannot connect to Kubernetes cluster."
exit 1
fi
echo "✅ Connected to cluster"
# Create temporary deployment file with updated image tags
TEMP_DEPLOYMENT=$(mktemp)
cp "$DEPLOYMENT_FILE" "$TEMP_DEPLOYMENT"
echo "🔧 Updating image tags in deployment manifests..."
# Update image tags for all services
sed -i "s|hub.musk.fun/k8s/nocr/telegram_listener:.*|hub.musk.fun/k8s/nocr/telegram_listener:${IMAGE_TAG}|g" "$TEMP_DEPLOYMENT"
sed -i "s|hub.musk.fun/k8s/nocr/telegram_client:.*|hub.musk.fun/k8s/nocr/telegram_client:${IMAGE_TAG}|g" "$TEMP_DEPLOYMENT"
sed -i "s|hub.musk.fun/k8s/nocr/text_matcher:.*|hub.musk.fun/k8s/nocr/text_matcher:${IMAGE_TAG}|g" "$TEMP_DEPLOYMENT"
sed -i "s|hub.musk.fun/k8s/nocr/text_matcher_migrator:.*|hub.musk.fun/k8s/nocr/text_matcher_migrator:${IMAGE_TAG}|g" "$TEMP_DEPLOYMENT"
sed -i "s|hub.musk.fun/k8s/nocr/users:.*|hub.musk.fun/k8s/nocr/users:${IMAGE_TAG}|g" "$TEMP_DEPLOYMENT"
sed -i "s|hub.musk.fun/k8s/nocr/users_migrator:.*|hub.musk.fun/k8s/nocr/users_migrator:${IMAGE_TAG}|g" "$TEMP_DEPLOYMENT"
echo "✅ Image tags updated"
# Apply deployments
echo "📦 Applying deployment manifests to cluster..."
kubectl apply -f "$TEMP_DEPLOYMENT" -n "$NAMESPACE"
# Clean up temp file
rm "$TEMP_DEPLOYMENT"
echo "✅ Manifests applied"
echo ""
echo "⏳ Waiting for rollouts to complete..."
echo ""
# Wait for each deployment to roll out
DEPLOYMENTS=("telegram-listener" "text-matcher" "users" "telegram-client")
TIMEOUT="300s"
for deployment in "${DEPLOYMENTS[@]}"; do
echo "🔄 Rolling out $deployment..."
if kubectl rollout status deployment/"$deployment" -n "$NAMESPACE" --timeout="$TIMEOUT"; then
echo "$deployment rolled out successfully"
else
echo "$deployment rollout failed or timed out"
echo "🔍 Pod status:"
kubectl get pods -n "$NAMESPACE" -l app="$deployment"
echo "🔍 Recent events:"
kubectl get events -n "$NAMESPACE" --sort-by='.lastTimestamp' | grep "$deployment" | tail -10
exit 1
fi
echo ""
done
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Deployment completed successfully!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📊 Pod status:"
kubectl get pods -n "$NAMESPACE" -o wide
echo ""
echo "🔍 Running health checks..."
bash "$(dirname "$0")/health-check.sh"