add ci/cd
test / pytest (push) Failing after 54s

This commit is contained in:
2026-04-21 21:49:16 +02:00
parent 8fa3dace79
commit 5aa87f60ad
4 changed files with 203 additions and 1 deletions
+111 -1
View File
@@ -373,7 +373,7 @@ ls -lh data
### 5. 容器更新后数据为什么没丢
因为数据库不放在镜像里,而是放在宿主机挂载目录 `DATA_DIR` 中。
因为数据库不放在镜像里,而是放在宿主机挂载目录 `DATA_DIR` 中。
镜像更新只会替换应用代码和运行环境,不会覆盖这个宿主机目录。
## 测试
@@ -394,6 +394,116 @@ python -m pytest
- 创建后的重定向行为
- 关键页面结构和 UX 文案
## CI / CD
仓库现在包含两条基础自动化流程,文件位于:
- `.github/workflows/test.yml`
- `.github/workflows/docker-image.yml`
### CIbranch push 自动跑 pytest
`test.yml` 会在任意 branch 的 `push` 上执行:
1. checkout 代码
2. 使用 Python `3.12`
3. 安装 `requirements.txt`
4. 运行 `pytest`
当前测试不依赖外部数据库服务。
测试使用 `tmp_path` 创建独立 SQLite 文件,并通过 `configure_database(...)` 切换到临时数据库,因此:
- 不会污染 `./data/app.db`
- 不要求额外启动 Docker 或 Compose
- 不要求额外配置测试环境变量
### CDtag 发布 Docker image
`docker-image.yml` 会在推送符合 `v*` 格式的 tag 时触发,例如:
- `v1.0.0`
- `v1.2.3`
workflow 会先校验该 tag 指向的提交是否可从 `origin/main` 到达;只有满足这个条件的 tag 才会继续构建并推送镜像。
镜像发布目标:
- Registry Host: `code.wanderingbadger.dev`
- Image Name: `${{ github.repository }}`
- Platforms:
- `linux/amd64`
- `linux/arm64`
推送的 tag 策略:
- `${tag}`
- `latest`
例如仓库名为 `tliu93/2026-moving-helper`,打出 `v1.0.0` 后会推送:
```text
code.wanderingbadger.dev/tliu93/2026-moving-helper:v1.0.0
code.wanderingbadger.dev/tliu93/2026-moving-helper:latest
```
### Actions / Gitea Secrets
需要在仓库的 Actions secrets 中配置:
- `REGISTRY_USERNAME`
- `REGISTRY_TOKEN`
推荐含义:
- `REGISTRY_USERNAME`: Gitea 用户名
- `REGISTRY_TOKEN`: 具备 Container Registry 推送权限的 Access Token
如果你的 Gitea 实例对 package / registry 权限做了单独控制,确保这个 token 至少具备对应仓库的镜像推送权限。
### 如何触发镜像发布
建议流程:
```bash
git checkout main
git pull --ff-only origin main
git tag v1.0.0
git push origin main --tags
```
如果只想推送单个 tag
```bash
git push origin v1.0.0
```
### 本地手动构建镜像
单架构本地构建:
```bash
docker build -t moving-helper:local .
```
本地运行:
```bash
docker run --rm -p 10000:10000 \
-e DATABASE_URL=sqlite:////app/data/app.db \
moving-helper:local
```
如果要模拟发布时的多架构构建,可以使用 buildx:
```bash
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t code.wanderingbadger.dev/${USER}/2026-moving-helper:test \
--load \
.
```
## 一次性 Notion 导入
项目内附带了一个一次性迁移脚本: