build: 完善一键部署脚本

This commit is contained in:
2026-05-23 17:24:58 +08:00
parent bc5bb28b15
commit d236b5348a
4 changed files with 64 additions and 3 deletions
+4 -1
View File
@@ -1,9 +1,11 @@
# ---- Build Stage ----
FROM golang:1.26-alpine AS builder
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add --no-cache git
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
RUN go env -w GOPROXY=https://goproxy.cn,direct && go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -o /bin/gen2d ./cmd/main.go
@@ -11,6 +13,7 @@ RUN CGO_ENABLED=0 GOOS=linux go build -o /bin/gen2d ./cmd/main.go
# ---- Runtime Stage ----
FROM alpine:3.20
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add --no-cache ca-certificates
COPY --from=builder /bin/gen2d /usr/local/bin/gen2d
Executable
+57
View File
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
set -euo pipefail
REPO_URL="git@gitee.com:hezhaohui123/gen2d.git"
INSTALL_DIR="${GEN2D_DIR:-$HOME/gen2d}"
PORT="${GEN2D_PORT:-10000}"
echo "==> gen2d 一键部署脚本"
# 1. 安装依赖检查
for cmd in git docker; do
if ! command -v "$cmd" &>/dev/null; then
echo "错误: 未找到 $cmd,请先安装"
exit 1
fi
done
if ! docker compose version &>/dev/null && ! docker-compose version &>/dev/null; then
echo "错误: 未找到 docker compose,请先安装"
exit 1
fi
# 2. 拉取代码
if [ -d "$INSTALL_DIR/.git" ]; then
echo "==> 目录 $INSTALL_DIR 已存在,拉取最新代码..."
cd "$INSTALL_DIR"
git pull
else
echo "==> 克隆仓库到 $INSTALL_DIR ..."
git clone "$REPO_URL" "$INSTALL_DIR"
cd "$INSTALL_DIR"
fi
# 3. 构建并启动
echo "==> 构建并启动 Docker 容器..."
if docker compose version &>/dev/null; then
COMPOSE_CMD="docker compose"
else
COMPOSE_CMD="docker-compose"
fi
GEN2D_PORT="$PORT" $COMPOSE_CMD up -d --build
# 获取公网 IP
PUBLIC_IP=$(curl -s --connect-timeout 3 https://ifconfig.me 2>/dev/null || \
curl -s --connect-timeout 3 https://api.ipify.org 2>/dev/null || \
echo "YOUR_SERVER_IP")
echo ""
echo "==> 部署完成!"
echo " 前端访问: http://$PUBLIC_IP:$PORT"
echo " 后端健康检查: http://$PUBLIC_IP:$PORT/api/v1/health"
echo ""
echo "常用命令:"
echo " 查看日志: cd $INSTALL_DIR && $COMPOSE_CMD logs -f"
echo " 停止服务: cd $INSTALL_DIR && $COMPOSE_CMD down"
echo " 重启服务: cd $INSTALL_DIR && $COMPOSE_CMD restart"
+1 -1
View File
@@ -15,7 +15,7 @@ services:
context: ./frontend
dockerfile: Dockerfile
ports:
- "10000:80"
- "${GEN2D_PORT:-10000}:80"
depends_on:
- backend
restart: unless-stopped
+2 -1
View File
@@ -1,9 +1,10 @@
# ---- Build Stage ----
FROM node:20-alpine AS builder
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci
RUN npm config set registry https://registry.npmmirror.com && npm ci
COPY . .
RUN npm run build