diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 61b1cdf..5715686 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -1,14 +1,4 @@ -# 部署管道 — moziplus v2.0 -# -# 触发条件: -# - push 到 main 分支 → 完整 CI(lint + test + coverage)+ 部署 -# -# 注意:非 main 分支的 CI 由 ci.yml 负责(快速门控) -# -# Gitea v1.23.4 限制注意: -# - 不支持 failure() 表达式 -# - 不支持 concurrency / permissions -# - 部署脚本占位,等姜维确认 act-runner 环境后再补具体命令 +# CD 管道 — moziplus-v2 测试 name: Deploy @@ -17,7 +7,6 @@ on: branches: [main] jobs: - # ── Job 1: CI(main 分支跑完整测试)───────────────── ci: runs-on: macos-arm64 steps: @@ -25,97 +14,85 @@ jobs: - name: Setup Python run: | - python3 -m venv .venv - .venv/bin/pip install --quiet -e ".[dev]" + python3 -m venv /tmp/ci-venv-deploy + /tmp/ci-venv-deploy/bin/pip install --quiet flake8 pytest - name: Lint run: | - test -d src && .venv/bin/ruff check src/ || echo "No src/ directory, skipping lint" + /tmp/ci-venv-deploy/bin/flake8 src/ --max-line-length=120 --extend-ignore=E501 || echo "No lint issues or src/ missing" - - name: Unit & Integration Tests + - name: Tests run: | if [ -d tests ]; then - .venv/bin/pytest tests/ -m "not e2e" -x -q + /tmp/ci-venv-deploy/bin/pytest tests/ -x -q else - echo "No tests/ directory, skipping tests" + echo "No tests/" fi - - name: Coverage Report - run: | - if [ -d tests ] && [ -d src ]; then - .venv/bin/pytest tests/ -m "not e2e" --cov=src --cov-report=term-missing -q - else - echo "No tests/ or src/ directory, skipping coverage" - fi - - # ── Job 2: 部署 ───────────────────────────────────── deploy: runs-on: macos-arm64 needs: ci steps: - uses: actions/checkout@v4 - - name: Record current version + - name: Deploy to test target 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 环境后再补 + TARGET="$HOME/.sanguo_projects/moziplus-v2-cd-test" + echo "🚀 Deploying to $TARGET" + mkdir -p "$TARGET" - - 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." - # placeholder: 后续替换为实际部署脚本 - true + # Sync code (exclude git/tests/docs) + rsync -a --delete \ + --exclude='.git/' \ + --exclude='docs/' \ + --exclude='tests/' \ + --exclude='__pycache__/' \ + --exclude='.venv/' \ + ./ "$TARGET/" + + echo "✅ Deploy completed" - name: Health check run: | - echo "=== Health check (placeholder) ===" - # TODO: 等服务启动后做健康检查 - # curl -sf http://localhost:8083/api/health || exit 1 - echo "Health check placeholder passed." - # placeholder: 后续替换为实际健康检查 - true + # 验证部署文件存在 + TARGET="$HOME/.sanguo_projects/moziplus-v2-cd-test" + if [ -f "$TARGET/src/hello.py" ]; then + echo "✅ Health check passed — hello.py deployed" + else + echo "❌ Health check failed — hello.py not found" + exit 1 + fi - # ── 失败时回滚 ──────────────────────────────── - # v1.23 不支持 if: failure() - # 回滚逻辑改由 notify-on-failure job 检测 commit status 后通知人工介入 - # 后续可升级到 v1.24+ 后改用 failure() 表达式 + - name: Record deploy version + run: | + TARGET="$HOME/.sanguo_projects/moziplus-v2-cd-test" + mkdir -p "$TARGET/data" + echo "{\"timestamp\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"commit\":\"${{ gitea.sha }}\"}" >> "$TARGET/data/deploy-history.jsonl" + echo "📝 Deploy history recorded" - # ── Job 3: 部署失败通知 ────────────────────────────── - notify-deploy-failure: + notify-on-failure: runs-on: macos-arm64 needs: [ci, deploy] if: always() steps: - - name: Check deploy result and notify + - name: Check results and notify env: - CI_TOKEN: ${{ secrets.CI_TOKEN }} + GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} + CI_RESULT: ${{ needs.ci.result }} + DEPLOY_RESULT: ${{ needs.deploy.result }} 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 "") + if [ "$CI_RESULT" = "failure" ] || [ "$DEPLOY_RESULT" = "failure" ]; then + echo "Pipeline failed, creating Issue..." + FAILED_JOBS="" + [ "$CI_RESULT" = "failure" ] && FAILED_JOBS="${FAILED_JOBS}ci " + [ "$DEPLOY_RESULT" = "failure" ] && FAILED_JOBS="${FAILED_JOBS}deploy " - 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 "Authorization: token $GITEA_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\"]}" \ + -d "{\"title\": \"[CD] 部署失败: ${{ gitea.sha }}\", \"body\": \"CI/CD pipeline 失败\\n\\nCommit: \`${{ gitea.sha }}\`\\nFailed: ${FAILED_JOBS}\"}" \ || echo "Failed to create issue" - - echo "Issue created for deploy failure." else - echo "Deploy succeeded." + echo "Pipeline succeeded." fi diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..c2c99e8 --- /dev/null +++ b/src/__init__.py @@ -0,0 +1 @@ +"""moziplus v2 test module""" diff --git a/src/hello.py b/src/hello.py new file mode 100644 index 0000000..25636b4 --- /dev/null +++ b/src/hello.py @@ -0,0 +1,9 @@ +"""Hello module for CD testing.""" + +def greet(name: str) -> str: + """Return greeting message.""" + return f"Hello, {name}!" + +def health() -> dict: + """Return health status.""" + return {"status": "ok", "version": "1.0.0"} diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_hello.py b/tests/test_hello.py new file mode 100644 index 0000000..0840077 --- /dev/null +++ b/tests/test_hello.py @@ -0,0 +1,10 @@ +"""Tests for hello module.""" +from src.hello import greet, health + + +def test_greet(): + assert greet("World") == "Hello, World!" + +def test_health(): + result = health() + assert result["status"] == "ok"