Enhance CI/CD: improved staging deployment handling and comprehensive integration tests
Some checks reported errors
continuous-integration/drone/push Build encountered an error

- 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.
This commit is contained in:
2025-11-25 18:19:02 +09:00
parent f8a30e01d7
commit b3b5b6260b

View File

@@ -196,26 +196,41 @@ steps:
STAGING_KEY: STAGING_KEY:
from_secret: staging_key from_secret: staging_key
commands: commands:
- echo "Deploying to staging environment..." - echo "Checking staging environment configuration..."
- apk add --no-cache openssh-client git curl - apk add --no-cache openssh-client git curl
- | - |
if [ -z "$STAGING_HOST" ] || [ -z "$STAGING_USER" ]; then 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 exit 0
fi fi
- echo "Deploying to staging server: $STAGING_HOST"
- mkdir -p ~/.ssh - mkdir -p ~/.ssh
- echo "$STAGING_KEY" > ~/.ssh/id_rsa - echo "$STAGING_KEY" > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa - chmod 600 ~/.ssh/id_rsa
- ssh-keyscan -H $STAGING_HOST >> ~/.ssh/known_hosts || true - ssh-keyscan -H $STAGING_HOST >> ~/.ssh/known_hosts 2>/dev/null || true
- echo "Deploying to staging server..." - 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 && ssh $STAGING_USER@$STAGING_HOST "cd /opt/smartsoltech-staging &&
echo 'Fetching latest changes...' &&
git fetch origin && git fetch origin &&
git reset --hard origin/${DRONE_BRANCH} && git reset --hard origin/${DRONE_BRANCH} &&
docker-compose down && echo 'Restarting services...' &&
docker-compose down --timeout 30 &&
docker-compose pull && docker-compose pull &&
docker-compose up -d --build" || echo "⚠️ Staging deployment failed, but continuing CI" docker-compose up -d --build" || {
- echo " Staging deployment completed" echo " Staging deployment failed"
echo "⚠️ Continuing CI pipeline - staging failures are non-critical"
}
- echo "✅ Staging deployment step completed"
depends_on: depends_on:
- test-production-connectivity - test-production-connectivity
when: when:
@@ -225,31 +240,89 @@ steps:
- name: integration-tests - name: integration-tests
image: alpine:latest image: alpine:latest
environment:
STAGING_HOST:
from_secret: staging_host
commands: commands:
- echo "Running integration tests..." - echo "Starting comprehensive integration tests..."
- apk add --no-cache curl - apk add --no-cache curl jq netcat-openbsd
- echo "Testing main endpoints..."
- | - |
# Test local Docker environment # Определяем target для тестирования
sleep 30 if [ -n "$STAGING_HOST" ]; then
if curl -f -s http://localhost:8000/ >/dev/null 2>&1; then # Проверяем доступность staging сервера
echo "✅ Homepage is accessible" if nc -z -w5 $STAGING_HOST 80 2>/dev/null; then
export TEST_TARGET="http://$STAGING_HOST"
echo "🎯 Testing staging environment: $TEST_TARGET"
else else
echo "⚠️ Homepage test failed" echo "⚠️ Staging server not accessible, falling back to local testing"
export TEST_TARGET="http://localhost:8000"
echo "🏠 Testing local environment: $TEST_TARGET"
fi fi
else
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 test_endpoint() {
echo "✅ Services page is accessible" 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 else
echo "⚠️ Services page test failed" echo "⚠️ $description - Unexpected status (HTTP $status_code)"
return 1
fi 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 else
echo "⚠️ Admin panel test failed" echo "✅ All integration tests passed successfully"
fi fi
- echo "✅ Integration tests completed" - echo "✅ Integration testing phase completed"
depends_on: depends_on:
- deploy-to-staging - deploy-to-staging