chore: Improve port management in Gitea deployment workflow
All checks were successful
Dev Deployment (Zero Downtime) / deploy-dev (push) Successful in 14m32s
All checks were successful
Dev Deployment (Zero Downtime) / deploy-dev (push) Successful in 14m32s
- Enhanced the deployment script to better handle port conflicts by checking for both Docker containers and non-Docker processes using the specified health port. - Added logic to wait for the port to be released and attempt to use an alternative port if the original is still in use after a timeout. - Improved feedback messages for better clarity during the deployment process.
This commit is contained in:
@@ -77,17 +77,87 @@ jobs:
|
|||||||
docker stop $EXISTING_CONTAINER 2>/dev/null || true
|
docker stop $EXISTING_CONTAINER 2>/dev/null || true
|
||||||
docker rm $EXISTING_CONTAINER 2>/dev/null || true
|
docker rm $EXISTING_CONTAINER 2>/dev/null || true
|
||||||
echo "✅ Old container removed"
|
echo "✅ Old container removed"
|
||||||
|
# Wait for Docker to release the port
|
||||||
|
echo "⏳ Waiting for Docker to release port ${HEALTH_PORT}..."
|
||||||
|
sleep 3
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Also check if port is in use by another process and free it
|
# Check if port is still in use by Docker containers (check all containers, not just running)
|
||||||
PORT_IN_USE=$(lsof -ti:${HEALTH_PORT} 2>/dev/null || echo "")
|
PORT_CONTAINER=$(docker ps -a --format "{{.ID}}\t{{.Names}}\t{{.Ports}}" | grep -E "(:${HEALTH_PORT}->|:${HEALTH_PORT}/)" | awk '{print $1}' | head -1 || echo "")
|
||||||
if [ ! -z "$PORT_IN_USE" ]; then
|
if [ ! -z "$PORT_CONTAINER" ]; then
|
||||||
echo "⚠️ Port ${HEALTH_PORT} is in use by process $PORT_IN_USE"
|
echo "⚠️ Port ${HEALTH_PORT} is still in use by container $PORT_CONTAINER"
|
||||||
echo "Attempting to free the port..."
|
echo "🛑 Stopping and removing container using port..."
|
||||||
kill -9 $PORT_IN_USE 2>/dev/null || true
|
docker stop $PORT_CONTAINER 2>/dev/null || true
|
||||||
|
docker rm $PORT_CONTAINER 2>/dev/null || true
|
||||||
|
sleep 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Also check for any containers with the same name that might be using the port
|
||||||
|
SAME_NAME_CONTAINER=$(docker ps -a -q -f name=$CONTAINER_NAME | head -1 || echo "")
|
||||||
|
if [ ! -z "$SAME_NAME_CONTAINER" ] && [ "$SAME_NAME_CONTAINER" != "$EXISTING_CONTAINER" ]; then
|
||||||
|
echo "⚠️ Found another container with same name: $SAME_NAME_CONTAINER"
|
||||||
|
docker stop $SAME_NAME_CONTAINER 2>/dev/null || true
|
||||||
|
docker rm $SAME_NAME_CONTAINER 2>/dev/null || true
|
||||||
sleep 2
|
sleep 2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Also check if port is in use by another process (non-Docker)
|
||||||
|
PORT_IN_USE=$(lsof -ti:${HEALTH_PORT} 2>/dev/null || ss -tlnp | grep ":${HEALTH_PORT} " | head -1 || echo "")
|
||||||
|
if [ ! -z "$PORT_IN_USE" ] && [ -z "$PORT_CONTAINER" ]; then
|
||||||
|
echo "⚠️ Port ${HEALTH_PORT} is in use by process"
|
||||||
|
echo "Attempting to free the port..."
|
||||||
|
# Try to find and kill the process
|
||||||
|
if command -v lsof >/dev/null 2>&1; then
|
||||||
|
PID=$(lsof -ti:${HEALTH_PORT} 2>/dev/null || echo "")
|
||||||
|
if [ ! -z "$PID" ]; then
|
||||||
|
kill -9 $PID 2>/dev/null || true
|
||||||
|
sleep 2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Final check: verify port is free and wait if needed
|
||||||
|
echo "🔍 Verifying port ${HEALTH_PORT} is free..."
|
||||||
|
MAX_WAIT=10
|
||||||
|
WAIT_COUNT=0
|
||||||
|
while [ $WAIT_COUNT -lt $MAX_WAIT ]; do
|
||||||
|
PORT_CHECK=$(docker ps --format "{{.Ports}}" | grep -E "(:${HEALTH_PORT}->|:${HEALTH_PORT}/)" || echo "")
|
||||||
|
if [ -z "$PORT_CHECK" ]; then
|
||||||
|
# Also check with lsof/ss if available
|
||||||
|
if command -v lsof >/dev/null 2>&1; then
|
||||||
|
PORT_CHECK=$(lsof -ti:${HEALTH_PORT} 2>/dev/null || echo "")
|
||||||
|
elif command -v ss >/dev/null 2>&1; then
|
||||||
|
PORT_CHECK=$(ss -tlnp | grep ":${HEALTH_PORT} " || echo "")
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
if [ -z "$PORT_CHECK" ]; then
|
||||||
|
echo "✅ Port ${HEALTH_PORT} is free!"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
WAIT_COUNT=$((WAIT_COUNT + 1))
|
||||||
|
echo "⏳ Port still in use, waiting... ($WAIT_COUNT/$MAX_WAIT)"
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
||||||
|
# If port is still in use, try alternative port
|
||||||
|
if [ $WAIT_COUNT -ge $MAX_WAIT ]; then
|
||||||
|
echo "⚠️ Port ${HEALTH_PORT} is still in use after waiting. Trying alternative port..."
|
||||||
|
HEALTH_PORT="3002"
|
||||||
|
echo "🔄 Using alternative port: ${HEALTH_PORT}"
|
||||||
|
# Quick check if alternative port is also in use
|
||||||
|
ALT_PORT_CHECK=$(docker ps --format "{{.Ports}}" | grep -E "(:${HEALTH_PORT}->|:${HEALTH_PORT}/)" || echo "")
|
||||||
|
if [ ! -z "$ALT_PORT_CHECK" ]; then
|
||||||
|
echo "❌ Alternative port ${HEALTH_PORT} is also in use!"
|
||||||
|
echo "Attempting to free alternative port..."
|
||||||
|
ALT_CONTAINER=$(docker ps -a --format "{{.ID}}\t{{.Names}}\t{{.Ports}}" | grep -E "(:${HEALTH_PORT}->|:${HEALTH_PORT}/)" | awk '{print $1}' | head -1 || echo "")
|
||||||
|
if [ ! -z "$ALT_CONTAINER" ]; then
|
||||||
|
docker stop $ALT_CONTAINER 2>/dev/null || true
|
||||||
|
docker rm $ALT_CONTAINER 2>/dev/null || true
|
||||||
|
sleep 2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
# Start new container with updated image
|
# Start new container with updated image
|
||||||
echo "🆕 Starting new dev container..."
|
echo "🆕 Starting new dev container..."
|
||||||
docker run -d \
|
docker run -d \
|
||||||
|
|||||||
Reference in New Issue
Block a user