From 3d2851a1f1aaa887eb1542e779ce59fdd5ffe1ef Mon Sep 17 00:00:00 2001 From: whm <973418690@qq.com> Date: Fri, 31 Jul 2026 10:20:46 +0800 Subject: [PATCH] feat: Linux one-click start pulls from Gitea ai_site Co-authored-by: Cursor --- .env.example | 7 +++++ README.md | 11 +++++--- pull-and-restart.sh | 10 +++++-- scripts/lib-aijz-deploy.sh | 53 +++++++++++++++++++++++++++++++++----- start.sh | 19 ++++++++++++-- 5 files changed, 86 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index 098e51e..fc66fa6 100644 --- a/.env.example +++ b/.env.example @@ -37,3 +37,10 @@ FIDELITY_PASSWORD=demo123 # AIJZ_PUBLIC_BASE_URL=https://aijz.example.com # 证书放 nginx/<域名>.pem + nginx/<域名>.key,或 nginx/fullchain.pem + privkey.pem # 改配置后:./reload-config.sh host-nginx 或 ./reload-config.sh platform + +# ---- 一键部署拉代码(./start.sh 默认拉取;./pull-and-restart.sh 专用)---- +# AIJZ_GIT_URL=https://gitea.yuxindazhineng.com/whm/ai_site.git +# GIT_BRANCH=main +# FORCE_GIT_RESET=1 +# AIJZ_SKIP_GIT_PULL=0 +# 私有库示例:AIJZ_GIT_URL=https://@gitea.yuxindazhineng.com/whm/ai_site.git diff --git a/README.md b/README.md index bdb4e0f..cfe40a0 100644 --- a/README.md +++ b/README.md @@ -32,13 +32,18 @@ ### Linux ```bash -chmod +x start.sh stop.sh restart.sh scripts/linux/*.sh -./start.sh # 默认 Compose +chmod +x start.sh stop.sh restart.sh pull-and-restart.sh scripts/linux/*.sh +./start.sh # 默认先从 Gitea 拉代码再 Compose 启动 ./start.sh --rebuild +./start.sh --no-pull # 跳过拉取 +./pull-and-restart.sh # 拉最新后重启 ./stop.sh -./restart.sh # 先停再启 +./restart.sh # 先停再启(不拉代码) ``` +代码源默认:`https://gitea.yuxindazhineng.com/whm/ai_site.git`(分支 `main`)。 +可用环境变量 `AIJZ_GIT_URL` / `GIT_BRANCH` / `AIJZ_SKIP_GIT_PULL=1` 覆盖(见 `.env.example`)。 + 无 Docker 时脚本会**自动回退**宿主机模式(`--host` 可强制)。 | 物理路径 | 内容 | diff --git a/pull-and-restart.sh b/pull-and-restart.sh index b6d37bc..963ef75 100644 --- a/pull-and-restart.sh +++ b/pull-and-restart.sh @@ -1,7 +1,11 @@ #!/usr/bin/env bash # AI 建站 — 拉取代码并重启 # 用法:./pull-and-restart.sh -# 环境变量:GIT_BRANCH(默认 master)、FORCE_GIT_RESET(默认 1,与远程硬对齐) +# 默认仓库:https://gitea.yuxindazhineng.com/whm/ai_site.git +# 环境变量: +# AIJZ_GIT_URL 覆盖仓库地址 +# GIT_BRANCH 分支(默认 main) +# FORCE_GIT_RESET 默认 1,与远程硬对齐 set -euo pipefail ROOT="$(cd "$(dirname "$0")" && pwd)" cd "$ROOT" @@ -11,10 +15,12 @@ cd "$ROOT" echo "========================================" echo " AI 建站 拉取并重启" echo " 路径: $ROOT" +echo " 仓库: ${AIJZ_GIT_URL:-https://gitea.yuxindazhineng.com/whm/ai_site.git}" +echo " 分支: ${GIT_BRANCH:-main}" echo "========================================" ensure_docker -git_pull_hard || true +git_pull_hard stack_down sleep 1 stack_up 1 diff --git a/scripts/lib-aijz-deploy.sh b/scripts/lib-aijz-deploy.sh index 782f182..68bef19 100644 --- a/scripts/lib-aijz-deploy.sh +++ b/scripts/lib-aijz-deploy.sh @@ -384,18 +384,57 @@ ${pub_line} EOF } -git_pull_hard() { - local branch="${GIT_BRANCH:-master}" - echo "拉取代码(branch=$branch)..." - if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then - echo "错误: 当前不是 git 仓库,跳过拉取。" >&2 +# 默认代码源(可用 AIJZ_GIT_URL / GIT_BRANCH 覆盖) +AIJZ_GIT_URL_DEFAULT="https://gitea.yuxindazhineng.com/whm/ai_site.git" + +ensure_git_origin() { + local url="${1:-${AIJZ_GIT_URL:-$AIJZ_GIT_URL_DEFAULT}}" + if ! command -v git >/dev/null 2>&1; then + echo "错误: 未安装 git,无法拉取代码。请先安装 git。" >&2 + return 1 + fi + if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + echo "当前目录不是 git 仓库,初始化并关联 origin -> $url" + git init + git remote add origin "$url" + return 0 + fi + if git remote get-url origin >/dev/null 2>&1; then + local cur + cur="$(git remote get-url origin)" + if [ "$cur" != "$url" ]; then + echo "更新 origin: $cur -> $url" + git remote set-url origin "$url" + fi + else + echo "添加 origin -> $url" + git remote add origin "$url" + fi +} + +# 从 Gitea ai_site 拉取并与远程对齐(默认硬重置,保留未跟踪文件如 .env) +# 环境变量: +# AIJZ_GIT_URL 仓库地址(默认 https://gitea.yuxindazhineng.com/whm/ai_site.git) +# GIT_BRANCH 分支(默认 main) +# FORCE_GIT_RESET 1=reset --hard 对齐远程(默认 1);0=仅快进合并 +git_pull_hard() { + local url="${AIJZ_GIT_URL:-$AIJZ_GIT_URL_DEFAULT}" + local branch="${GIT_BRANCH:-main}" + echo "拉取代码($url branch=$branch)..." + ensure_git_origin "$url" || return 1 + if ! git fetch origin --progress; then + echo "错误: git fetch 失败。私有仓库请配置凭据,或设置 AIJZ_GIT_URL 为带 token 的地址。" >&2 + return 1 + fi + if ! git rev-parse --verify "origin/$branch" >/dev/null 2>&1; then + echo "错误: 远程不存在分支 origin/$branch" >&2 return 1 fi - git fetch origin --progress if [ "${FORCE_GIT_RESET:-1}" = "1" ]; then + git checkout -B "$branch" "origin/$branch" git reset --hard "origin/$branch" else - git checkout "$branch" 2>/dev/null || true + git checkout "$branch" 2>/dev/null || git checkout -b "$branch" "origin/$branch" git merge --ff-only "origin/$branch" fi echo "当前: $(git rev-parse --short HEAD) $(git log -1 --pretty=%s)" diff --git a/start.sh b/start.sh index 974325d..f29bce4 100644 --- a/start.sh +++ b/start.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash # AI 建站 — Linux 一键启动(Docker Compose) -# 用法:cd 项目根 && ./start.sh [--rebuild] +# 用法:cd 项目根 && ./start.sh [--rebuild] [--no-pull] +# 默认从 Gitea 拉取最新代码再启动(可用 --no-pull / AIJZ_SKIP_GIT_PULL=1 跳过) # 行尾:LF set -euo pipefail ROOT="$(cd "$(dirname "$0")" && pwd)" @@ -9,21 +10,35 @@ cd "$ROOT" . "$ROOT/scripts/lib-aijz-deploy.sh" REBUILD=0 +DO_PULL=1 for a in "$@"; do case "$a" in --rebuild|-r) REBUILD=1 ;; + --no-pull) DO_PULL=0 ;; + --pull) DO_PULL=1 ;; -h|--help) - echo "用法: $0 [--rebuild]" + echo "用法: $0 [--rebuild] [--no-pull|--pull]" + echo " 默认拉取: ${AIJZ_GIT_URL:-https://gitea.yuxindazhineng.com/whm/ai_site.git} 分支 ${GIT_BRANCH:-main}" + echo " 跳过拉取: --no-pull 或 AIJZ_SKIP_GIT_PULL=1" exit 0 ;; esac done +if [ "${AIJZ_SKIP_GIT_PULL:-0}" = "1" ]; then + DO_PULL=0 +fi echo "========================================" echo " AI 建站 启动" echo " 路径: $ROOT" echo "========================================" +if [ "$DO_PULL" = "1" ]; then + git_pull_hard +else + echo "已跳过代码拉取(--no-pull / AIJZ_SKIP_GIT_PULL)" +fi + ensure_docker stack_up "$REBUILD" healthcheck