|
| 1 | +name: Health Check |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, develop] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + |
| 9 | +jobs: |
| 10 | + health-check: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + services: |
| 14 | + postgres: |
| 15 | + image: postgres:15 |
| 16 | + env: |
| 17 | + POSTGRES_PASSWORD: testpassword |
| 18 | + POSTGRES_USER: testuser |
| 19 | + POSTGRES_DB: testdb |
| 20 | + options: >- |
| 21 | + --health-cmd pg_isready |
| 22 | + --health-interval 10s |
| 23 | + --health-timeout 5s |
| 24 | + --health-retries 5 |
| 25 | + ports: |
| 26 | + - 5432:5432 |
| 27 | + |
| 28 | + env: |
| 29 | + DATABASE_URL: postgresql://testuser:testpassword@localhost:5432/testdb |
| 30 | + NODE_ENV: test |
| 31 | + |
| 32 | + steps: |
| 33 | + - name: Checkout code |
| 34 | + uses: actions/checkout@v4 |
| 35 | + |
| 36 | + - name: Setup Node.js |
| 37 | + uses: actions/setup-node@v4 |
| 38 | + with: |
| 39 | + node-version: "18" |
| 40 | + cache: "yarn" |
| 41 | + |
| 42 | + - name: Install dependencies |
| 43 | + run: yarn install --frozen-lockfile |
| 44 | + |
| 45 | + - name: Validate SQL Schema |
| 46 | + run: | |
| 47 | + # Install PostgreSQL client |
| 48 | + sudo apt-get update |
| 49 | + sudo apt-get install -y postgresql-client |
| 50 | +
|
| 51 | + # Test SQL syntax |
| 52 | + echo "Testing SQL syntax..." |
| 53 | + psql $DATABASE_URL -f migrations.sql --dry-run || { |
| 54 | + echo "❌ SQL syntax validation failed" |
| 55 | + exit 1 |
| 56 | + } |
| 57 | + echo "✅ SQL syntax is valid" |
| 58 | +
|
| 59 | + - name: Run Database Migrations |
| 60 | + run: | |
| 61 | + echo "Running migrations..." |
| 62 | + psql $DATABASE_URL -f migrations.sql |
| 63 | + echo "✅ Migrations completed successfully" |
| 64 | +
|
| 65 | + - name: Test Database Extensions |
| 66 | + run: | |
| 67 | + echo "Testing required PostgreSQL extensions..." |
| 68 | + psql $DATABASE_URL -c "SELECT * FROM pg_extension WHERE extname IN ('pg_trgm', 'fuzzystrmatch');" | grep -E "(pg_trgm|fuzzystrmatch)" || { |
| 69 | + echo "❌ Required extensions not found" |
| 70 | + exit 1 |
| 71 | + } |
| 72 | + echo "✅ Required extensions are installed" |
| 73 | +
|
| 74 | + - name: Test Database Connectivity |
| 75 | + run: | |
| 76 | + echo "Testing basic database operations..." |
| 77 | +
|
| 78 | + # Test basic CRUD operations |
| 79 | + psql $DATABASE_URL -c " |
| 80 | + INSERT INTO plates (plate_number) VALUES ('TEST123') ON CONFLICT DO NOTHING; |
| 81 | + SELECT plate_number FROM plates WHERE plate_number = 'TEST123'; |
| 82 | + " | grep "TEST123" || { |
| 83 | + echo "❌ Basic database operations failed" |
| 84 | + exit 1 |
| 85 | + } |
| 86 | + echo "✅ Database connectivity and basic operations work" |
| 87 | +
|
| 88 | + - name: Build Application |
| 89 | + run: | |
| 90 | + echo "Building Next.js application..." |
| 91 | + yarn build |
| 92 | + echo "✅ Application built successfully" |
| 93 | +
|
| 94 | + - name: Test Application Startup |
| 95 | + run: | |
| 96 | + echo "Testing application startup..." |
| 97 | +
|
| 98 | + # Start the app in background |
| 99 | + yarn start & |
| 100 | + APP_PID=$! |
| 101 | +
|
| 102 | + # Wait for app to start |
| 103 | + sleep 10 |
| 104 | +
|
| 105 | + # Test if app responds |
| 106 | + curl -f http://localhost:3000 || { |
| 107 | + echo "❌ Application failed to start or respond" |
| 108 | + kill $APP_PID 2>/dev/null |
| 109 | + exit 1 |
| 110 | + } |
| 111 | +
|
| 112 | + # Cleanup |
| 113 | + kill $APP_PID 2>/dev/null |
| 114 | + echo "✅ Application starts and responds successfully" |
| 115 | +
|
| 116 | + - name: Test Critical API Endpoints |
| 117 | + run: | |
| 118 | + echo "Testing critical API endpoints..." |
| 119 | + |
| 120 | + # Start the app in background |
| 121 | + yarn start & |
| 122 | + APP_PID=$! |
| 123 | + sleep 10 |
| 124 | + |
| 125 | + # Test health check endpoint (if it exists) |
| 126 | + curl -f http://localhost:3000/api/health-check || echo "⚠️ Health check endpoint not found (optional)" |
| 127 | + |
| 128 | + # Test MQTT brokers endpoint |
| 129 | + curl -f http://localhost:3000/api/mqtt/brokers || { |
| 130 | + echo "❌ MQTT brokers API endpoint failed" |
| 131 | + kill $APP_PID 2>/dev/null |
| 132 | + exit 1 |
| 133 | + } |
| 134 | + |
| 135 | + # Test plate-reads endpoint with test payload |
| 136 | + echo "Testing plate-reads endpoint with test payload..." |
| 137 | + curl -X POST \ |
| 138 | + -H "Content-Type: application/json" \ |
| 139 | + -d @test-payload.json \ |
| 140 | + -f http://localhost:3000/api/plate-reads || { |
| 141 | + echo "❌ Plate-reads API endpoint failed with test payload" |
| 142 | + kill $APP_PID 2>/dev/null |
| 143 | + exit 1 |
| 144 | + } |
| 145 | + echo "✅ Plate-reads endpoint processed test payload successfully" |
| 146 | + |
| 147 | + # Verify the plate was actually processed (check database) |
| 148 | + echo "Verifying plate data was processed..." |
| 149 | + PLATE_COUNT=$(psql $DATABASE_URL -t -c "SELECT COUNT(*) FROM plate_reads WHERE plate_number IS NOT NULL;") |
| 150 | + if [ "$PLATE_COUNT" -gt 0 ]; then |
| 151 | + echo "✅ Plate data was successfully stored in database ($PLATE_COUNT records)" |
| 152 | + else |
| 153 | + echo "⚠️ No plate data found in database (may be expected for test payload)" |
| 154 | + fi |
| 155 | + |
| 156 | + # Cleanup |
| 157 | + kill $APP_PID 2>/dev/null |
| 158 | + echo "✅ Critical API endpoints respond correctly" |
| 159 | +
|
| 160 | + - name: Validate Configuration Files |
| 161 | + run: | |
| 162 | + echo "Validating configuration files..." |
| 163 | +
|
| 164 | + # Check if package.json is valid |
| 165 | + node -e "JSON.parse(require('fs').readFileSync('package.json'))" || { |
| 166 | + echo "❌ package.json is invalid" |
| 167 | + exit 1 |
| 168 | + } |
| 169 | +
|
| 170 | + # Check if next.config.js loads without errors |
| 171 | + node -e "require('./next.config.js')" || { |
| 172 | + echo "❌ next.config.js has errors" |
| 173 | + exit 1 |
| 174 | + } |
| 175 | +
|
| 176 | + echo "✅ Configuration files are valid" |
| 177 | +
|
| 178 | + - name: Test Database Schema Integrity |
| 179 | + run: | |
| 180 | + echo "Testing database schema integrity..." |
| 181 | +
|
| 182 | + # Test foreign key constraints |
| 183 | + psql $DATABASE_URL -c " |
| 184 | + SELECT |
| 185 | + tc.table_name, |
| 186 | + kcu.column_name, |
| 187 | + ccu.table_name AS foreign_table_name, |
| 188 | + ccu.column_name AS foreign_column_name |
| 189 | + FROM information_schema.table_constraints AS tc |
| 190 | + JOIN information_schema.key_column_usage AS kcu |
| 191 | + ON tc.constraint_name = kcu.constraint_name |
| 192 | + AND tc.table_schema = kcu.table_schema |
| 193 | + JOIN information_schema.constraint_column_usage AS ccu |
| 194 | + ON ccu.constraint_name = tc.constraint_name |
| 195 | + AND ccu.table_schema = tc.table_schema |
| 196 | + WHERE tc.constraint_type = 'FOREIGN KEY' |
| 197 | + AND tc.table_name IN ('mqttnotifications', 'plate_tags', 'plate_reads'); |
| 198 | + " || { |
| 199 | + echo "❌ Foreign key constraints check failed" |
| 200 | + exit 1 |
| 201 | + } |
| 202 | +
|
| 203 | + echo "✅ Database schema integrity verified" |
| 204 | +
|
| 205 | + - name: Test Environment Variables |
| 206 | + run: | |
| 207 | + echo "Testing environment variable handling..." |
| 208 | +
|
| 209 | + # Test that the app can handle missing env vars gracefully |
| 210 | + node -e " |
| 211 | + try { |
| 212 | + require('./lib/settings.js'); |
| 213 | + console.log('✅ Settings module loads correctly'); |
| 214 | + } catch (error) { |
| 215 | + console.error('❌ Settings module failed to load:', error.message); |
| 216 | + process.exit(1); |
| 217 | + } |
| 218 | + " |
| 219 | +
|
| 220 | + - name: Summary |
| 221 | + if: always() |
| 222 | + run: | |
| 223 | + echo "🎉 Health check completed!" |
| 224 | + echo "All critical systems verified:" |
| 225 | + echo " ✅ SQL schema validation" |
| 226 | + echo " ✅ Database connectivity" |
| 227 | + echo " ✅ Application build and startup" |
| 228 | + echo " ✅ Critical API endpoints" |
| 229 | + echo " ✅ Configuration validation" |
| 230 | + echo " ✅ Database schema integrity" |
0 commit comments