Merge PR #33: Add CD pipeline
Deploy / ci (push) Successful in 4s
Deploy / deploy (push) Successful in 2s
Deploy / notify-on-failure (push) Successful in 0s
CI / lint (push) Successful in 7s
CI / test (push) Successful in 4s
CI / test (pull_request) Successful in 3s
CI / lint (pull_request) Successful in 8s
CI / notify-on-failure (push) Successful in 2s
CI / notify-on-failure (pull_request) Successful in 2s

This commit was merged in pull request #33.
This commit is contained in:
2026-06-11 04:01:37 +00:00
6 changed files with 72 additions and 75 deletions
+4 -4
View File
@@ -47,17 +47,17 @@ jobs:
- name: Setup Python - name: Setup Python
run: | run: |
python3 -m venv .venv python3 -m venv /tmp/ci-venv-test
if [ -f pyproject.toml ]; then if [ -f pyproject.toml ]; then
.venv/bin/pip install --quiet -e ".[dev]" /tmp/ci-venv-test/bin/pip install --quiet -e ".[dev]"
else else
echo "No pyproject.toml, skipping dev install" /tmp/ci-venv-test/bin/pip install --quiet pytest
fi fi
- name: Run tests (exclude E2E) - name: Run tests (exclude E2E)
run: | run: |
if [ -d tests ]; then if [ -d tests ]; then
.venv/bin/pytest tests/ -m "not e2e" -x -q /tmp/ci-venv-test/bin/pytest tests/ -m "not e2e" -x -q
else else
echo "No tests/ directory, skipping tests" echo "No tests/ directory, skipping tests"
fi fi
+48 -71
View File
@@ -1,14 +1,4 @@
# 部署管道 — moziplus v2.0 # CD 管道 — moziplus-v2 测试
#
# 触发条件:
# - push 到 main 分支 → 完整 CIlint + test + coverage+ 部署
#
# 注意:非 main 分支的 CI 由 ci.yml 负责(快速门控)
#
# Gitea v1.23.4 限制注意:
# - 不支持 failure() 表达式
# - 不支持 concurrency / permissions
# - 部署脚本占位,等姜维确认 act-runner 环境后再补具体命令
name: Deploy name: Deploy
@@ -17,7 +7,6 @@ on:
branches: [main] branches: [main]
jobs: jobs:
# ── Job 1: CI(main 分支跑完整测试)─────────────────
ci: ci:
runs-on: macos-arm64 runs-on: macos-arm64
steps: steps:
@@ -25,97 +14,85 @@ jobs:
- name: Setup Python - name: Setup Python
run: | run: |
python3 -m venv .venv python3 -m venv /tmp/ci-venv-deploy
.venv/bin/pip install --quiet -e ".[dev]" /tmp/ci-venv-deploy/bin/pip install --quiet flake8 pytest
- name: Lint - name: Lint
run: | 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: | run: |
if [ -d tests ]; then if [ -d tests ]; then
.venv/bin/pytest tests/ -m "not e2e" -x -q /tmp/ci-venv-deploy/bin/pytest tests/ -x -q
else else
echo "No tests/ directory, skipping tests" echo "No tests/"
fi 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: deploy:
runs-on: macos-arm64 runs-on: macos-arm64
needs: ci needs: ci
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Record current version - name: Deploy to test target
run: | run: |
echo "Deploying commit: ${{ gitea.sha }}" TARGET="$HOME/.sanguo_projects/moziplus-v2-cd-test"
echo "Branch: ${{ gitea.ref }}" echo "🚀 Deploying to $TARGET"
echo "Timestamp: $(date -u +%Y-%m-%dT%H:%M:%SZ)" mkdir -p "$TARGET"
# TODO: bash scripts/deploy.sh --version
# 等姜维确认 act-runner 环境后再补
- name: Deploy # Sync code (exclude git/tests/docs)
run: | rsync -a --delete \
echo "=== Deploy step (placeholder) ===" --exclude='.git/' \
echo "Source: ${{ gitea.workspace }}" --exclude='docs/' \
# TODO: 实际部署脚本 --exclude='tests/' \
# bash scripts/deploy.sh --source="$GITHUB_WORKSPACE" --target="$HOME/.sanguo_projects/sanguo_moziplus_v2" --health-check --exclude='__pycache__/' \
echo "Deploy placeholder completed." --exclude='.venv/' \
# placeholder: 后续替换为实际部署脚本 ./ "$TARGET/"
true
echo "✅ Deploy completed"
- name: Health check - name: Health check
run: | run: |
echo "=== Health check (placeholder) ===" # 验证部署文件存在
# TODO: 等服务启动后做健康检查 TARGET="$HOME/.sanguo_projects/moziplus-v2-cd-test"
# curl -sf http://localhost:8083/api/health || exit 1 if [ -f "$TARGET/src/hello.py" ]; then
echo "Health check placeholder passed." echo "Health check passed — hello.py deployed"
# placeholder: 后续替换为实际健康检查 else
true echo "❌ Health check failed — hello.py not found"
exit 1
fi
# ── 失败时回滚 ──────────────────────────────── - name: Record deploy version
# v1.23 不支持 if: failure() run: |
# 回滚逻辑改由 notify-on-failure job 检测 commit status 后通知人工介入 TARGET="$HOME/.sanguo_projects/moziplus-v2-cd-test"
# 后续可升级到 v1.24+ 后改用 failure() 表达式 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-on-failure:
notify-deploy-failure:
runs-on: macos-arm64 runs-on: macos-arm64
needs: [ci, deploy] needs: [ci, deploy]
if: always() if: always()
steps: steps:
- name: Check deploy result and notify - name: Check results and notify
env: env:
CI_TOKEN: ${{ secrets.CI_TOKEN }} GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
CI_RESULT: ${{ needs.ci.result }}
DEPLOY_RESULT: ${{ needs.deploy.result }}
run: | run: |
STATUS=$(curl -sf \ if [ "$CI_RESULT" = "failure" ] || [ "$DEPLOY_RESULT" = "failure" ]; then
-H "Authorization: token $CI_TOKEN" \ echo "Pipeline failed, creating Issue..."
"${{ gitea.api_url }}/repos/${{ gitea.repository }}/commits/${{ gitea.sha }}/status" \ FAILED_JOBS=""
| python3 -c "import sys,json; print(json.load(sys.stdin).get('state',''))" 2>/dev/null || echo "") [ "$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 \ curl -sf -X POST \
-H "Authorization: token $CI_TOKEN" \ -H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
"${{ gitea.api_url }}/repos/${{ gitea.repository }}/issues" \ "${{ 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 "Failed to create issue"
echo "Issue created for deploy failure."
else else
echo "Deploy succeeded." echo "Pipeline succeeded."
fi fi
+1
View File
@@ -0,0 +1 @@
"""moziplus v2 test module"""
+9
View File
@@ -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"}
View File
+10
View File
@@ -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"