Files
novalon-website/scripts/setup-cicd/lib/gitea-setup.sh
T
张翔 929f3ed250 chore: 添加设计分析参考和辅助脚本
- IHG设计分析HTML参考页面
- 布局检查、DOM诊断、验收测试等辅助脚本
2026-06-07 16:22:11 +08:00

126 lines
4.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
setup_gitea_webhook() {
show_banner "配置 Gitea Webhook"
local webhook_url="${JENKINS_URL}/generic-webhook-trigger/invoke?token=${WEBHOOK_TOKEN}"
log_info "Webhook URL: ${webhook_url}"
check_existing_webhook
create_webhook "$webhook_url"
test_webhook_delivery
}
check_existing_webhook() {
log_info "检查现有 Webhook..."
local webhooks=$(gitea_api_call "GET" "/repos/${REPO_OWNER}/${REPO_NAME}/hooks")
if [ -n "$webhooks" ] && [ "$webhooks" != "[]" ]; then
local existing_count=$(echo "$webhooks" | jq 'length')
log_warning "发现 ${existing_count} 个现有 Webhook"
echo "$webhooks" | jq -r '.[] | "\(.id) | \(.url)"' | while IFS='|' read -r hook_id hook_url; do
log_info " [ID: $hook_id] $hook_url"
done
read -p "是否删除所有旧 Webhook 并重新创建?(y/N): " delete_old
if [[ "$delete_old" =~ ^[Yy]$ ]]; then
echo "$webhooks" | jq -r '.[].id' | while read hook_id; do
gitea_api_call "DELETE" "/repos/${REPO_OWNER}/${REPO_NAME}/hooks/${hook_id}" > /dev/null
log_info " 已删除 Webhook #${hook_id}"
done
log_success "旧 Webhook 已清理"
fi
else
log_success "没有发现冲突的 Webhook"
fi
}
create_webhook() {
local webhook_url="$1"
log_info "创建新 Webhook..."
local payload="{
\"type\": \"gitea\",
\"config\": {
\"url\": \"${webhook_url}\",
\"content_type\": \"json\",
\"secret\": \"\",
\"http_method\": \"post\",
\"max_builds\": 0,
\"active\": true,
\"events\": [
\"push\"
],
\"branch_filter\": \"main,develop\"
}
}"
local result=$(gitea_api_call "POST" "/repos/${REPO_OWNER}/${REPO_NAME}/hooks" "$payload")
local hook_id=$(echo "$result" | jq -r '.id // empty')
if [ -n "$hook_id" ]; then
log_success "Webhook 创建成功 (ID: ${hook_id})"
log_info "触发事件: Push Events (main, develop 分支)"
log_info "目标 URL: ${webhook_url}"
else
log_error "Webhook 创建失败!"
log_error "响应: $result"
exit 1
fi
}
test_webhook_delivery() {
show_banner "测试 Webhook 连通性"
log_info "发送测试请求到 Gitea..."
local webhooks=$(gitea_api_call "GET" "/repos/${REPO_OWNER}/${REPO_NAME}/hooks")
local latest_hook_id=$(echo "$webhooks" | jq -r '.[-1].id')
if [ -z "$latest_hook_id" ] || [ "$latest_hook_id" = "null" ]; then
log_error "未找到可用的 Webhook ID"
return 1
fi
log_info "测试 Webhook #${latest_hook_id}..."
local test_result=$(gitea_api_CALL "POST" "/repos/${REPO_OWNER}/${REPO_NAME}/hooks/${latest_hook_id}/tests")
sleep 3
log_info "检查 Jenkins 是否收到触发..."
local jenkins_jobs=$(jenkins_api_call "GET" "/job/${JOB_NAME}/api/json?tree=lastBuild[number,url,result]" 2>/dev/null)
if [ -n "$jenkins_jobs" ]; then
local last_build=$(echo "$jenkins_jobs" | jq '.lastBuild.number // 0')
log_success "Jenkins 已收到 Webhook 触发(最新构建: #${last_build}"
else
log_warning "无法确认 Jenkins 是否收到触发(可能需要等待几秒)"
log_info "请手动访问: ${JENKINS_URL}/job/${JOB_NAME}/"
fi
}
show_webhook_status() {
echo ""
log_info "当前 Gitea Webhook 列表:"
echo "─────────────────────────────────────"
local webhooks=$(gitea_api_call "GET" "/repos/${REPO_OWNER}/${REPO_NAME}/hooks")
if [ -n "$webhooks" ] && [ "$webhooks" != "[]" ]; then
echo "$webhooks" | jq -r '.[] | "📌 [#\(.id)] \(.type) → \(.config.url)\n 状态: \(if .active then "✅ 激活" else "❌ 停用" end)\n 事件: \(.config.events | join(", "))\n 分支过滤: \(.config.branch_filter // "无")\n"'
else
log_info "(无 Webhook"
fi
echo ""
}