-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate-ci.sh
More file actions
executable file
·338 lines (293 loc) · 11.9 KB
/
validate-ci.sh
File metadata and controls
executable file
·338 lines (293 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/bash
# CI Validation Script
# This script validates GitHub Actions workflows and Docker Compose setups locally
# Usage: ./validate-ci.sh [quick|full|workflows-only]
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to validate GitHub Actions workflow syntax
validate_workflows() {
print_status "Validating GitHub Actions workflows..."
if command -v gh >/dev/null 2>&1; then
print_status "Using GitHub CLI to validate workflows..."
for workflow in .github/workflows/*.yml; do
if [ -f "$workflow" ]; then
print_status "Validating $(basename "$workflow")..."
# Check workflow syntax using GitHub CLI
gh_output=$(gh workflow view "$(basename "$workflow")" 2>&1)
gh_exit_code=$?
if [ $gh_exit_code -eq 0 ]; then
print_success "✅ $(basename "$workflow") syntax is valid (GitHub CLI)"
elif echo "$gh_output" | grep -q "gh auth login"; then
print_warning "⚠️ GitHub CLI not authenticated, falling back to YAML validation"
# Fallback to basic YAML validation
if command -v yq >/dev/null 2>&1; then
# Try different yq syntaxes (mikefarah vs python version)
if yq eval . "$workflow" >/dev/null 2>&1; then
print_success "✅ $(basename "$workflow") YAML syntax is valid (yq mikefarah)"
elif yq . "$workflow" >/dev/null 2>&1; then
print_success "✅ $(basename "$workflow") YAML syntax is valid (yq python)"
else
print_error "❌ $(basename "$workflow") has invalid YAML syntax"
return 1
fi
else
print_warning "Install 'yq' for YAML validation"
fi
else
print_warning "⚠️ Could not validate $(basename "$workflow") with GitHub CLI: $gh_output"
# Fallback to basic YAML validation
if command -v yq >/dev/null 2>&1; then
# Try different yq syntaxes (mikefarah vs python version)
if yq eval . "$workflow" >/dev/null 2>&1; then
print_success "✅ $(basename "$workflow") YAML syntax is valid (yq mikefarah)"
elif yq . "$workflow" >/dev/null 2>&1; then
print_success "✅ $(basename "$workflow") YAML syntax is valid (yq python)"
else
print_error "❌ $(basename "$workflow") has invalid YAML syntax"
return 1
fi
else
print_warning "Install 'yq' for better YAML validation"
fi
fi
fi
done
else
print_warning "GitHub CLI not found. Install 'gh' for better workflow validation"
# Basic YAML validation
if command -v yq >/dev/null 2>&1; then
for workflow in .github/workflows/*.yml; do
if [ -f "$workflow" ]; then
print_status "Basic YAML validation for $(basename "$workflow")..."
# Try different yq syntaxes (mikefarah vs python version)
if yq eval . "$workflow" >/dev/null 2>&1; then
print_success "✅ $(basename "$workflow") YAML syntax is valid (yq mikefarah)"
elif yq . "$workflow" >/dev/null 2>&1; then
print_success "✅ $(basename "$workflow") YAML syntax is valid (yq python)"
else
print_error "❌ $(basename "$workflow") has invalid YAML syntax"
return 1
fi
fi
done
else
print_warning "Install 'yq' for YAML validation"
fi
fi
print_success "GitHub Actions workflow validation completed"
}
# Function to validate Docker Compose files
validate_docker_compose() {
print_status "Validating Docker Compose files..."
# Set temporary credentials for validation
export MONGODB_USERNAME="validation_user"
export MONGODB_PASSWORD="validation_password"
export MONGODB_DATABASE="validation_db"
export COUCHDB_USERNAME="validation_admin"
export COUCHDB_PASSWORD="validation_password"
export COUCHDB_DATABASE="validation_db"
for compose_file in docker-compose.*.yml; do
if [ -f "$compose_file" ]; then
print_status "Validating $compose_file..."
# Check compose file syntax
if docker compose -f "$compose_file" config >/dev/null 2>&1; then
print_success "✅ $compose_file syntax is valid"
else
print_error "❌ $compose_file has invalid syntax"
print_status "Running detailed validation..."
docker compose -f "$compose_file" config || true
return 1
fi
# Check for required environment variables
print_status "Checking required environment variables for $compose_file..."
if grep -q "MONGODB" "$compose_file"; then
missing_vars=()
for var in MONGODB_USERNAME MONGODB_PASSWORD MONGODB_DATABASE; do
if ! grep -q "$var" "$compose_file"; then
missing_vars+=("$var")
fi
done
if [ ${#missing_vars[@]} -eq 0 ]; then
print_success "✅ All MongoDB environment variables are referenced"
else
print_warning "⚠️ Missing MongoDB variables: ${missing_vars[*]}"
fi
fi
if grep -q "COUCHDB" "$compose_file"; then
missing_vars=()
for var in COUCHDB_USERNAME COUCHDB_PASSWORD COUCHDB_DATABASE; do
if ! grep -q "$var" "$compose_file"; then
missing_vars+=("$var")
fi
done
if [ ${#missing_vars[@]} -eq 0 ]; then
print_success "✅ All CouchDB environment variables are referenced"
else
print_warning "⚠️ Missing CouchDB variables: ${missing_vars[*]}"
fi
fi
fi
done
print_success "Docker Compose validation completed"
}
# Function to test Docker Compose setups locally
test_docker_setups() {
print_status "Testing Docker Compose setups locally..."
if [ ! -f "test-docker-setups.sh" ]; then
print_error "test-docker-setups.sh not found"
return 1
fi
chmod +x test-docker-setups.sh
print_status "Running quick validation tests..."
# Test with minimal credentials (just syntax validation)
export MONGODB_USERNAME="test_user"
export MONGODB_PASSWORD="test_password"
export MONGODB_DATABASE="test_db"
export COUCHDB_USERNAME="test_admin"
export COUCHDB_PASSWORD="test_password"
export COUCHDB_DATABASE="test_db"
# Test that compose files can be parsed with credentials
for db in mongodb couchdb; do
print_status "Testing $db compose file with credentials..."
if docker compose -f "docker-compose.$db.yml" config >/dev/null 2>&1; then
print_success "✅ docker-compose.$db.yml works with test credentials"
else
print_error "❌ docker-compose.$db.yml failed with test credentials"
return 1
fi
done
print_success "Local Docker Compose setup validation completed"
}
# Function to run full integration tests
run_full_tests() {
print_status "Running full integration tests..."
if [ ! -f "test-docker-setups.sh" ]; then
print_error "test-docker-setups.sh not found"
return 1
fi
# Set unique test credentials
export MONGODB_USERNAME="ci_test_user_$(date +%s)"
export MONGODB_PASSWORD="ci_test_password_$(openssl rand -hex 16)"
export MONGODB_DATABASE="ci_test_db"
export COUCHDB_USERNAME="ci_test_admin_$(date +%s)"
export COUCHDB_PASSWORD="ci_test_password_$(openssl rand -hex 16)"
export COUCHDB_DATABASE="ci_test_db"
print_status "Testing MongoDB setup..."
if ./test-docker-setups.sh mongodb; then
print_success "✅ MongoDB integration test passed"
else
print_error "❌ MongoDB integration test failed"
return 1
fi
print_status "Testing CouchDB setup..."
if ./test-docker-setups.sh couchdb; then
print_success "✅ CouchDB integration test passed"
else
print_error "❌ CouchDB integration test failed"
return 1
fi
print_success "Full integration tests completed"
}
# Function to check prerequisites
check_prerequisites() {
print_status "Checking prerequisites..."
# Check Docker
if command -v docker >/dev/null 2>&1; then
print_success "✅ Docker is installed: $(docker --version)"
else
print_error "❌ Docker is not installed"
return 1
fi
# Check Docker Compose
if docker compose version >/dev/null 2>&1; then
print_success "✅ Docker Compose is available: $(docker compose version)"
else
print_error "❌ Docker Compose is not available"
return 1
fi
# Check if Docker daemon is running
if docker info >/dev/null 2>&1; then
print_success "✅ Docker daemon is running"
else
print_error "❌ Docker daemon is not running"
return 1
fi
# Optional tools
for tool in yq jq gh curl; do
if command -v "$tool" >/dev/null 2>&1; then
print_success "✅ $tool is available"
else
print_warning "⚠️ $tool is not installed (optional but recommended)"
fi
done
print_success "Prerequisites check completed"
}
# Function to display usage
usage() {
echo "Usage: $0 [quick|full|workflows-only]"
echo ""
echo "Options:"
echo " quick - Validate workflows and compose files only (default)"
echo " full - Run full integration tests"
echo " workflows-only - Validate GitHub Actions workflows only"
echo ""
echo "Examples:"
echo " $0 # Quick validation"
echo " $0 full # Full integration tests"
echo " $0 workflows-only # Just workflow validation"
}
# Main execution
main() {
local mode="${1:-quick}"
echo "🔍 CI Validation Script"
echo "======================="
echo ""
case "$mode" in
"quick")
check_prerequisites
validate_workflows
validate_docker_compose
test_docker_setups
print_success "🎉 Quick validation completed successfully!"
;;
"full")
check_prerequisites
validate_workflows
validate_docker_compose
run_full_tests
print_success "🎉 Full validation completed successfully!"
;;
"workflows-only")
validate_workflows
print_success "🎉 Workflow validation completed successfully!"
;;
"-h"|"--help"|"help")
usage
exit 0
;;
*)
print_error "Unknown option: $mode"
usage
exit 1
;;
esac
}
# Run main function with all arguments
main "$@"