fix: Improve production health check to use Docker health status
All checks were successful
Production Deployment (Zero Downtime) / deploy-production (push) Successful in 10m54s

- Use Docker health check status instead of host-based curl
- Test from inside container instead of from host
- Better error messages and debugging
- More reliable health check that doesn't depend on port mapping
This commit is contained in:
2026-01-09 20:05:31 +01:00
parent e5233138ab
commit 37178ce421

View File

@@ -209,17 +209,62 @@ jobs:
- name: Production Health Check - name: Production Health Check
run: | run: |
echo "🔍 Running production health checks..." echo "🔍 Running production health checks..."
for i in {1..20}; do COMPOSE_FILE="docker-compose.production.yml"
if curl -f http://localhost:3000/api/health && curl -f http://localhost:3000/ > /dev/null; then CONTAINER_NAME="portfolio-app"
echo "✅ Production is fully operational!"
exit 0 # Get the production container ID
CONTAINER_ID=$(docker compose -f $COMPOSE_FILE ps -q portfolio 2>/dev/null | head -1)
if [ -z "$CONTAINER_ID" ]; then
CONTAINER_ID=$(docker ps -q -f "name=^/${CONTAINER_NAME}$")
fi fi
echo "⏳ Waiting for production... ($i/20)"
sleep 3 if [ -z "$CONTAINER_ID" ]; then
done echo "❌ Production container not found!"
echo "❌ Production health check failed!" docker ps --filter "name=portfolio" --format "table {{.ID}}\t{{.Names}}\t{{.Status}}"
docker compose -f docker-compose.production.yml logs --tail=50
exit 1 exit 1
fi
echo "📦 Found container: $CONTAINER_ID"
# Wait for container to be healthy (using Docker's health check)
HEALTH_CHECK_PASSED=false
for i in {1..30}; do
HEALTH=$(docker inspect $CONTAINER_ID --format='{{.State.Health.Status}}' 2>/dev/null || echo "starting")
STATUS=$(docker inspect $CONTAINER_ID --format='{{.State.Status}}' 2>/dev/null || echo "unknown")
if [ "$HEALTH" == "healthy" ] && [ "$STATUS" == "running" ]; then
echo "✅ Container is healthy and running!"
# Test from inside the container (most reliable)
if docker exec $CONTAINER_ID curl -f -s --max-time 5 http://localhost:3000/api/health > /dev/null 2>&1; then
echo "✅ Health endpoint responds from inside container!"
HEALTH_CHECK_PASSED=true
break
else
echo "⚠️ Container is healthy but HTTP endpoint test failed. Retrying..."
fi
fi
if [ $((i % 5)) -eq 0 ]; then
echo "📊 Status: $STATUS, Health: $HEALTH (attempt $i/30)"
fi
echo "⏳ Waiting for production... ($i/30)"
sleep 2
done
if [ "$HEALTH_CHECK_PASSED" != "true" ]; then
echo "❌ Production health check failed!"
echo "📋 Container status:"
docker inspect $CONTAINER_ID --format='Name: {{.Name}}, Status: {{.State.Status}}, Health: {{.State.Health.Status}}' 2>/dev/null || echo "Could not inspect container"
echo "📋 Container logs:"
docker compose -f $COMPOSE_FILE logs --tail=50 portfolio 2>/dev/null || docker logs $CONTAINER_ID --tail=50 2>/dev/null || echo "Could not get logs"
echo "📋 Testing from inside container:"
docker exec $CONTAINER_ID curl -v http://localhost:3000/api/health 2>&1 || echo "Container HTTP test failed"
exit 1
fi
echo "✅ Production is fully operational!"
- name: Cleanup - name: Cleanup
run: | run: |