From b3b5b6260b260af9177abf709ea24c1ee1622c06 Mon Sep 17 00:00:00 2001 From: "Andrey K. Choi" Date: Tue, 25 Nov 2025 18:19:02 +0900 Subject: [PATCH] Enhance CI/CD: improved staging deployment handling and comprehensive integration tests - Enhanced staging deployment with better error handling and fallback logic - Added comprehensive integration tests with intelligent target selection (staging vs local) - Improved connectivity checks with detailed status reporting - Added success rate tracking and flexible error tolerance - Enhanced logging and user-friendly output for CI pipeline - Maintained backward compatibility for environments without staging setup These improvements make the CI/CD pipeline more robust and informative. --- .drone.yml | 121 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 97 insertions(+), 24 deletions(-) diff --git a/.drone.yml b/.drone.yml index 7cda31e..d0e3008 100644 --- a/.drone.yml +++ b/.drone.yml @@ -196,26 +196,41 @@ steps: STAGING_KEY: from_secret: staging_key commands: - - echo "Deploying to staging environment..." + - echo "Checking staging environment configuration..." - apk add --no-cache openssh-client git curl - | if [ -z "$STAGING_HOST" ] || [ -z "$STAGING_USER" ]; then - echo "⚠️ Staging credentials not configured, skipping staging deployment" + echo "⚠️ Staging credentials not configured" + echo "ℹ️ Skipping staging deployment - this is normal for development CI" + echo "✅ Continuing with integration tests on local environment" exit 0 fi + - echo "Deploying to staging server: $STAGING_HOST" - mkdir -p ~/.ssh - echo "$STAGING_KEY" > ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa - - ssh-keyscan -H $STAGING_HOST >> ~/.ssh/known_hosts || true - - echo "Deploying to staging server..." + - ssh-keyscan -H $STAGING_HOST >> ~/.ssh/known_hosts 2>/dev/null || true + - echo "Testing staging server connectivity..." + - | + if ! nc -z $STAGING_HOST 22 2>/dev/null; then + echo "❌ Cannot connect to staging server on port 22" + echo "⚠️ Skipping staging deployment due to connectivity issues" + exit 0 + fi + - echo "Deploying to staging environment..." - | ssh $STAGING_USER@$STAGING_HOST "cd /opt/smartsoltech-staging && + echo 'Fetching latest changes...' && git fetch origin && git reset --hard origin/${DRONE_BRANCH} && - docker-compose down && + echo 'Restarting services...' && + docker-compose down --timeout 30 && docker-compose pull && - docker-compose up -d --build" || echo "⚠️ Staging deployment failed, but continuing CI" - - echo "✅ Staging deployment completed" + docker-compose up -d --build" || { + echo "❌ Staging deployment failed" + echo "⚠️ Continuing CI pipeline - staging failures are non-critical" + } + - echo "✅ Staging deployment step completed" depends_on: - test-production-connectivity when: @@ -225,31 +240,89 @@ steps: - name: integration-tests image: alpine:latest + environment: + STAGING_HOST: + from_secret: staging_host commands: - - echo "Running integration tests..." - - apk add --no-cache curl - - echo "Testing main endpoints..." + - echo "Starting comprehensive integration tests..." + - apk add --no-cache curl jq netcat-openbsd - | - # Test local Docker environment - sleep 30 - if curl -f -s http://localhost:8000/ >/dev/null 2>&1; then - echo "✅ Homepage is accessible" + # Определяем target для тестирования + if [ -n "$STAGING_HOST" ]; then + # Проверяем доступность staging сервера + if nc -z -w5 $STAGING_HOST 80 2>/dev/null; then + export TEST_TARGET="http://$STAGING_HOST" + echo "🎯 Testing staging environment: $TEST_TARGET" + else + echo "⚠️ Staging server not accessible, falling back to local testing" + export TEST_TARGET="http://localhost:8000" + echo "🏠 Testing local environment: $TEST_TARGET" + fi else - echo "⚠️ Homepage test failed" + export TEST_TARGET="http://localhost:8000" + echo "🏠 Testing local environment: $TEST_TARGET" fi + - echo "Waiting for services to be ready..." + - sleep 30 + - echo "Running endpoint availability tests..." - | - if curl -f -s http://localhost:8000/services/ >/dev/null 2>&1; then - echo "✅ Services page is accessible" - else - echo "⚠️ Services page test failed" - fi + test_endpoint() { + local url="$1" + local description="$2" + echo "Testing $description ($url)..." + + local status_code=$(curl -o /dev/null -s -w "%{http_code}" -m 10 "$url" 2>/dev/null || echo "000") + + if [ "$status_code" = "200" ]; then + echo "✅ $description - OK (HTTP $status_code)" + return 0 + elif [ "$status_code" = "404" ]; then + echo "⚠️ $description - Not Found (HTTP $status_code)" + return 1 + elif [ "$status_code" = "000" ]; then + echo "❌ $description - Connection Failed" + return 1 + else + echo "⚠️ $description - Unexpected status (HTTP $status_code)" + return 1 + fi + } - | - if curl -f -s http://localhost:8000/admin/ >/dev/null 2>&1; then - echo "✅ Admin panel is accessible" + # Счетчик ошибок + errors=0 + total_tests=0 + + # Основные страницы + total_tests=$((total_tests + 1)) + test_endpoint "$TEST_TARGET/" "Homepage" || ((errors++)) + + total_tests=$((total_tests + 1)) + test_endpoint "$TEST_TARGET/services/" "Services page" || ((errors++)) + + total_tests=$((total_tests + 1)) + test_endpoint "$TEST_TARGET/career/" "Career page" || ((errors++)) + + total_tests=$((total_tests + 1)) + test_endpoint "$TEST_TARGET/contact/" "Contact page" || ((errors++)) + + total_tests=$((total_tests + 1)) + test_endpoint "$TEST_TARGET/admin/" "Admin panel" || echo "ℹ️ Admin panel test failed (expected for production)" + + echo "" + echo "📊 Integration Test Results:" + echo " Total tests: $total_tests" + echo " Failures: $errors" + echo " Success rate: $(( (total_tests - errors) * 100 / total_tests ))%" + + if [ $errors -gt 2 ]; then + echo "❌ Too many critical endpoint failures ($errors)" + exit 1 + elif [ $errors -gt 0 ]; then + echo "⚠️ Some tests failed but within acceptable limits" else - echo "⚠️ Admin panel test failed" + echo "✅ All integration tests passed successfully" fi - - echo "✅ Integration tests completed" + - echo "✅ Integration testing phase completed" depends_on: - deploy-to-staging