# CD 管道 — moziplus-v2 测试 name: Deploy on: push: branches: [main] jobs: ci: runs-on: macos-arm64 steps: - uses: actions/checkout@v4 - name: Setup Python run: | python3 -m venv /tmp/ci-venv-deploy /tmp/ci-venv-deploy/bin/pip install --quiet flake8 pytest - name: Lint run: | /tmp/ci-venv-deploy/bin/flake8 src/ --max-line-length=120 --extend-ignore=E501 || echo "No lint issues or src/ missing" - name: Tests run: | if [ -d tests ]; then /tmp/ci-venv-deploy/bin/pytest tests/ -x -q else echo "No tests/" fi deploy: runs-on: macos-arm64 needs: ci steps: - uses: actions/checkout@v4 - name: Deploy to test target run: | TARGET="$HOME/.sanguo_projects/moziplus-v2-cd-test" echo "🚀 Deploying to $TARGET" mkdir -p "$TARGET" # 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: | # 验证部署文件存在 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 - 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" notify-on-failure: runs-on: macos-arm64 needs: [ci, deploy] if: always() steps: - name: Check results and notify env: GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }} CI_RESULT: ${{ needs.ci.result }} DEPLOY_RESULT: ${{ needs.deploy.result }} run: | 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 " curl -sf -X POST \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ "${{ gitea.api_url }}/repos/${{ gitea.repository }}/issues" \ -d "{\"title\": \"[CD] 部署失败: ${{ gitea.sha }}\", \"body\": \"CI/CD pipeline 失败\\n\\nCommit: \`${{ gitea.sha }}\`\\nFailed: ${FAILED_JOBS}\"}" \ || echo "Failed to create issue" else echo "Pipeline succeeded." fi