Skip to content

feat: Redis ve MongoDB sağlık kontrolü eklendi; Redis yapılandırması … #65

feat: Redis ve MongoDB sağlık kontrolü eklendi; Redis yapılandırması …

feat: Redis ve MongoDB sağlık kontrolü eklendi; Redis yapılandırması … #65

name: Activity Log Service CI/CD
on:
push:
branches: [master]
paths:
- "activity-log-service/**"
- "craft-pilot-commons/**"
- ".github/workflows/activity-log-service-ci-cd.yml"
workflow_dispatch:
inputs:
environment:
description: "Environment to deploy to"
required: true
default: "prod"
type: choice
options:
- prod
- debug
jobs:
build-and-deploy:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- name: Set up JDK
uses: actions/setup-java@v3
with:
java-version: "17"
distribution: "temurin"
- name: Cache Maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
- name: Build commons library (if needed)
run: |
if [ -d "craft-pilot-commons" ]; then
cd craft-pilot-commons
mvn clean install -DskipTests
fi
- name: Build with Maven
run: |
cd activity-log-service
mvn clean package -DskipTests
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: arm64
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Prepare build context
run: |
# Create a temporary directory for build context
mkdir -p /tmp/docker-build-context
# Copy directories to build context
cp -r activity-log-service /tmp/docker-build-context/
# Copy craft-pilot-commons if it exists
if [ -d "craft-pilot-commons" ]; then
cp -r craft-pilot-commons /tmp/docker-build-context/
fi
- name: Build and Push Docker image
id: docker_build
uses: docker/build-push-action@v4
with:
context: /tmp/docker-build-context
file: /tmp/docker-build-context/activity-log-service/Dockerfile
platforms: linux/arm64
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/activity-log-service:latest-arm64
cache-from: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/activity-log-service:buildcache
cache-to: type=registry,ref=${{ secrets.DOCKERHUB_USERNAME }}/activity-log-service:buildcache,mode=max
- name: Deploy to VPS
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
# Set error handling
set -e
echo "Starting deployment of Activity Log Service..."
# Check if network exists, create if needed
if ! docker network inspect craftpilot-network >/dev/null 2>&1; then
echo "Creating craftpilot network..."
docker network create craftpilot-network
fi
# Check if required services are running
echo "Checking required services..."
for svc in redis kafka eureka-server; do
if ! docker ps | grep -q $svc; then
echo "WARNING: $svc service not running. This may cause issues."
else
echo "$svc is running."
fi
done
# MongoDB container kontrolü - sabit container ismi ile güvenilir sistem
MONGODB_CONTAINER="craftpilot-mongodb"
if ! docker ps | grep -q "$MONGODB_CONTAINER"; then
echo "WARNING: MongoDB container '$MONGODB_CONTAINER' not running or missing!"
echo "Current MongoDB containers (if any):"
docker ps | grep -i mongo || echo "No MongoDB containers found!"
echo "Using the default container name anyway: $MONGODB_CONTAINER"
else
echo "✅ MongoDB container '$MONGODB_CONTAINER' is running: $(docker inspect --format='{{.State.Health.Status}}' $MONGODB_CONTAINER 2>/dev/null || echo 'health status unavailable')"
fi
# MongoDB URI yapılandırması - kredensiyalleri güvenli şekilde kullan
if [ -n "${{ secrets.MONGODB_URI }}" ]; then
# Secrets'tan gelen URI'da container ismi güncellemesi
MONGODB_URI=$(echo "${{ secrets.MONGODB_URI }}" | sed "s/mongodb:27017/$MONGODB_CONTAINER:27017/g" | sed "s/@mongodb:/@$MONGODB_CONTAINER:/g")
echo "Using MongoDB URI from secrets (adjusted hostname): $(echo $MONGODB_URI | sed 's/:[^:]*@/:\*\*\*@/g')"
else
# Komponentlerden URI oluşturma
MONGODB_URI="mongodb://${{ secrets.MONGO_ROOT_USERNAME }}:${{ secrets.MONGO_ROOT_PASSWORD }}@${MONGODB_CONTAINER}:27017/${{ secrets.MONGO_INITDB_DATABASE }}?authSource=admin&retryWrites=true&w=majority&serverSelectionTimeoutMS=30000&connectTimeoutMS=20000&socketTimeoutMS=60000"
echo "Built MongoDB URI from components: $(echo $MONGODB_URI | sed 's/:[^:]*@/:\*\*\*@/g')"
fi
# MongoDB bağlantısını deployment öncesi test et
echo "Testing MongoDB connection before deployment..."
if docker run --rm --network craftpilot-network mongo:6.0 mongosh --quiet --eval "db.runCommand({ping:1}).ok" admin --host $MONGODB_CONTAINER --port 27017 -u ${{ secrets.MONGO_ROOT_USERNAME }} -p ${{ secrets.MONGO_ROOT_PASSWORD }} --authenticationDatabase admin | grep -q "1"; then
echo "✅ MongoDB connection test successful!"
else
echo "⚠️ MongoDB connection test failed! This may cause issues with the service."
echo "Checking MongoDB container status:"
docker ps | grep -i $MONGODB_CONTAINER || echo "Container not found!"
# Devam etmeyi seçiyoruz, bazı durumlarda geçici ağ sorunları olabilir
fi
# Pull the latest Docker image
docker pull ${{ secrets.DOCKERHUB_USERNAME }}/activity-log-service:latest-arm64
# Stop and remove existing container if it exists
echo "Stopping previous container if exists..."
docker stop activity-log-service 2>/dev/null || echo "No running container found"
docker rm activity-log-service 2>/dev/null || echo "No container to remove"
# Setup environment variables
ENVIRONMENT="${{ github.event.inputs.environment }}"
DOCKER_USERNAME="${{ secrets.DOCKERHUB_USERNAME }}"
# Deployment with proper conditional statement
echo "Deploying Activity Log Service in ${ENVIRONMENT:-prod} mode..."
if [ "${ENVIRONMENT}" = "debug" ]; then
echo "Starting in DEBUG mode with remote debugging enabled"
docker run -d \
--name activity-log-service \
--network craftpilot-network \
--restart unless-stopped \
-p 8095:8095 \
-p 5010:5010 \
-e SPRING_PROFILES_ACTIVE=prod \
-e SPRING_APPLICATION_NAME=activity-log-service \
-e KAFKA_BOOTSTRAP_SERVERS=kafka:9092 \
-e ACTIVITY_KAFKA_CONSUMER_TOPIC=activity-events \
-e EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://craftpilot:13579ada@eureka-server:8761/eureka/ \
-e HOSTNAME=activity-log-service \
-e SPRING_SECURITY_USER_NAME=craftpilot \
-e SPRING_SECURITY_USER_PASSWORD=13579ada \
-e MONGODB_URI="${MONGODB_URI}" \
-e MONGODB_DATABASE="${{ secrets.MONGO_INITDB_DATABASE }}" \
-e MONGO_INITDB_DATABASE="${{ secrets.MONGO_INITDB_DATABASE }}" \
-e MONGO_ROOT_USERNAME="${{ secrets.MONGO_ROOT_USERNAME }}" \
-e MONGO_ROOT_PASSWORD="${{ secrets.MONGO_ROOT_PASSWORD }}" \
-e MONGODB_HOST="${MONGODB_CONTAINER}" \
-e REDIS_HOST=redis \
-e REDIS_PORT=6379 \
-e REDIS_PASSWORD=13579ada \
-e MANAGEMENT_ENDPOINTS_WEB_BASE_PATH=/actuator \
-e MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=health,info,metrics,prometheus,loggers,env \
-e MANAGEMENT_ENDPOINT_HEALTH_SHOW_DETAILS=always \
-e MANAGEMENT_HEALTH_MONGODB_ENABLED=true \
-e MANAGEMENT_ENDPOINT_HEALTH_PROBES_ENABLED=true \
-e MANAGEMENT_HEALTH_LIVENESSSTATE_ENABLED=true \
-e MANAGEMENT_HEALTH_READINESSSTATE_ENABLED=true \
-e SERVER_PORT=8095 \
-e LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_DATA_MONGODB=DEBUG \
-e LOGGING_LEVEL_COM_CRAFTPILOT=DEBUG \
-e JAVA_OPTS="-XX:+UseContainerSupport -XX:MaxRAMPercentage=70.0 -XX:+UseG1GC -Djava.security.egd=file:/dev/./urandom -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5010" \
"${DOCKER_USERNAME}/activity-log-service:latest-arm64"
else
echo "Starting in PRODUCTION mode"
docker run -d \
--name activity-log-service \
--network craftpilot-network \
--restart unless-stopped \
-p 8095:8095 \
-e SPRING_PROFILES_ACTIVE=prod \
-e SPRING_APPLICATION_NAME=activity-log-service \
-e KAFKA_BOOTSTRAP_SERVERS=kafka:9092 \
-e ACTIVITY_KAFKA_CONSUMER_TOPIC=activity-events \
-e EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://craftpilot:13579ada@eureka-server:8761/eureka/ \
-e HOSTNAME=activity-log-service \
-e SPRING_SECURITY_USER_NAME=craftpilot \
-e SPRING_SECURITY_USER_PASSWORD=13579ada \
-e MONGODB_URI="${MONGODB_URI}" \
-e MONGODB_DATABASE="${{ secrets.MONGO_INITDB_DATABASE }}" \
-e MONGO_INITDB_DATABASE="${{ secrets.MONGO_INITDB_DATABASE }}" \
-e MONGO_ROOT_USERNAME="${{ secrets.MONGO_ROOT_USERNAME }}" \
-e MONGO_ROOT_PASSWORD="${{ secrets.MONGO_ROOT_PASSWORD }}" \
-e MONGODB_HOST="${MONGODB_CONTAINER}" \
-e REDIS_HOST=redis \
-e REDIS_PORT=6379 \
-e REDIS_PASSWORD=13579ada \
-e MANAGEMENT_ENDPOINTS_WEB_BASE_PATH=/actuator \
-e MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE=health,info,metrics,prometheus \
-e MANAGEMENT_ENDPOINT_HEALTH_SHOW_DETAILS=always \
-e MANAGEMENT_HEALTH_MONGODB_ENABLED=true \
-e MANAGEMENT_ENDPOINT_HEALTH_PROBES_ENABLED=true \
-e MANAGEMENT_HEALTH_LIVENESSSTATE_ENABLED=true \
-e MANAGEMENT_HEALTH_READINESSSTATE_ENABLED=true \
-e SERVER_PORT=8095 \
-e JAVA_OPTS="-XX:+UseContainerSupport -XX:MaxRAMPercentage=70.0 -XX:+UseG1GC -Djava.security.egd=file:/dev/./urandom" \
"${DOCKER_USERNAME}/activity-log-service:latest-arm64"
fi
# Geliştirilmiş sağlık kontrolü
echo "Waiting for service to start..."
max_attempts=20
attempt=1
while [ $attempt -le $max_attempts ]; do
echo "Health check attempt $attempt/$max_attempts..."
# Container çalışıyor mu kontrolü
if ! docker ps | grep -q activity-log-service; then
echo "Container stopped unexpectedly! Checking logs..."
docker logs activity-log-service
exit 1
fi
# MongoDB bağlantı durumunu kontrol et
if [ $attempt -eq 3 ] || [ $attempt -eq 10 ]; then
echo "Checking MongoDB connection from inside container..."
docker exec activity-log-service nc -zv $MONGODB_CONTAINER 27017 || echo "MongoDB connection failed from container"
echo "Database environment variables:"
docker exec activity-log-service env | grep -E 'MONGO|mongodb' || echo "No MongoDB env vars found"
# Özel sağlık kontrolü
echo "MongoDB component health check:"
docker exec activity-log-service curl -s http://localhost:8095/actuator/health/mongo || echo "MongoDB health component unavailable"
fi
# Health endpoint kontrolü
HEALTH_CHECK=$(curl -s -m 5 http://localhost:8095/actuator/health || echo '{"status":"UNKNOWN"}')
if echo "$HEALTH_CHECK" | grep -q '"status":"UP"'; then
echo "✅ Service is healthy!"
# MongoDB bileşeni kontrolü
if echo "$HEALTH_CHECK" | grep -q '"mongo":{"status":"UP"' || docker exec activity-log-service curl -s http://localhost:8095/actuator/health/mongo | grep -q '"status":"UP"'; then
echo "✅ MongoDB connection verified! Deployment successful."
docker logs activity-log-service --tail 20
exit 0
else
echo "Service is up but MongoDB component status unclear. Checking again..."
# 15. denemeden sonra servisi genel olarak UP durumundaysa kabul et
if [ $attempt -gt 15 ]; then
echo "Service is reporting as healthy, accepting deployment despite unclear MongoDB status"
docker logs activity-log-service --tail 20
exit 0
fi
fi
else
echo "Service not yet healthy: $(echo "$HEALTH_CHECK" | grep -o '"status":"[^"]*"' || echo 'no status')"
docker logs activity-log-service --tail 5
fi
sleep 10
attempt=$((attempt + 1))
done
echo "⚠️ Service failed to become fully healthy within timeout"
echo "Final health status: $(curl -s http://localhost:8095/actuator/health || echo 'unavailable')"
echo "CONTAINER LOGS:"
docker logs activity-log-service --tail 50
# MongoDB bağlantı sorunları için son kontrol
echo "FINAL MONGODB CONNECTION CHECK:"
docker exec activity-log-service nc -zv $MONGODB_CONTAINER 27017 || echo "Failed to connect to MongoDB"
docker exec activity-log-service curl -s http://localhost:8095/actuator/health/mongo || echo "MongoDB health component unavailable"
echo "Container will continue running for troubleshooting"