#!/bin/bash # ============================================================================ # Gitea Health Check Script # ============================================================================ # Validates that all critical services are running and responsive # # Usage: ./health-check.sh # Exit codes: 0 = healthy, 1 = unhealthy # ============================================================================ set -e # ============================================================================ # Configuration # ============================================================================ readonly POSTGRES_CONTAINER="gitea-postgres" readonly GITEA_CONTAINER="gitea" readonly NGINX_CONTAINER="gitea-nginx" readonly GITEA_URL="http://localhost:3000" readonly TIMEOUT=10 # Output colors readonly GREEN='\033[0;32m' readonly YELLOW='\033[1;33m' readonly RED='\033[0;31m' readonly NC='\033[0m' # ============================================================================ # Logging Functions # ============================================================================ log_info() { echo -e "${YELLOW}[CHECK]${NC} $1" } log_success() { echo -e "${GREEN}[OK]${NC} $1" } log_error() { echo -e "${RED}[FAIL]${NC} $1" >&2 } # ============================================================================ # Health Check Functions # ============================================================================ check_container_running() { local container="$1" if docker ps --format '{{.Names}}' | grep -q "^${container}$"; then log_success "Container ${container} is running" return 0 else log_error "Container ${container} is not running" return 1 fi } check_postgres_healthy() { log_info "Checking PostgreSQL health..." if docker exec "${POSTGRES_CONTAINER}" pg_isready -U gitea -q; then log_success "PostgreSQL is healthy" return 0 else log_error "PostgreSQL is not responding" return 1 fi } check_gitea_responsive() { log_info "Checking Gitea web interface..." if curl -sf -m "${TIMEOUT}" "${GITEA_URL}" > /dev/null; then log_success "Gitea is responding" return 0 else log_error "Gitea is not responding at ${GITEA_URL}" return 1 fi } check_nginx_responding() { log_info "Checking Nginx..." if docker exec "${NGINX_CONTAINER}" nginx -t 2>&1 | grep -q "successful"; then log_success "Nginx configuration is valid" return 0 else log_error "Nginx configuration test failed" return 1 fi } # ============================================================================ # Main Execution # ============================================================================ main() { local exit_code=0 echo "==========================================" echo "Gitea Deployment Health Check" echo "==========================================" echo "" # Check all containers are running check_container_running "${POSTGRES_CONTAINER}" || exit_code=1 check_container_running "${GITEA_CONTAINER}" || exit_code=1 check_container_running "${NGINX_CONTAINER}" || exit_code=1 echo "" # Check service health check_postgres_healthy || exit_code=1 check_gitea_responsive || exit_code=1 check_nginx_responding || exit_code=1 echo "" echo "==========================================" if [ $exit_code -eq 0 ]; then log_success "All health checks passed" else log_error "Some health checks failed" fi return $exit_code } main "$@"