6666 username : ${{ secrets.SSH_USERNAME }}
6767 key : ${{ secrets.SSH_PRIVATE_KEY }}
6868 script : |
69+ # Ensure network exists
70+ echo "=== Creating or verifying network ==="
71+ docker network create craftpilot-network 2>/dev/null || true
72+
73+ # Determine MongoDB container and connection info
74+ echo "=== Checking MongoDB container ==="
75+ MONGODB_CONTAINER="craftpilot-mongodb"
76+ if ! docker ps | grep -q "$MONGODB_CONTAINER"; then
77+ echo "WARNING: Default MongoDB container not found, trying alternative names..."
78+ MONGODB_CONTAINER=$(docker ps --format '{{.Names}}' | grep -E 'mongo|mongodb' | head -n 1)
79+
80+ if [ -z "$MONGODB_CONTAINER" ]; then
81+ echo "ERROR: No MongoDB container found, using default name"
82+ MONGODB_CONTAINER="craftpilot-mongodb"
83+ else
84+ echo "Found MongoDB container: $MONGODB_CONTAINER"
85+ fi
86+ fi
87+ echo "Using MongoDB container: $MONGODB_CONTAINER"
88+
89+ # Configure MongoDB URI
90+ if [ -n "${{ secrets.MONGODB_URI }}" ]; then
91+ # Use the URI directly from secrets if available
92+ MONGODB_URI="${{ secrets.MONGODB_URI }}"
93+ echo "Using pre-configured MongoDB URI from secrets"
94+ else
95+ # Otherwise, build the URI from components
96+ MONGODB_URI="mongodb://${{ secrets.MONGO_ROOT_USERNAME }}:${{ secrets.MONGO_ROOT_PASSWORD }}@${MONGODB_CONTAINER}:27017/${{ secrets.MONGO_INITDB_DATABASE || 'craftpilot' }}?authSource=admin&retryWrites=true&w=majority&serverSelectionTimeoutMS=30000&connectTimeoutMS=20000&socketTimeoutMS=60000"
97+ echo "Built MongoDB URI from credential components"
98+ fi
99+ echo "MongoDB URI: $(echo $MONGODB_URI | sed 's/:[^:]*@/:\*\*\*@/g')"
69100
70101 # Deploy Credit Service
102+ echo "=== Deploying Credit Service ==="
71103 docker pull ${{ secrets.DOCKERHUB_USERNAME }}/credit-service:latest-arm64
72104 docker stop credit-service || true
73105 docker rm credit-service || true
@@ -84,23 +116,65 @@ jobs:
84116 -e GOOGLE_APPLICATION_CREDENTIALS=/gcp-credentials.json \
85117 -e SPRING_SECURITY_USER_NAME=craftpilot \
86118 -e SPRING_SECURITY_USER_PASSWORD=13579ada \
119+ -e MONGODB_URI="$MONGODB_URI" \
120+ -e MONGODB_DATABASE="${{ secrets.MONGO_INITDB_DATABASE || 'craftpilot' }}" \
87121 -e REDIS_HOST=redis \
88122 -e REDIS_PORT=6379 \
89123 -e REDIS_PASSWORD=13579ada \
90124 -e MANAGEMENT_HEALTH_VALIDATE_GROUP_MEMBERSHIP=false \
125+ -e MANAGEMENT_ENDPOINTS_WEB_EXPOSURE_INCLUDE="health,info,metrics,prometheus" \
126+ -e MANAGEMENT_ENDPOINT_HEALTH_SHOW_DETAILS=always \
91127 ${{ secrets.DOCKERHUB_USERNAME }}/credit-service:latest-arm64
92128
129+ # Verify MongoDB connection
130+ echo "=== Verifying MongoDB connection ==="
131+ sleep 5
132+ if docker exec credit-service nc -z -w5 $MONGODB_CONTAINER 27017; then
133+ echo "✅ MongoDB connection successful"
134+ else
135+ echo "⚠️ MongoDB connection check failed"
136+ echo "Testing connection from host machine..."
137+ nc -z -w5 localhost 27017 && echo "Host can connect to MongoDB" || echo "Host cannot connect to MongoDB"
138+
139+ # Check network details
140+ echo "Container network details:"
141+ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' credit-service
142+ docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $MONGODB_CONTAINER
143+ fi
144+
93145 # Enhanced health check with longer timeout
94- echo "Waiting for service to start..."
95- for i in {1..12}; do
146+ echo "=== Waiting for service to start ==="
147+ MAX_ATTEMPTS=12
148+ ATTEMPT=1
149+
150+ while [ $ATTEMPT -le $MAX_ATTEMPTS ]; do
151+ echo "Health check attempt $ATTEMPT/$MAX_ATTEMPTS"
152+
153+ # Check container status
154+ if ! docker ps | grep -q "credit-service"; then
155+ echo "Container stopped unexpectedly! Checking logs..."
156+ docker logs credit-service
157+ exit 1
158+ fi
159+
160+ # Check health
96161 if curl -s http://localhost:8058/actuator/health | grep -q "UP"; then
97- echo "Service is healthy"
162+ echo "✅ Service is healthy"
98163 exit 0
99164 fi
100- echo "Attempt $i: Service not ready yet..."
165+
166+ echo "Service not ready yet... Waiting"
167+
168+ # Show logs at middle of attempts
169+ if [ $ATTEMPT -eq 6 ]; then
170+ echo "Service logs:"
171+ docker logs credit-service --tail 50
172+ fi
173+
174+ ATTEMPT=$((ATTEMPT + 1))
101175 sleep 10
102176 done
103177
104- echo "Service failed to become healthy within timeout"
178+ echo "❌ Service failed to become healthy within timeout"
105179 docker logs credit-service
106180 exit 1
0 commit comments