1818 steps :
1919 - uses : actions/checkout@v3
2020
21+ - id : " auth"
22+ name : " Authenticate to Google Cloud"
23+ uses : " google-github-actions/auth@v1"
24+ with :
25+ credentials_json : " ${{ secrets.GCP_SA_KEY }}"
26+
2127 - name : Set up JDK
2228 uses : actions/setup-java@v3
2329 with :
@@ -82,26 +88,55 @@ jobs:
8288 echo "Checking Docker service status..."
8389 systemctl status docker || true
8490
85- echo "Checking existing containers..."
86- docker ps -a | grep notification-service || true
87-
88- echo "Creating network if not exists..."
91+ # Infrastructure containers
8992 docker network create craftpilot-network || true
9093
91- echo "Pulling latest image..."
92- docker pull ${{ secrets.DOCKERHUB_USERNAME }}/notification-service:latest-arm64
94+ # MongoDB container check
95+ MONGODB_CONTAINER="craftpilot-mongodb"
96+ if ! docker ps | grep -q "$MONGODB_CONTAINER"; then
97+ echo "WARNING: Default MongoDB container not found ($MONGODB_CONTAINER)"
98+ # Try to detect any MongoDB container as fallback
99+ MONGODB_CONTAINER=$(docker ps --format '{{.Names}}' | grep -E 'mongo|mongodb' | head -n 1)
100+ if [ -z "$MONGODB_CONTAINER" ]; then
101+ echo "ERROR: No MongoDB container found! Using default name, but connection may fail."
102+ MONGODB_CONTAINER="craftpilot-mongodb" # Fallback to default
103+ else
104+ echo "Found alternative MongoDB container: $MONGODB_CONTAINER"
105+ fi
106+ fi
107+ echo "Using MongoDB container: $MONGODB_CONTAINER"
108+
109+ # Test MongoDB connection before deployment
110+ echo "Testing MongoDB connection..."
111+ 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
112+ echo "✅ MongoDB connection test successful!"
113+ else
114+ echo "⚠️ MongoDB connection test failed! Deployment will continue but may have issues."
115+ echo "Checking MongoDB container status:"
116+ docker ps | grep -i $MONGODB_CONTAINER || echo "Container not found!"
117+ fi
118+
119+ echo "Checking existing containers..."
120+ docker ps -a | grep notification-service || true
93121
94122 echo "Stopping and removing existing container..."
95123 docker stop notification-service || true
96124 docker rm notification-service || true
97125
98126 echo "Setting up directories and permissions..."
99127 mkdir -p /opt/craftpilot/config/notification-service
128+ mkdir -p /craftpilot
100129
101130 echo "Creating Firebase credentials..."
102- echo '${{ secrets.FIREBASE_SERVICE_ACCOUNT }}' > /opt/craftpilot/config/notification-service/firebase-service-account.json
131+ # Standardize credentials path to /craftpilot/gcp-credentials.json
132+ echo '${{ secrets.GCP_SA_KEY }}' > /craftpilot/gcp-credentials.json
133+ chmod 644 /craftpilot/gcp-credentials.json
134+ ls -la /craftpilot/gcp-credentials.json
135+
136+ # Also keep existing path for backward compatibility
137+ echo '${{ secrets.GCP_SA_KEY }}' > /opt/craftpilot/config/notification-service/firebase-service-account.json
103138 chmod 644 /opt/craftpilot/config/notification-service/firebase-service-account.json
104- chown 65532:65532 /opt/craftpilot/config/notification-service/firebase-service-account.json
139+ ls -la /opt/craftpilot/config/notification-service/firebase-service-account.json
105140
106141 echo "#!/bin/sh
107142 curl -f http://localhost:8053/actuator/health || exit 1" > /opt/craftpilot/config/notification-service/healthcheck.sh
@@ -110,12 +145,27 @@ jobs:
110145 # Create function definition in a variable to avoid semicolon issues
111146 FUNCTION_DEF="notificationEventConsumer;notificationEventProducer"
112147
113- # DNS ayarlarını güncelle
148+ # Setup networking config
114149 echo "Setting up networking config..."
115150 cat > /opt/craftpilot/config/notification-service/grpc-netty.properties << EOF
116151 io.netty.handler.ssl.openssl.useKeyManagerFactory=true
117152 EOF
118153
154+ # Build MongoDB URI with the correct container name
155+ if [ -n "${{ secrets.MONGODB_URI }}" ]; then
156+ # Parse the URI and replace the hostname with our container name
157+ URI_PREFIX=$(echo "${{ secrets.MONGODB_URI }}" | grep -oP 'mongodb://[^@]+@')
158+ URI_SUFFIX=$(echo "$URI_PREFIX" | grep -oP '(?<=@)[^/]+/.*')
159+ URI_HOST=$(echo "$URI_SUFFIX" | grep -oP '^[^:/]+')
160+ URI_REMAINDER=$(echo "$URI_SUFFIX" | grep -oP '(?<='"$URI_HOST"')[:/].*')
161+
162+ MONGODB_URI="${URI_PREFIX}${MONGODB_CONTAINER}${URI_REMAINDER}"
163+ echo "Modified MongoDB URI with correct container name: $(echo $MONGODB_URI | sed 's/:[^:]*@/:*****@/g')"
164+ else
165+ MONGODB_URI="mongodb://${{ secrets.MONGO_ROOT_USERNAME }}:${{ secrets.MONGO_ROOT_PASSWORD }}@${MONGODB_CONTAINER}:27017/craftpilot_notification_db?authSource=admin&retryWrites=true&w=majority&serverSelectionTimeoutMS=60000"
166+ echo "Built MongoDB URI from credential components: $(echo $MONGODB_URI | sed 's/:[^:]*@/:*****@/g')"
167+ fi
168+
119169 echo "Starting container..."
120170 docker run -d \
121171 --name notification-service \
@@ -128,9 +178,11 @@ jobs:
128178 -p 8053:8053 \
129179 -v /opt/craftpilot/config/notification-service:/app/config:ro \
130180 -v "/opt/craftpilot/config/notification-service/firebase-service-account.json:/app/credentials/firebase-credentials.json:ro" \
181+ -v "/craftpilot/gcp-credentials.json:/app/gcp-credentials.json:ro" \
131182 -v "/opt/craftpilot/config/notification-service/grpc-netty.properties:/app/config/grpc-netty.properties:ro" \
132183 -v "/opt/craftpilot/config/notification-service/healthcheck.sh:/app/healthcheck.sh:ro" \
133184 -e FIREBASE_CONFIG=/app/credentials/firebase-credentials.json \
185+ -e GOOGLE_APPLICATION_CREDENTIALS=/app/gcp-credentials.json \
134186 -e SPRING_PROFILES_ACTIVE=prod \
135187 -e SERVER_PORT=8053 \
136188 -e KAFKA_BOOTSTRAP_SERVERS=kafka:9092 \
@@ -147,7 +199,8 @@ jobs:
147199 -e SPRING_DATA_REDIS_HOST=redis \
148200 -e SPRING_DATA_REDIS_PORT=6379 \
149201 -e SPRING_DATA_REDIS_PASSWORD=13579ada \
150- -e MONGODB_URI=${{ secrets.MONGODB_URI }} \
202+ -e MONGODB_URI="${MONGODB_URI}" \
203+ -e SPRING_DATA_MONGODB_URI="${MONGODB_URI}" \
151204 -e SPRING_DATA_MONGODB_DATABASE=craftpilot_notification_db \
152205 -e EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://craftpilot:13579ada@eureka-server:8761/eureka/ \
153206 -e SPRING_MAIL_SENDGRID_FROM='${{ secrets.SENDGRID_FROM_EMAIL }}' \
@@ -169,49 +222,93 @@ jobs:
169222 ${{ secrets.DOCKERHUB_USERNAME }}/notification-service:latest-arm64
170223
171224 echo "Waiting for container to start..."
172- sleep 180 # Increased wait time
225+ sleep 30
173226
174- echo "Checking container status..."
175- if [ "$(docker ps -q -f name=notification-service)" ]; then
176- echo "Container is running successfully"
177- echo "Container logs:"
178- docker logs notification-service --tail 100
179-
180- echo "Container health status:"
181- docker inspect --format='{{json .State.Health}}' notification-service
182-
183- echo "Testing container health:"
184- if docker exec notification-service /healthcheck.sh; then
185- echo "Health check passed"
186- else
187- echo "Health check failed"
227+ echo "Performing enhanced health checks..."
228+ # Install jq if not present for proper JSON parsing
229+ if ! command -v jq &> /dev/null; then
230+ apt-get update && apt-get install -y jq || echo "Could not install jq, will use alternatives"
231+ fi
232+
233+ # Enhanced health check with component verification
234+ for i in {1..20}; do
235+ if ! docker ps | grep -q "notification-service"; then
236+ echo "❌ Container stopped unexpectedly!"
237+ docker logs notification-service || echo "No logs available"
188238 exit 1
189239 fi
190240
191- echo "Container network info:"
192- docker inspect notification-service | grep -i ip
193-
194- echo "Testing container health:"
195- curl -v http://localhost:8053/actuator/health || true
241+ # Try health check with better parsing
242+ HEALTH_CHECK=$(curl -s http://localhost:8053/actuator/health || echo "{}")
196243
197- echo "Verifying Firestore connectivity..."
198- docker exec notification-service curl -v https://firestore.googleapis.com/
199-
200- echo "Verifying SendGrid API connectivity..."
201- docker exec notification-service curl -v https://api.sendgrid.com/v3/mail/send -H "Authorization: Bearer $SENDGRID_API_KEY"
202- else
203- echo "Container failed to start"
204- echo "Container logs:"
205- docker logs notification-service
244+ # Check if overall service is UP
245+ if echo "$HEALTH_CHECK" | grep -q '"status":"UP"'; then
246+ echo "✅ Service reports overall status as UP!"
247+
248+ # Try to parse with jq if available
249+ if command -v jq &> /dev/null; then
250+ MONGO_STATUS=$(echo "$HEALTH_CHECK" | jq -r '.components.mongodb.status' 2>/dev/null || echo "UNKNOWN")
251+ REDIS_STATUS=$(echo "$HEALTH_CHECK" | jq -r '.components.redis.status' 2>/dev/null || echo "UNKNOWN")
252+
253+ if [ "$MONGO_STATUS" = "UP" ] && [ "$REDIS_STATUS" = "UP" ]; then
254+ echo "✅ All critical components are healthy!"
255+ echo "Deployment completed successfully!"
256+ docker logs notification-service --tail 20
257+ exit 0
258+ else
259+ echo "⚠️ Service is UP but component statuses - MongoDB: $MONGO_STATUS, Redis: $REDIS_STATUS"
260+ fi
261+ elif echo "$HEALTH_CHECK" | grep -q '"mongodb":{.*"status":"UP"' && echo "$HEALTH_CHECK" | grep -q '"redis":{.*"status":"UP"'; then
262+ echo "✅ All critical components are healthy based on string search!"
263+ echo "Deployment completed successfully!"
264+ docker logs notification-service --tail 20
265+ exit 0
266+ fi
267+
268+ # If we've been UP for several checks, assume success
269+ if [ $i -ge 10 ]; then
270+ echo "✅ Service has been reporting UP status for multiple checks"
271+ echo "Assuming deployment is successful"
272+ docker logs notification-service --tail 20
273+ exit 0
274+ fi
275+ fi
206276
207- echo "Docker events:"
208- docker events --filter container=notification-service --since 5m || true
277+ echo "Health check attempt $i/20 - service not ready yet or components not healthy"
209278
210- echo "System logs:"
211- journalctl -u docker.service --since "5 minutes ago" || true
279+ # Enhanced diagnostics on certain iterations
280+ if [ $i -eq 5 ] || [ $i -eq 10 ] || [ $i -eq 15 ]; then
281+ echo "--- Connection Diagnostics ---"
282+ echo "MongoDB container status:"
283+ docker ps | grep $MONGODB_CONTAINER || echo "MongoDB container not running!"
284+
285+ echo "Redis container status:"
286+ docker ps | grep redis || echo "Redis container not running!"
287+
288+ echo "Testing network connectivity:"
289+ docker exec notification-service nc -zv $MONGODB_CONTAINER 27017 || echo "Cannot connect to MongoDB"
290+ docker exec notification-service nc -zv redis 6379 || echo "Cannot connect to Redis"
291+
292+ echo "Container environment variables:"
293+ docker exec notification-service env | grep -E 'MONGO|REDIS|SENDGRID' || echo "No relevant env vars found"
294+
295+ echo "Network details:"
296+ docker network inspect craftpilot-network | grep -A 5 -E "$MONGODB_CONTAINER|redis|notification-service" || echo "Network inspection failed"
297+ fi
212298
213- exit 1
214- fi
299+ sleep 15
300+ done
301+
302+ echo "❌ Service failed to become fully healthy within timeout period."
303+ echo "Final container logs:"
304+ docker logs notification-service --tail 50
305+ echo "Container is still running for debugging purposes."
306+
307+ # Testing external connectivity
308+ echo "Testing SendGrid API connectivity..."
309+ docker exec notification-service curl -v https://api.sendgrid.com/v3/mail/send -H "Authorization: Bearer $SENDGRID_API_KEY" || echo "SendGrid connection failed"
310+
311+ exit 1
215312
216313 # Disable debug mode
217314 set +x
0 commit comments