From ea3c288df73c4b60e35b94213ec102747eb1e7e6 Mon Sep 17 00:00:00 2001 From: KenanZhu Date: Wed, 4 Nov 2026 10:18:24 +0800 Subject: [PATCH] Preserve production website unit with deployment drop-in --- .gitattributes | 1 + deploy/autolibrary-webserver.service | 19 ------------------ deploy/systemd/10-gitea-release.conf | 4 ++++ docs/gitea-deployment.md | 30 ++++++++++++++++------------ 4 files changed, 22 insertions(+), 32 deletions(-) delete mode 100644 deploy/autolibrary-webserver.service create mode 100644 deploy/systemd/10-gitea-release.conf diff --git a/.gitattributes b/.gitattributes index 02aed88..8b1dcdf 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,4 +2,5 @@ /frontend/** text=auto eol=lf *.sh text eol=lf *.service text eol=lf +/deploy/systemd/*.conf text eol=lf *.yml text eol=lf diff --git a/deploy/autolibrary-webserver.service b/deploy/autolibrary-webserver.service deleted file mode 100644 index e0ab30e..0000000 --- a/deploy/autolibrary-webserver.service +++ /dev/null @@ -1,19 +0,0 @@ -[Unit] -Description=AutoLibrary Website -After=network.target - -[Service] -Type=exec -User=autolibrary -Group=autolibrary -WorkingDirectory=/opt/autolibrary/current -ExecStart=/opt/autolibrary/current/webserver start --config /opt/autolibrary/shared/config.json -Environment=GIN_MODE=release -UMask=0027 -NoNewPrivileges=true -Restart=on-failure -RestartSec=5s -TimeoutStopSec=20s - -[Install] -WantedBy=multi-user.target diff --git a/deploy/systemd/10-gitea-release.conf b/deploy/systemd/10-gitea-release.conf new file mode 100644 index 0000000..f9647ed --- /dev/null +++ b/deploy/systemd/10-gitea-release.conf @@ -0,0 +1,4 @@ +[Service] +WorkingDirectory=/opt/autolibrary/current +ExecStart= +ExecStart=/opt/autolibrary/current/webserver start --config /opt/autolibrary/shared/config.json diff --git a/docs/gitea-deployment.md b/docs/gitea-deployment.md index 2a9ac51..8c5dcc4 100644 --- a/docs/gitea-deployment.md +++ b/docs/gitea-deployment.md @@ -6,34 +6,37 @@ 安装 Bash、Git、curl、sudo、`build-essential`、`util-linux`、Go 1.25.6 或更新版本、Node.js 22.13 或更新版本。确认 Runner 的服务环境中能找到 `git`、`node`、`npm`、`go` 和 `gcc`,并能访问 Gitea、GitHub 上固定版本的 checkout Action、npm registry 和 Go 模块代理。 -部署使用两个专用账号。首次创建账号及目录时运行以下命令。已有同名账号时复用,并确认组和权限与示例相符。 +网站继续使用现有的 `autolibrary` 账号。首次接入时创建专用部署账号和发布目录。已有部署账号时复用,并确认组和权限与示例相符。 ```sh sudo useradd --system --user-group --create-home --home-dir /var/lib/autolibrary-runner autolibrary-deploy -sudo useradd --system --user-group --no-create-home autolibrary sudo chmod 0700 /var/lib/autolibrary-runner sudo install -d -o root -g autolibrary-deploy -m 1775 /opt/autolibrary sudo install -d -o autolibrary-deploy -g autolibrary-deploy -m 0755 /opt/autolibrary/releases -sudo install -d -o root -g autolibrary -m 0771 /opt/autolibrary/shared -sudo install -d -o autolibrary -g autolibrary -m 0750 /opt/autolibrary/shared/logs +sudo install -d -o root -g autolibrary -m 0751 /opt/autolibrary/shared +sudo ln -s /home/autolibrary/logs /opt/autolibrary/shared/logs ``` -从现有站点迁移时,按 [Ubuntu 部署说明](migration-to-ubuntu.md) 停服备份,再迁入现有配置和数据。新站点可以从 `config.example.json` 起步并填入实际参数。生产配置路径为 `/opt/autolibrary/shared/config.json`,文件权限设为 `0640`,所有者设为 `root:autolibrary`。配置应包括: +现网的基础 unit 为 `autolibrary-webserver.service`,运行账号为 `autolibrary`,原程序和配置位于 `/home/autolibrary`。按 [Ubuntu 部署说明](migration-to-ubuntu.md) 备份后,将原配置复制到 `/opt/autolibrary/shared/config.json`,设为 `root:autolibrary`、权限 `0640`。调整副本中的路径: -- SQLite 数据库和主题文件使用发布目录之外的绝对路径,例如 `/opt/autolibrary/shared/themes.db` 与 `/opt/autolibrary/shared/themes_files`。运行用户 `autolibrary` 需要对数据库所在目录和主题文件目录有写权限。 -- `frontend.host` 监听 `127.0.0.1`,`frontend.static.path` 使用 `./frontend/dist`。生产端口与现有 Nginx upstream 对齐。 -- 保留生产 API Key、Casdoor 配置及独立运行的 Casdoor 服务。启用 API Key 校验时填写实际的 Key。 +- 将三个 SQLite 数据库和主题文件目录的路径改为现有 `/home/autolibrary/backend/data/` 下的绝对路径,继续使用原数据。运行用户需要对数据库所在目录和主题文件目录有写权限。 +- 将 `frontend.static.path` 改为 `./frontend/dist`,让程序从本次发布目录读取资源。沿用前端 `3003`、后端 `8080` 及现有监听地址。 +- 保留现有 API Key 和 `backend.casdoor` 配置。网站通过 `http://127.0.0.1:8000` 使用 Casdoor 处理账户和认证。现网的 `autolibrary-casdoor.service` 独立运行,发布任务只重启 `autolibrary-webserver.service`。 -已有 `current` 时,它应为指向上一版的符号链接。核实上一版可用新 unit 和共享配置启动,并把该链接本身交给部署账号(`sudo chown -h autolibrary-deploy:autolibrary-deploy /opt/autolibrary/current`),以便部署替换和回滚。若原站使用其他 unit,在第一次自动发布前停止并禁用该旧 unit,避免争用端口。 +首次接入时,以部署账号创建指向原站的链接,作为首次发布失败时的回退目标。已有 `current` 时,确认链接指向可用的上一版并由部署账号拥有。 + +```sh +sudo -u autolibrary-deploy ln -s /home/autolibrary /opt/autolibrary/current +``` ## 2. 安装网站服务和 Runner -从网站源码目录安装 unit。它在首次发布时启动。 +在现有网站 unit 上安装 drop-in,只调整工作目录和启动命令。原 unit 继续提供运行账号、重启策略和 `/home/autolibrary/logs/errors.log` 的 stderr 设置。新服务器可先按 [Ubuntu 部署说明](migration-to-ubuntu.md#4-systemd) 安装基础 unit。已有站点执行 `daemon-reload` 后,覆盖配置会在下一次网站服务启动时生效。 ```sh -sudo install -m 0644 deploy/autolibrary-webserver.service /etc/systemd/system/autolibrary-webserver.service +sudo install -d -m 0755 /etc/systemd/system/autolibrary-webserver.service.d +sudo install -m 0644 deploy/systemd/10-gitea-release.conf /etc/systemd/system/autolibrary-webserver.service.d/10-gitea-release.conf sudo systemctl daemon-reload -sudo systemctl enable autolibrary-webserver.service sudo visudo -f /etc/sudoers.d/autolibrary-deploy ``` @@ -86,6 +89,7 @@ git push -u origin main ## 排查和回滚 - 任务排队时,检查仓库 Actions 设置、Runner 在线状态和标签。 -- 重启或健康检查失败时,检查 Actions 日志和 `sudo journalctl -u autolibrary-webserver.service -n 100`。检查端口、共享配置、数据路径和权限。 +- 重启或健康检查失败时,检查 Actions 日志、`sudo journalctl -u autolibrary-webserver.service -n 100` 和 `/home/autolibrary/logs/errors.log`。检查端口、共享配置、数据路径和权限。 +- 网站发布检查覆盖首页和网站后端。排查账户或登录时,分别查看 `systemctl status autolibrary-casdoor.service` 和 `curl -fsS http://127.0.0.1:8000/.well-known/openid-configuration`。 - 发生业务回归时,用 `git revert` 后推送 `main` 重新构建上一份可用代码。紧急切回已有目录时,先确保 Runner 没有在部署,再按 [Ubuntu 部署说明](migration-to-ubuntu.md#回滚) 切换 `current`。清理历史发布目录前确认其未被 `current` 使用,并至少保留一个可用的上一版。 - 自动回滚切换的是程序和静态文件。涉及数据库结构变化时,上线前备份共享数据,并确认旧版本兼容变更后的结构。