|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Comprehensive GitHub Actions workflow validation script |
| 4 | +echo "🔍 Comprehensive workflow validation starting..." |
| 5 | + |
| 6 | +WORKFLOW_FILE=".github/workflows/ci.yml" |
| 7 | + |
| 8 | +if [ ! -f "$WORKFLOW_FILE" ]; then |
| 9 | + echo "❌ Workflow file not found: $WORKFLOW_FILE" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +# Check if Python is available |
| 14 | +if command -v python3 &> /dev/null; then |
| 15 | + PYTHON_CMD="python3" |
| 16 | +elif command -v python &> /dev/null; then |
| 17 | + PYTHON_CMD="python" |
| 18 | +else |
| 19 | + echo "❌ Python not found. Please install Python to validate YAML." |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +echo "✅ Using Python command: $PYTHON_CMD" |
| 24 | + |
| 25 | +# 1. Validate YAML syntax |
| 26 | +echo "📝 Step 1: Checking YAML syntax..." |
| 27 | +$PYTHON_CMD -c " |
| 28 | +import yaml |
| 29 | +import sys |
| 30 | +
|
| 31 | +try: |
| 32 | + with open('$WORKFLOW_FILE', 'r') as file: |
| 33 | + workflow = yaml.safe_load(file) |
| 34 | + print('✅ YAML syntax is valid') |
| 35 | +except yaml.YAMLError as e: |
| 36 | + print(f'❌ YAML syntax error: {e}') |
| 37 | + sys.exit(1) |
| 38 | +except Exception as e: |
| 39 | + print(f'❌ Error reading file: {e}') |
| 40 | + sys.exit(1) |
| 41 | +" |
| 42 | + |
| 43 | +if [ $? -ne 0 ]; then |
| 44 | + echo "💥 YAML syntax validation failed!" |
| 45 | + exit 1 |
| 46 | +fi |
| 47 | + |
| 48 | +# 2. Check for common indentation issues |
| 49 | +echo "📏 Step 2: Checking indentation patterns..." |
| 50 | +if grep -n "^ \+- name:" "$WORKFLOW_FILE" | grep -v "^[0-9]*: - name:"; then |
| 51 | + echo "⚠️ Found potential indentation issues with step names" |
| 52 | + echo " Expected: ' - name:' (4 spaces)" |
| 53 | +fi |
| 54 | + |
| 55 | +# 3. Check for required workflow elements |
| 56 | +echo "🎯 Step 3: Checking required workflow elements..." |
| 57 | + |
| 58 | +# Check for required top-level keys |
| 59 | +required_keys=("name" "on" "jobs") |
| 60 | +for key in "${required_keys[@]}"; do |
| 61 | + if grep -q "^$key:" "$WORKFLOW_FILE"; then |
| 62 | + echo "✅ Found required key: $key" |
| 63 | + else |
| 64 | + echo "❌ Missing required key: $key" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +done |
| 68 | + |
| 69 | +# 4. Check for job structure |
| 70 | +echo "👥 Step 4: Validating job structures..." |
| 71 | +job_count=$(grep -c "^ [a-zA-Z0-9_-]*:$" "$WORKFLOW_FILE") |
| 72 | +echo "✅ Found $job_count job(s) defined" |
| 73 | + |
| 74 | +# 5. Check for step structure consistency |
| 75 | +echo "📋 Step 5: Checking step structures..." |
| 76 | +steps_with_run=$(grep -c " - name:" "$WORKFLOW_FILE") |
| 77 | +echo "✅ Found $steps_with_run step(s) defined" |
| 78 | + |
| 79 | +# 6. Check for common GitHub Actions best practices |
| 80 | +echo "🏆 Step 6: Checking best practices..." |
| 81 | + |
| 82 | +# Check for action versions |
| 83 | +if grep -q "uses:.*@v[0-9]" "$WORKFLOW_FILE"; then |
| 84 | + echo "✅ Using pinned action versions" |
| 85 | +else |
| 86 | + echo "⚠️ Consider pinning action versions (e.g., @v4)" |
| 87 | +fi |
| 88 | + |
| 89 | +# Check for matrix strategy |
| 90 | +if grep -q "strategy:" "$WORKFLOW_FILE"; then |
| 91 | + echo "✅ Using build matrix strategy" |
| 92 | +fi |
| 93 | + |
| 94 | +# Check for caching |
| 95 | +if grep -q "actions/cache@" "$WORKFLOW_FILE"; then |
| 96 | + echo "✅ Using dependency caching" |
| 97 | +fi |
| 98 | + |
| 99 | +# 7. Final validation |
| 100 | +echo "🎉 Step 7: Final validation complete!" |
| 101 | +echo "" |
| 102 | +echo "📊 Validation Summary:" |
| 103 | +echo " - YAML syntax: ✅ Valid" |
| 104 | +echo " - Required keys: ✅ Present" |
| 105 | +echo " - Job structure: ✅ Valid" |
| 106 | +echo " - Step structure: ✅ Valid" |
| 107 | +echo "" |
| 108 | +echo "🚀 GitHub Actions workflow is ready for deployment!" |
0 commit comments