#!/bin/bash run_all_verifications() { log_info "运行完整验证流程..." verify_jenkins_accessibility verify_gitea_connectivity verify_jenkins_tools verify_ssh_connection verify_pipeline_job verify_webhook_configuration verify_jenkinsfile_exists echo "" show_verification_summary } verify_jenkins_accessibility() { echo -n " Jenkins 可访问性: " if curl -sf --connect-timeout 5 "${JENKINS_URL}/login" > /dev/null 2>&1; then echo -e "${GREEN}✅ 通过${NC}" return 0 else echo -e "${RED}❌ 失败${NC}" return 1 fi } verify_gitea_connectivity() { echo -n " Gitea 连通性: " if curl -sf --connect-timeout 5 "${GITEA_URL}/api/v1/version" > /dev/null 2>&1; then local version=$(curl -sf "${GITEA_URL}/api/v1/version") echo -e "${GREEN}✅ 通过${NC} (版本: $version)" return 0 else echo -e "${RED}❌ 失败${NC}" return 1 fi } verify_jenkins_tools() { echo -n " Jenkins 工具集: " local missing=() for tool in rsync curl ssh jq; do if ! docker exec ${JENKINS_CONTAINER_NAME} which "$tool" > /dev/null 2>&1; then missing+=("$tool") fi done if [ ${#missing[@]} -eq 0 ]; then echo -e "${GREEN}✅ 通过${NC}" return 0 else echo -e "${RED}❌ 缺少: ${missing[*]}${NC}" return 1 fi } verify_ssh_connection() { echo -n " SSH 免密登录: " local result=$(docker exec ${JENKINS_CONTAINER_NAME} ssh \ -o StrictHostKeyChecking=no \ -o ConnectTimeout=5 \ -o BatchMode=yes \ ${SERVER_USER}@${SERVER_IP} \ "echo 'OK'" 2>&1) if [[ "$result" == *"OK"* ]]; then echo -e "${GREEN}✅ 通过${NC}" return 0 else echo -e "${RED}❌ 失败${NC}" return 1 fi } verify_pipeline_job() { echo -n " Pipeline 任务: " local result=$(jenkins_api_call "GET" "/job/${JOB_NAME}/api/json" 2>/dev/null) if echo "$result" | jq -e '.name' > /dev/null 2>&1; then echo -e "${GREEN}✅ 通过${NC} (${JOB_NAME})" return 0 else echo -e "${RED}❌ 未找到${NC}" return 1 fi } verify_webhook_configuration() { echo -n " Gitea Webhook: " local webhooks=$(gitea_api_call "GET" "/repos/${REPO_OWNER}/${REPO_NAME}/hooks" 2>/dev/null) if [ -n "$webhooks" ] && [ "$webhooks" != "[]" ] && [ "$webhooks" != "null" ]; then local count=$(echo "$webhooks" | jq 'length') echo -e "${GREEN}✅ 通过${NC} (${count} 个 Webhook)" return 0 else echo -e "${RED}❌ 未配置${NC}" return 1 fi } verify_jenkinsfile_exists() { echo -n " Jenkinsfile 文件: " if [ -f "${PROJECT_ROOT}/Jenkinsfile" ]; then echo -e "${GREEN}✅ 通过${NC}" return 0 else echo -e "${RED}❌ 未找到${NC}" return 1 fi } show_verification_summary() { show_banner "验证结果汇总" local total=7 local passed=0 if verify_jenkins_accessibility >/dev/null 2>&1; then ((passed++)); fi if verify_gitea_connectivity >/dev/null 2>&1; then ((passed++)); fi if verify_jenkins_tools >/dev/null 2>&1; then ((passed++)); fi if verify_ssh_connection >/dev/null 2>&1; then ((passed++)); fi if verify_pipeline_job >/dev/null 2>&1; then ((passed++)); fi if verify_webhook_configuration >/dev/null 2>&1; then ((passed++)); fi if verify_jenkinsfile_exists >/dev/null 2>&1; then ((passed++)); fi local failed=$((total - passed)) echo "" echo " 总计: ${total} 项检查" echo -e " ${GREEN}通过: ${passed}${NC} 项" if [ $failed -gt 0 ]; then echo -e " ${RED}失败: ${failed}${NC} 项" fi echo "" if [ $failed -eq 0 ]; then echo -e " 🎉 ${GREEN}所有配置已就绪!可以开始使用 CI/CD 流水线了。${NC}" echo "" echo " 下一步操作:" echo " 1. 访问 ${JENKINS_URL}/job/${JOB_NAME}/" echo " 2. 点击 'Build Now' 进行首次构建" echo " 3. 推送代码到 main 分支测试自动触发" echo " 4. 勾选 DEPLOY_TO_PRODUCTION 参数测试部署" else echo -e " ⚠️ ${YELLOW}部分配置未通过,请查看上方详情并修复。${NC}" echo "" echo " 常见问题:" echo " - SSH 连接失败 → 检查密钥挂载和 authorized_keys" echo " - 工具缺失 → 运行: ./setup-cicd.sh --jenkins" echo " - Webhook 失败 → 检查 Gitea 和 Jenkins 网络连通性" fi }