161 lines
5.4 KiB
Bash
Executable File
161 lines
5.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
||
# =============================================================================
|
||
# deploy.sh —— 把工作目录里验收通过的 custom/ 同步到线上 Gitea
|
||
#
|
||
# 默认:只做预演,打印动作,不修改线上文件。
|
||
# 真正执行:必须显式加 --yes;只有 QQ 明确同意后才能运行。
|
||
# =============================================================================
|
||
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,8p' "$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' "$*"; }
|
||
fail() { printf '\033[31m[错误]\033[0m %s\n' "$*" >&2; exit 1; }
|
||
|
||
[ -d "$SRC_CUSTOM" ] || fail "找不到 $SRC_CUSTOM"
|
||
for required in \
|
||
templates/base/footer_content.tmpl \
|
||
public/assets/css/qq-pages.css \
|
||
public/assets/css/theme-qq-auto.css \
|
||
public/assets/css/theme-qq-light.css \
|
||
public/assets/css/theme-qq-dark.css; do
|
||
[ -f "$SRC_CUSTOM/$required" ] || fail "源码缺少关键文件:custom/$required"
|
||
done
|
||
|
||
say "源目录:$SRC_CUSTOM"
|
||
find "$SRC_CUSTOM" -type f | sed "s|$SRC_CUSTOM/| custom/|" | sort
|
||
|
||
if [ "$DO_IT" -eq 0 ]; then
|
||
say "预演:加 --yes 后会执行"
|
||
echo " 1) 完整备份 $DST_CUSTOM 与 $APP_INI -> $BACKUP"
|
||
echo " 2) 校验备份含 app.ini、QQ 主题与关键模板"
|
||
echo " 3) rsync --delete-delay 同步 custom/(保留线上额外的 theme-*.css)"
|
||
echo " 4) 修正权限、重启并执行 HTTP 健康检查"
|
||
echo " 5) 任一步骤失败时调用 rollback.sh 精确回滚"
|
||
[ "$PATCH_INI" -eq 1 ] && echo " 6) 同步改写 $APP_INI 的 [ui] DEFAULT_THEME / THEMES"
|
||
echo
|
||
warn "线上未做任何修改。获得 QQ 明确批准后才能执行:./scripts/deploy.sh --yes"
|
||
exit 0
|
||
fi
|
||
|
||
say "完整备份到 $BACKUP"
|
||
sudo mkdir -p /var/backups
|
||
sudo tar czf "$BACKUP" -C / var/lib/gitea/custom etc/gitea/app.ini
|
||
sudo chmod 600 "$BACKUP"
|
||
sudo tar tzf "$BACKUP" >/dev/null
|
||
for required in \
|
||
var/lib/gitea/custom/templates/base/footer_content.tmpl \
|
||
var/lib/gitea/custom/public/assets/css/qq-pages.css \
|
||
var/lib/gitea/custom/public/assets/css/theme-qq-auto.css \
|
||
var/lib/gitea/custom/public/assets/css/theme-qq-light.css \
|
||
var/lib/gitea/custom/public/assets/css/theme-qq-dark.css \
|
||
etc/gitea/app.ini; do
|
||
sudo tar tzf "$BACKUP" | grep -Fx "$required" >/dev/null || fail "备份缺少关键文件:$required"
|
||
done
|
||
say "备份校验通过:$(sudo du -h "$BACKUP" | cut -f1)"
|
||
|
||
rollback_on_failure() {
|
||
trap - ERR
|
||
warn "部署验证失败,立即从 $BACKUP 精确回滚"
|
||
"$REPO_DIR/scripts/rollback.sh" --yes "$BACKUP" || {
|
||
printf '\033[31m[严重]\033[0m 自动回滚失败,请立即人工处理。备份:%s\n' "$BACKUP" >&2
|
||
exit 1
|
||
}
|
||
fail "部署失败,已恢复到部署前状态"
|
||
}
|
||
trap 'rollback_on_failure' ERR
|
||
|
||
say "同步 custom/ -> $DST_CUSTOM/"
|
||
sudo mkdir -p "$DST_CUSTOM"
|
||
# theme-*.css 可能是管理员另外安装的旧主题;保护它们不被 --delete 删除,
|
||
# 同名 QQ 主题仍会从源码正常更新。
|
||
if ! sudo rsync -a --delete-delay \
|
||
--exclude='.git*' \
|
||
--filter='protect public/assets/css/theme-*.css' \
|
||
"$SRC_CUSTOM/" "$DST_CUSTOM/"; then
|
||
rollback_on_failure
|
||
fi
|
||
sudo chown -R git:git "$DST_CUSTOM"
|
||
|
||
if [ "$PATCH_INI" -eq 1 ]; then
|
||
say "改写 $APP_INI 的 [ui] 段"
|
||
sudo cp -a "$APP_INI" "$APP_INI.bak-$STAMP"
|
||
if ! sudo python3 - "$APP_INI" <<'PY'
|
||
import pathlib
|
||
import sys
|
||
|
||
p = pathlib.Path(sys.argv[1])
|
||
txt = p.read_text(encoding="utf-8")
|
||
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 line in lines:
|
||
stripped = line.strip()
|
||
if stripped.startswith("[") and stripped.endswith("]"):
|
||
in_ui = stripped == "[ui]"
|
||
if in_ui:
|
||
if stripped.lower().startswith("default_theme"):
|
||
out.append("DEFAULT_THEME = qq-auto")
|
||
seen.add("default")
|
||
continue
|
||
if stripped.lower().startswith("themes"):
|
||
out.append("THEMES = " + ",".join(themes))
|
||
seen.add("themes")
|
||
continue
|
||
out.append(line)
|
||
if "default" not in seen or "themes" not in seen:
|
||
updated = []
|
||
for line in out:
|
||
updated.append(line)
|
||
if line.strip() == "[ui]":
|
||
if "default" not in seen:
|
||
updated.append("DEFAULT_THEME = qq-auto")
|
||
if "themes" not in seen:
|
||
updated.append("THEMES = " + ",".join(themes))
|
||
out = updated
|
||
p.write_text("\n".join(out) + "\n", encoding="utf-8")
|
||
PY
|
||
then
|
||
rollback_on_failure
|
||
fi
|
||
fi
|
||
|
||
say "重启 Gitea"
|
||
if ! sudo systemctl restart gitea; then
|
||
rollback_on_failure
|
||
fi
|
||
|
||
healthy=0
|
||
for _ in 1 2 3 4 5; do
|
||
if systemctl is-active --quiet gitea && \
|
||
(! command -v curl >/dev/null || curl -fsS -o /dev/null https://git.qyhhh.top/); then
|
||
healthy=1
|
||
break
|
||
fi
|
||
sleep 2
|
||
done
|
||
[ "$healthy" -eq 1 ] || rollback_on_failure
|
||
|
||
say "部署完成,Gitea 运行正常"
|
||
echo "回滚命令:./scripts/rollback.sh $BACKUP"
|