Files
canina/scripts/deploy.sh
T

171 lines
7.2 KiB
Bash

#!/bin/bash
set -e
# Explicit no-data-write stage rollout. Run only after testing and pushing HEAD.
if [ "${2:-}" = "stage-backend-only" ]; then
[ "${1:-}" = "develop" ] || exit 2
cd /opt/services/canina/stage
exec 9>/opt/services/canina/stage-backend-deploy.lock
flock -n 9 || { echo "Stage deployment already in progress"; exit 1; }
[ "$(git branch --show-current)" = develop ]
git fetch origin develop
[ "$(git rev-parse HEAD)" = "$(git rev-parse origin/develop)" ]
git diff --quiet -- backend scripts/compose.stage.yml scripts/deploy.sh
# Read tracked compose directly; preserve generated config and nginx.
docker compose -p stage -f scripts/compose.stage.yml --project-directory . build backend_stage
docker compose -p stage -f scripts/compose.stage.yml --project-directory . up -d --no-deps --remove-orphans backend_stage
exit 0
fi
export DOCKER_BUILDKIT=1
export COMPOSE_DOCKER_CLI_BUILD=1
# Ensure nexus is connected to default bridge network so docker build can reach it
docker network connect bridge nexus 2>/dev/null || true
# Nexus npm registry
NEXUS_IP=$(docker inspect -f '{{.NetworkSettings.Networks.bridge.IPAddress}}' nexus 2>/dev/null || true)
if [ -z "$NEXUS_IP" ]; then
NEXUS_IP="172.17.0.3"
fi
NPM_REGISTRY="http://${NEXUS_IP}:8081/repository/bitchpedar/"
BRANCH="${1:-develop}"
echo "Starting Deployment for branch: ${BRANCH}"
if [ "$BRANCH" == "develop" ]; then
COMPOSE_FILE="compose.stage.yml"
TARGET_DIR="/opt/services/canina/stage"
BACKEND_HOST="backend_stage"
APP_HOST="stage.canina.ir"
ADMIN_HOST="stageadmin.canina.ir"
API_URL="https://stageapi.canina.ir"
CONTAINER_PREFIX="canino_backend_stage"
else
COMPOSE_FILE="compose.prod.yml"
TARGET_DIR="/opt/services/canina/prod"
BACKEND_HOST="backend_prod"
APP_HOST="canina.ir"
ADMIN_HOST="admin.canina.ir"
API_URL="https://api.canina.ir"
CONTAINER_PREFIX="canino_backend_prod"
fi
mkdir -p "$TARGET_DIR"
if [ ! -d "$TARGET_DIR/.git" ]; then
echo "Cloning repository into $TARGET_DIR..."
git clone -b "$BRANCH" https://git.parsaaghayi.ir/parsa/canina.git "$TARGET_DIR"
else
echo "Pulling latest changes in $TARGET_DIR..."
cd "$TARGET_DIR"
git fetch origin "$BRANCH"
git reset --hard "origin/$BRANCH"
cd -
fi
cd "$TARGET_DIR"
# Capture the bot key from Docker before recreating the bot; runner mounts may not include the host env file.
if docker inspect canina-bot >/dev/null 2>&1; then
LIVE_BOT_KEY=$(docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' canina-bot | awk -F= '$1 == "SUPPORT_INTERNAL_KEY" {print substr($0, index($0, "=") + 1); exit}')
if [ -n "$LIVE_BOT_KEY" ]; then
grep -v '^SUPPORT_CHAT_KEY=' .support-chat.env > .support-chat.env.tmp 2>/dev/null || true
printf 'SUPPORT_CHAT_KEY=%s\n' "$LIVE_BOT_KEY" >> .support-chat.env.tmp
mv .support-chat.env.tmp .support-chat.env
chmod 600 .support-chat.env
fi
unset LIVE_BOT_KEY
fi
# Inject support-chat runtime configuration from CI/CD without committing secrets.
# The bot is the runtime owner of the internal key; synchronize only the
# matching value into the colocated backend env file, never into Git or logs.
BOT_ENV="/opt/services/ai/canina-bot/support.env"
if [ -r "$BOT_ENV" ]; then
BOT_KEY=$(awk -F= '$1 == "SUPPORT_INTERNAL_KEY" {print substr($0, index($0, "=") + 1); exit}' "$BOT_ENV")
if [ -n "$BOT_KEY" ]; then
touch .support-chat.env
grep -v '^SUPPORT_CHAT_KEY=' .support-chat.env > .support-chat.env.tmp || true
printf "SUPPORT_CHAT_KEY=%s\n" "$BOT_KEY" >> .support-chat.env.tmp
mv .support-chat.env.tmp .support-chat.env
chmod 600 .support-chat.env
fi
unset BOT_KEY
fi
if [ -n "${SUPPORT_CHAT_KEY:-}" ]; then
touch .support-chat.env
grep -v '^SUPPORT_CHAT_KEY=' .support-chat.env > .support-chat.env.tmp || true
printf 'SUPPORT_CHAT_KEY=%s\n' "$SUPPORT_CHAT_KEY" >> .support-chat.env.tmp
mv .support-chat.env.tmp .support-chat.env
chmod 600 .support-chat.env
fi
if [ ! -s .support-chat.env ]; then
echo "ERROR: missing .support-chat.env / SUPPORT_CHAT_KEY; refusing deployment" >&2
exit 1
fi
# Ensure .revalidation.env exists so compose never fails with missing env file
touch .revalidation.env
# ✅ فایل‌های کانفیگ را از پوشه‌ی scripts مخزن به TARGET_DIR کپی می‌کنیم
echo "Copying configuration files in $TARGET_DIR..."
cp "scripts/$COMPOSE_FILE" "docker-compose.yml"
sed -i 's/- REDIS_PORT: 6379/- REDIS_PORT=6379/g' "docker-compose.yml" 2>/dev/null || true
echo "Patching nginx.conf for environment..."
sed -i "s/__BACKEND_HOST__/${BACKEND_HOST}/g" "nginx.conf"
sed -i "s/__APP_HOST__/${APP_HOST}/g" "nginx.conf"
sed -i "s/__ADMIN_HOST__/${ADMIN_HOST}/g" "nginx.conf"
echo "Using Nexus npm proxy: $NPM_REGISTRY"
NPM_REGISTRY_ARG="--build-arg NPM_REGISTRY=${NPM_REGISTRY}"
echo "Building Docker images..."
docker compose --progress plain build \
--build-arg NEXT_PUBLIC_API_URL="${API_URL}/api" \
--build-arg VITE_API_URL="${API_URL}" \
${NPM_REGISTRY_ARG}
echo "Building support-chat bot image from committed source..."
BOT_IMAGE="canina-support-bot:${BRANCH}"
DOCKER_BUILDKIT=0 docker build --pull=false -f scripts/support-chat/Dockerfile -t "${BOT_IMAGE}" .
BOT_ENV="${TARGET_DIR}/.support-bot.runtime.env"
docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' canina-bot > "${BOT_ENV}"
chmod 600 "${BOT_ENV}"
echo "Starting containers..."
docker compose up -d --remove-orphans
echo "Starting support-chat bot container..."
docker rm -f canina-bot >/dev/null 2>&1 || true
docker run -d --name canina-bot --restart unless-stopped --env-file "${BOT_ENV}" -e HERMES_HOME=/opt/data -e HERMES_RUNTIME_DIR=/opt/hermes/tools -e API_SERVER_HOST=0.0.0.0 -e API_SERVER_PORT=8642 --network traefik_public -v canina_bot_data:/opt/data "${BOT_IMAGE}" gateway run
docker network connect --ip 172.22.0.15 vpn_net canina-bot
rm -f "${BOT_ENV}"
echo "Waiting for backend to start..."
sleep 15
# Apply committed Prisma migrations as part of the CI/CD deployment.
# This is intentionally migrate deploy (not db push): it is repeatable,
# fail-closed, and applies only migrations already reviewed and committed.
echo "Applying committed database migrations..."
for migration in \
20261013210253_add_admin_modules \
20261015000000_add_support_conversations \
20261020000000_add_blog_freshness_authorship \
20261022000000_add_orchestration_v4_core \
20261022190000_harden_orchestration_ledger \
20261023170000_prepare_workflow_outbox \
20261024060000_prepare_not_before \
20261024090000_stage_test_principal; do
docker exec -e PRISMA_HIDE_UPDATE_MESSAGE=true "${CONTAINER_PREFIX}" npx prisma migrate resolve --applied "$migration" >/dev/null 2>&1 || true
done
docker exec -e PRISMA_HIDE_UPDATE_MESSAGE=true "${CONTAINER_PREFIX}" npx prisma migrate deploy
# Code-only deployment: seed and destructive data changes are not performed here.
# Auto-cleanup old docker builder cache to keep disk healthy (keeps latest 6GB)
echo "Cleaning up old docker build cache..."
docker builder prune -af --keep-storage 6GB 2>/dev/null || true
docker image prune -f 2>/dev/null || true
echo "Deployment for $BRANCH completed successfully!"