diff --git a/.dockerignore b/.dockerignore index 456b108..d5d1707 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,9 +1,13 @@ .git .gitignore +.github +.env .pytest_cache .venv __pycache__ *.pyc +*.db +backups data tests diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..36df2af --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,60 @@ +name: docker-image + +on: + push: + tags: + - "v*" + +env: + REGISTRY_HOST: code.wanderingbadger.dev + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push: + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Verify tag commit is on main + run: | + git fetch origin main --no-tags + TAG_COMMIT="${GITHUB_SHA}" + MAIN_COMMIT="$(git rev-parse origin/main)" + + if ! git merge-base --is-ancestor "$TAG_COMMIT" "$MAIN_COMMIT"; then + echo "Tag ${GITHUB_REF_NAME} does not point to a commit reachable from origin/main" + exit 1 + fi + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + with: + platforms: amd64,arm64 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log in to Gitea Container Registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY_HOST }} + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_TOKEN }} + + - name: Build and push multi-arch image + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: true + provenance: false + sbom: false + tags: | + ${{ env.REGISTRY_HOST }}/${{ env.IMAGE_NAME }}:${{ github.ref_name }} + ${{ env.REGISTRY_HOST }}/${{ env.IMAGE_NAME }}:latest \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..b6dfe53 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,28 @@ +name: test + +on: + push: + branches: + - "**" + +jobs: + pytest: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + cache: pip + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run pytest + run: pytest \ No newline at end of file diff --git a/README.md b/README.md index d693117..1b07bb1 100644 --- a/README.md +++ b/README.md @@ -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` + +### CI:branch 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 +- 不要求额外配置测试环境变量 + +### CD:tag 发布 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 导入 项目内附带了一个一次性迁移脚本: