Files
gaochengyu 73ab36f7ff feat: 开源母线电容计算器并接入独立自动部署
整理 Buck 与 PMSM 完整源码、离线构建、校核测试和使用说明,采用 MIT 许可。
增加页首开源链接,支持推送 main 后自动测试、构建并发布到原计算器网址。
2026-11-27 04:38:28 +08:00

77 lines
2.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 自动部署
计算器使用独立的 Gitea Webhook 和发布目录,与博客构建分开。在线入口仍为:
<https://blog.ggodname.top/tools/impedance-calculators.html>
## 流程
1. Gitea 向 `/_deploy/bus-capacitor-calculator` 发送 main 分支的 push 事件。
2. `webhook.py` 校验请求体 HMAC、仓库 ID 和全名,将事件写入队列。
3. systemd path 唤醒单个 `worker.py`,拉取当前 main,运行核心测试、部署回归测试及构建。
4. 发布器检查输出,写入独立版本目录,再原子切换 `current`。失败或候选版本已过期时,不切换线上版本。
公开仓库使用匿名 HTTPS 拉取,不需要 Git 写入令牌。Webhook 密钥只保存在服务器配置和 Gitea 的 Hook 设置中,不进入仓库。
## 安装位置
随附的 systemd 单元使用现有部署账号 `codex`,默认目录如下。部署到其他机器时,修改账号、目录和 `runtime_dir`。
```text
/srv/bus-capacitor/automation/
├── webhook.py
├── worker.py
├── config.json # 从 config.example.json 生成,权限 600
├── state/
├── builds/
├── logs/
└── repository.git/
/var/www/bus-capacitor/
├── releases/
└── current -> releases/某个通过检查的版本
```
服务器需要 Python 3.12+、Git、Node.js 22+ 和 systemd。Web 服务账号只需读取发布目录。
复制发布器和配置后,将三个服务单元安装到 `/etc/systemd/system/`,执行:
```bash
systemctl daemon-reload
systemctl enable --now bus-capacitor-webhook.service bus-capacitor-deploy.path
```
在 Gitea 为本仓库创建 push Webhook,URL 填 `https://blog.ggodname.top/_deploy/bus-capacitor-calculator`,内容类型为 `application/json`,密钥与服务器配置一致。
## Caddy 路由
将下面路由放在博客默认 `handle` 之前。旧计算器 URL 直接读取独立发布目录,因此博客重新发布不会覆盖计算器。
```caddyfile
handle /_deploy/bus-capacitor-calculator {
reverse_proxy 127.0.0.1:19082
}
@bus_capacitor_files path /tools/impedance-calculators.html /tools/buck-rlc-calculator.html /tools/motor-impedance-calculator.html
handle @bus_capacitor_files {
uri strip_prefix /tools
root * /var/www/bus-capacitor/current
encode gzip zstd
header Cache-Control "no-cache"
file_server
}
handle /tools/calculator-deployment.json {
rewrite * /deployment.json
root * /var/www/bus-capacitor/current
header Cache-Control "no-cache"
file_server
}
```
检查 `state/last-attempt.json`、`state/last-success.json` 和 `logs/` 可以查看构建结果。公网版本号可读取:
<https://blog.ggodname.top/tools/calculator-deployment.json>
修改计算器源码只需推送 main;修改 Webhook 或发布器本身后,需要将控制程序同步到安装目录并重启相应服务。