From 339fc18fdd6be558da20c505a1eac730159c5133 Mon Sep 17 00:00:00 2001 From: zhouyonggao <1971162852@qq.com> Date: Fri, 28 Nov 2025 16:43:40 +0800 Subject: [PATCH] =?UTF-8?q?refactor(scripts):=20=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E9=83=A8=E7=BD=B2=E8=84=9A=E6=9C=AC=E4=B8=BAdocker=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将原有的直接部署脚本替换为使用docker容器运行的版本,提升部署的一致性和可移植性 --- scripts/deploy.sh | 22 ---------------------- scripts/deploy_docker.sh | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 22 deletions(-) delete mode 100644 scripts/deploy.sh create mode 100644 scripts/deploy_docker.sh diff --git a/scripts/deploy.sh b/scripts/deploy.sh deleted file mode 100644 index 3993deb..0000000 --- a/scripts/deploy.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env bash -set -euo pipefail -ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" -ENV_NAME="${1:-prod}" -export APP_ENV="$ENV_NAME" -cd "$ROOT_DIR" -if [ -f ".env.$ENV_NAME" ]; then - true -elif [ -f "server/.env.$ENV_NAME" ]; then - true -else - echo ".env.$ENV_NAME not found" >&2 - exit 1 -fi -mkdir -p log -cd "$ROOT_DIR/server" -go build -o "bin/marketing-data-server" "./cmd/server" -cd "$ROOT_DIR" -TARGET="$ROOT_DIR/server/bin/marketing-data-server" -nohup env APP_ENV="$ENV_NAME" "$TARGET" > "$ROOT_DIR/log/server.out" 2>&1 & -echo $! > "$ROOT_DIR/server/bin/server.pid" -echo "started: $TARGET pid $(cat "$ROOT_DIR/server/bin/server.pid")" diff --git a/scripts/deploy_docker.sh b/scripts/deploy_docker.sh new file mode 100644 index 0000000..bea209d --- /dev/null +++ b/scripts/deploy_docker.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +set -euo pipefail +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +ENV_NAME="${1:-prod}" +IMAGE="marketing-system-data-tool" +TAG="$ENV_NAME" +PORT="${PORT:-8077}" +cd "$ROOT_DIR" +ENV_FILE=".env.$ENV_NAME" +if [ ! -f "$ENV_FILE" ] && [ -f "server/.env.$ENV_NAME" ]; then + ENV_FILE="server/.env.$ENV_NAME" +fi +if [ ! -f "$ENV_FILE" ]; then + echo "$ENV_FILE not found" >&2 + exit 1 +fi +docker build -t "$IMAGE:$TAG" -f Dockerfile . +mkdir -p log storage/export +CID_NAME="marketing-data-$ENV_NAME" +RUNNING=$(docker inspect -f '{{.State.Running}}' "$CID_NAME" 2>/dev/null || echo false) +if [ "$RUNNING" = "true" ]; then + docker stop "$CID_NAME" >/dev/null 2>&1 || true +fi +if docker ps -a --format '{{.Names}}' | grep -q "^${CID_NAME}$"; then + docker rm "$CID_NAME" >/dev/null 2>&1 || true +fi +docker run -d \ + --name "$CID_NAME" \ + --restart unless-stopped \ + --env APP_ENV="$ENV_NAME" \ + --env-file "$ENV_FILE" \ + -p "$PORT:8077" \ + -v "$ROOT_DIR/storage/export:/app/storage/export" \ + -v "$ROOT_DIR/log:/app/log" \ + "$IMAGE:$TAG" +echo "container: $CID_NAME image: $IMAGE:$TAG port: $PORT"