Files
ljwx-k8s/gitea-workflow-example.yaml
2026-02-20 22:50:45 +08:00

222 lines
8.3 KiB
YAML

# Gitea Actions Workflow 示例
# 用于演示如何将测试报告上传到 MinIO
# 文件路径: .gitea/workflows/test-and-report.yaml
name: Test and Upload Reports to MinIO
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test-and-report:
runs-on: ubuntu-latest
steps:
# 1. 检出代码
- name: Checkout code
uses: actions/checkout@v3
# 2. 设置环境(根据项目类型选择)
# Node.js 示例
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
# Python 示例(注释掉的,根据需要启用)
# - name: Setup Python
# uses: actions/setup-python@v4
# with:
# python-version: '3.11'
# 3. 安装依赖
- name: Install dependencies
run: npm ci
# Python: pip install -r requirements.txt
# 4. 运行测试并生成报告
- name: Run tests with coverage
run: |
# Jest (Node.js)
npm test -- --coverage --coverageReporters=html --coverageReporters=json
# Pytest (Python)
# pytest --html=report.html --self-contained-html --cov=. --cov-report=html
# 5. 安装 MinIO Client
- name: Install MinIO Client
run: |
wget https://dl.min.io/client/mc/release/linux-amd64/mc
chmod +x mc
sudo mv mc /usr/local/bin/
# 6. 上传测试报告到 MinIO
- name: Upload test reports to MinIO
env:
# 在 Gitea 仓库设置中配置这些 Secrets
MINIO_ENDPOINT: http://minio.storage.svc.cluster.local:9000
MINIO_ACCESS_KEY: minioadmin
MINIO_SECRET_KEY: minioadmin123
run: |
# 配置 MinIO 别名
mc alias set myminio $MINIO_ENDPOINT $MINIO_ACCESS_KEY $MINIO_SECRET_KEY
# 创建报告目录结构
REPO_NAME="${{ github.repository }}"
BRANCH_NAME="${{ github.ref_name }}"
COMMIT_SHA="${{ github.sha }}"
SHORT_SHA="${COMMIT_SHA:0:7}"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
# 报告存储路径
REPORT_PATH="test-reports/${REPO_NAME}/${BRANCH_NAME}/${SHORT_SHA}/${TIMESTAMP}"
echo "📊 Uploading reports to: $REPORT_PATH"
# 上传测试报告(根据项目调整路径)
# Node.js (Jest)
if [ -d "coverage" ]; then
mc cp --recursive coverage/ myminio/${REPORT_PATH}/coverage/
echo "✅ Coverage report uploaded"
fi
# Python (Pytest)
# if [ -f "report.html" ]; then
# mc cp report.html myminio/${REPORT_PATH}/
# echo "✅ Test report uploaded"
# fi
# if [ -d "htmlcov" ]; then
# mc cp --recursive htmlcov/ myminio/${REPORT_PATH}/coverage/
# echo "✅ Coverage report uploaded"
# fi
# 生成索引页面
cat > index.html <<EOF
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test Reports - ${REPO_NAME}</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
h1 { color: #333; border-bottom: 2px solid #4CAF50; padding-bottom: 10px; }
.info { background: #e3f2fd; padding: 15px; border-radius: 5px; margin: 20px 0; }
.info p { margin: 5px 0; }
.links { margin: 30px 0; }
.links a {
display: inline-block;
background: #4CAF50;
color: white;
padding: 12px 24px;
text-decoration: none;
border-radius: 5px;
margin: 5px 10px 5px 0;
transition: background 0.3s;
}
.links a:hover { background: #45a049; }
.badge { background: #2196F3; color: white; padding: 3px 8px; border-radius: 3px; font-size: 12px; }
</style>
</head>
<body>
<div class="container">
<h1>📊 Test Reports</h1>
<div class="info">
<p><strong>Repository:</strong> ${REPO_NAME}</p>
<p><strong>Branch:</strong> <span class="badge">${BRANCH_NAME}</span></p>
<p><strong>Commit:</strong> <code>${SHORT_SHA}</code></p>
<p><strong>Build Time:</strong> ${TIMESTAMP}</p>
<p><strong>Workflow Run:</strong> #${{ github.run_number }}</p>
</div>
<div class="links">
<h2>📄 Available Reports</h2>
<a href="./coverage/index.html">📈 Coverage Report</a>
<a href="./report.html">🧪 Test Report</a>
</div>
<div class="info">
<p><strong>View in MinIO Console:</strong></p>
<p><a href="http://localhost:31001" target="_blank">http://localhost:31001</a></p>
<p><small>Navigate to: <code>test-reports/${REPO_NAME}/${BRANCH_NAME}/${SHORT_SHA}/${TIMESTAMP}</code></small></p>
</div>
</div>
</body>
</html>
EOF
# 上传索引页面
mc cp index.html myminio/${REPORT_PATH}/
# 输出访问链接
echo "==========================================="
echo "✅ Reports uploaded successfully!"
echo "==========================================="
echo ""
echo "📊 Report URLs:"
echo " Index: http://localhost:31000/${REPORT_PATH}/index.html"
echo " Coverage: http://localhost:31000/${REPORT_PATH}/coverage/index.html"
echo ""
echo "🔗 MinIO Console:"
echo " http://localhost:31001/browser/test-reports/${REPO_NAME}/${BRANCH_NAME}/${SHORT_SHA}/${TIMESTAMP}"
echo ""
# 保存链接到输出(用于后续步骤)
echo "REPORT_INDEX_URL=http://localhost:31000/${REPORT_PATH}/index.html" >> $GITHUB_ENV
echo "REPORT_COVERAGE_URL=http://localhost:31000/${REPORT_PATH}/coverage/index.html" >> $GITHUB_ENV
# 7. 在 PR 中评论报告链接(仅在 PR 时执行)
- name: Comment PR with report links
if: github.event_name == 'pull_request'
run: |
# 注意:这需要 Gitea 支持 actions/github-script 或类似功能
# 以下是示例输出,实际使用时需要调整
echo "📊 测试报告已生成!"
echo ""
echo "🔗 查看报告:"
echo "- 📈 Coverage: ${{ env.REPORT_COVERAGE_URL }}"
echo "- 📊 Index: ${{ env.REPORT_INDEX_URL }}"
# 8. 上传构建产物(可选)
- name: Upload build artifacts
if: github.ref == 'refs/heads/main'
run: |
# 如果有构建产物(dist/, build/, target/等)
if [ -d "dist" ]; then
ARTIFACT_PATH="artifacts/${REPO_NAME}/${BRANCH_NAME}/${SHORT_SHA}"
mc cp --recursive dist/ myminio/${ARTIFACT_PATH}/
echo "✅ Build artifacts uploaded to: ${ARTIFACT_PATH}"
fi
# 9. 清理旧报告(可选,每周运行一次)
- name: Cleanup old reports
if: github.event_name == 'schedule' || github.ref == 'refs/heads/main'
run: |
# 删除 30 天前的报告
mc rm --recursive --older-than 30d myminio/test-reports/ || true
echo "✅ Old reports cleaned up"
# 可选:添加定时任务清理旧报告
# on:
# schedule:
# - cron: '0 2 * * 0' # 每周日凌晨 2 点运行