#!/usr/bin/env bash # ============================================================================= # deploy.sh —— 把工作目录里验收通过的 custom/ 拷进线上 Gitea # # 默认:只做「预演」(dry-run),打印将要发生的一切,不动线上任何文件。 # 真正执行:必须显式加 --yes。 # # 用法: # ./scripts/deploy.sh # 预演,安全 # ./scripts/deploy.sh --yes # 真部署(备份 → 复制 → chown → 重启) # ./scripts/deploy.sh --yes --patch-ini # 同时把 app.ini 的 [ui] 主题列表换成 qq 系列 # # 铁律: # 1. 任何写入前先备份 /var/lib/gitea/custom 与 /etc/gitea/app.ini,备份落在 # /var/backups/gitea-custom-<时间戳>.tar.gz(含恢复脚本说明)。 # 2. 只覆盖 custom/,绝不触碰 /usr/local/bin/gitea 与数据库。 # 3. 只有 QQ 明确同意后才能加 --yes 运行。 # ============================================================================= set -euo pipefail REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" SRC_CUSTOM="$REPO_DIR/custom" DST_CUSTOM="/var/lib/gitea/custom" APP_INI="/etc/gitea/app.ini" STAMP="$(date +%Y%m%d-%H%M%S)" BACKUP="/var/backups/gitea-custom-$STAMP.tar.gz" DO_IT=0 PATCH_INI=0 for arg in "$@"; do case "$arg" in --yes) DO_IT=1 ;; --patch-ini) PATCH_INI=1 ;; -h|--help) sed -n '2,20p' "$0"; exit 0 ;; *) echo "未知参数: $arg" >&2; exit 2 ;; esac done say() { printf '\033[36m==>\033[0m %s\n' "$*"; } warn() { printf '\033[33m[!]\033[0m %s\n' "$*"; } [ -d "$SRC_CUSTOM" ] || { echo "找不到 $SRC_CUSTOM" >&2; exit 1; } # ---------------------------------------------------------------- 1. 内容清单 say "源目录:$SRC_CUSTOM" find "$SRC_CUSTOM" -type f | sed "s|$SRC_CUSTOM/| custom/|" | sort if [ "$DO_IT" -eq 0 ]; then say "预演:下面这些动作会在加 --yes 后真正执行" echo " 1) tar 备份 $DST_CUSTOM 与 $APP_INI -> $BACKUP" echo " 2) rsync 复制 custom/ -> $DST_CUSTOM/" echo " 3) chown -R git:git $DST_CUSTOM" echo " 4) systemctl restart gitea" [ "$PATCH_INI" -eq 1 ] && echo " 5) 改写 $APP_INI 的 [ui] DEFAULT_THEME / THEMES(先备份 .bak-$STAMP)" echo warn "线上未做任何修改。确认外观无误后,再由 QQ 批准执行:./scripts/deploy.sh --yes" exit 0 fi # ---------------------------------------------------------------- 2. 备份 say "备份到 $BACKUP" sudo mkdir -p /var/backups sudo tar czf "$BACKUP" -C / \ --exclude='custom/public/assets/css/theme-qq*.css' \ var/lib/gitea/custom etc/gitea/app.ini sudo chmod 600 "$BACKUP" say "备份完成:$(sudo du -h "$BACKUP" | cut -f1)" # ---------------------------------------------------------------- 3. 复制 say "复制 custom/ -> $DST_CUSTOM/" sudo mkdir -p "$DST_CUSTOM" sudo rsync -a --exclude='.git*' "$SRC_CUSTOM/" "$DST_CUSTOM/" sudo chown -R git:git "$DST_CUSTOM" say "复制完成" # ---------------------------------------------------------------- 4. app.ini 可选 if [ "$PATCH_INI" -eq 1 ]; then say "改写 $APP_INI 的 [ui] 段" sudo cp -a "$APP_INI" "$APP_INI.bak-$STAMP" sudo python3 - "$APP_INI" <<'PY' import sys, re, pathlib p = pathlib.Path(sys.argv[1]) txt = p.read_text() themes = ["auto", "gitea", "arc-green", "gitea-gruvbox", "github-auto", "github-light", "github-dark", "github-soft-dark", "qq-auto", "qq-light", "qq-dark"] lines = txt.splitlines() out, in_ui = [], False seen = set() for ln in lines: s = ln.strip() if s.startswith("[") and s.endswith("]"): in_ui = (s == "[ui]") if in_ui: if s.lower().startswith("default_theme"): out.append("DEFAULT_THEME = qq-auto"); seen.add("default"); continue if s.lower().startswith("themes"): out.append("THEMES = " + ",".join(themes)); seen.add("themes"); continue out.append(ln) if "default" not in seen or "themes" not in seen: # [ui] 段里缺字段:插到段首 res = [] for ln in out: res.append(ln) if ln.strip() == "[ui]": if "default" not in seen: res.append("DEFAULT_THEME = qq-auto") if "themes" not in seen: res.append("THEMES = " + ",".join(themes)) out = res p.write_text("\n".join(out) + "\n") print("app.ini [ui] 已更新") PY say "app.ini 已更新(原文件备份为 $APP_INI.bak-$STAMP)" fi # ---------------------------------------------------------------- 5. 重启 say "重启 gitea" sudo systemctl restart gitea sleep 2 systemctl is-active gitea && say "gitea 运行中" command -v curl >/dev/null && curl -s -o /dev/null -w "HTTP %{http_code}\n" https://git.qyhhh.top/ || true cat <