From db184c32b5a255d469bcff56de8ac1a34dad7e13 Mon Sep 17 00:00:00 2001 From: cfdaily Date: Sat, 6 Jun 2026 13:58:47 +0800 Subject: [PATCH] fix: rename GITEA_TOKEN to CI_TOKEN in workflows (Gitea reserves GITEA_* prefix) --- .gitea/workflows/ci.yml | 92 ++++++++++++++++++++++++++++++++ .gitea/workflows/deploy.yml | 103 ++++++++++++++++++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 .gitea/workflows/ci.yml create mode 100644 .gitea/workflows/deploy.yml diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..066b445 --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,92 @@ +# CI 管道 — moziplus v2.0 +# +# 触发条件: +# - push(非 main 分支) +# - pull_request(opened, synchronize) +# +# Gitea v1.23.4 限制注意: +# - 不支持 failure() 表达式,用 always() + shell 条件判断替代 +# - 不支持 concurrency / continue-on-error / timeout-minutes / permissions +# - 无内置 CI_TOKEN,需手动配置 PAT 为 secret +# - runs-on 只支持单个 label + +name: CI + +on: + push: + branches: + - '**' + - '!main' + pull_request: + types: [opened, synchronize] + +jobs: + # ── Job 1: Lint ────────────────────────────────────── + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup Python + run: | + python3 -m venv .venv + .venv/bin/pip install --quiet flake8 + + - name: Lint with flake8 + run: | + .venv/bin/flake8 src/ --max-line-length=120 --extend-ignore=E501 + + # ── Job 2: Test ────────────────────────────────────── + test: + runs-on: ubuntu-latest + needs: lint + steps: + - uses: actions/checkout@v4 + + - name: Setup Python + run: | + python3 -m venv .venv + .venv/bin/pip install --quiet -r requirements.txt + + - name: Run tests (exclude E2E) + run: | + .venv/bin/pytest tests/ -m "not e2e" -x -q + + # ── Job 3: CI 失败通知 ─────────────────────────────── + # v1.23 不支持 failure(),用 always() + shell 检查 commit status 替代 + notify-on-failure: + runs-on: ubuntu-latest + needs: [lint, test] + if: always() + steps: + - name: Check results and notify + env: + CI_TOKEN: ${{ secrets.CI_TOKEN }} + run: | + # 查询当前 commit 的 status + STATUS=$(curl -sf \ + -H "Authorization: token $CI_TOKEN" \ + "${{ gitea.api_url }}/repos/${{ gitea.repository }}/commits/${{ gitea.sha }}/status" \ + | python3 -c "import sys,json; print(json.load(sys.stdin).get('state',''))" 2>/dev/null || echo "") + + echo "Commit status: $STATUS" + + if [ "$STATUS" != "success" ]; then + echo "CI failed or status unknown, sending notification..." + + # 如果是 PR 事件,写评论通知 + PR_NUMBER="${{ gitea.event.pull_request.number }}" + if [ -n "$PR_NUMBER" ]; then + curl -sf -X POST \ + -H "Authorization: token $CI_TOKEN" \ + -H "Content-Type: application/json" \ + "${{ gitea.api_url }}/repos/${{ gitea.repository }}/issues/${PR_NUMBER}/comments" \ + -d "{\"body\": \"❌ **CI 失败**\\n\\n请检查 CI 日志并修复。\\n\\n触发 commit: \`${{ gitea.sha }}\`\"}" \ + || echo "Failed to post PR comment" + echo "PR comment posted." + else + echo "Not a PR event, skipping PR comment." + fi + else + echo "CI passed, no notification needed." + fi diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..053f97b --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,103 @@ +# 部署管道 — moziplus v2.0 +# +# 触发条件: +# - push 到 main 分支 +# +# Gitea v1.23.4 限制注意: +# - 不支持 failure() 表达式 +# - 不支持 concurrency / permissions +# - 部署脚本占位,等姜维确认 act-runner 环境后再补具体命令 + +name: Deploy + +on: + push: + branches: [main] + +jobs: + # ── Job 1: CI(main 分支跑完整测试)───────────────── + ci: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup Python + run: | + python3 -m venv .venv + .venv/bin/pip install --quiet -r requirements.txt + + - name: Lint + run: | + .venv/bin/flake8 src/ --max-line-length=120 --extend-ignore=E501 + + - name: Unit & Integration Tests + run: | + .venv/bin/pytest tests/ -m "not e2e" -x -q + + # ── Job 2: 部署 ───────────────────────────────────── + deploy: + runs-on: ubuntu-latest + needs: ci + steps: + - uses: actions/checkout@v4 + + - name: Record current version + run: | + echo "Deploying commit: ${{ gitea.sha }}" + echo "Branch: ${{ gitea.ref }}" + echo "Timestamp: $(date -u +%Y-%m-%dT%H:%M:%SZ)" + # TODO: bash scripts/deploy.sh --version + # 等姜维确认 act-runner 环境后再补 + + - name: Deploy + run: | + echo "=== Deploy step (placeholder) ===" + echo "Source: ${{ gitea.workspace }}" + # TODO: 实际部署脚本 + # bash scripts/deploy.sh --source="$GITHUB_WORKSPACE" --target="$HOME/.sanguo_projects/sanguo_moziplus_v2" --health-check + echo "Deploy placeholder completed." + + - name: Health check + run: | + echo "=== Health check ===" + # TODO: 等服务启动后做健康检查 + # curl -sf http://localhost:8083/api/health || exit 1 + echo "Health check placeholder passed." + + # ── 失败时回滚 ──────────────────────────────── + # v1.23 不支持 if: failure() + # 回滚逻辑改由 notify-on-failure job 检测 commit status 后通知人工介入 + # 后续可升级到 v1.24+ 后改用 failure() 表达式 + + # ── Job 3: 部署失败通知 ────────────────────────────── + notify-deploy-failure: + runs-on: ubuntu-latest + needs: [ci, deploy] + if: always() + steps: + - name: Check deploy result and notify + env: + CI_TOKEN: ${{ secrets.CI_TOKEN }} + run: | + STATUS=$(curl -sf \ + -H "Authorization: token $CI_TOKEN" \ + "${{ gitea.api_url }}/repos/${{ gitea.repository }}/commits/${{ gitea.sha }}/status" \ + | python3 -c "import sys,json; print(json.load(sys.stdin).get('state',''))" 2>/dev/null || echo "") + + echo "Deploy status: $STATUS" + + if [ "$STATUS" != "success" ]; then + echo "Deploy failed, creating Issue for manual intervention..." + + # 创建 Issue 通知人工介入 + curl -sf -X POST \ + -H "Authorization: token $CI_TOKEN" \ + -H "Content-Type: application/json" \ + "${{ gitea.api_url }}/repos/${{ gitea.repository }}/issues" \ + -d "{\"title\": \"🔴 部署失败: commit ${{ gitea.sha }}\", \"body\": \"部署失败,需人工介入排查。\\n\\n触发 commit: \`${{ gitea.sha }}\`\\n分支: main\\n\\n请检查 deploy 日志并手动处理。\", \"labels\": [\"bug\", \"priority:high\"]}" \ + || echo "Failed to create issue" + + echo "Issue created for deploy failure." + else + echo "Deploy succeeded." + fi