|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Local CI/CD Testing Script |
| 4 | +# This script allows you to test individual parts of your CI/CD pipeline locally |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Colors for output |
| 9 | +RED='\033[0;31m' |
| 10 | +GREEN='\033[0;32m' |
| 11 | +YELLOW='\033[1;33m' |
| 12 | +BLUE='\033[0;34m' |
| 13 | +NC='\033[0m' # No Color |
| 14 | + |
| 15 | +# Function to print colored output |
| 16 | +print_status() { |
| 17 | + echo -e "${BLUE}[INFO]${NC} $1" |
| 18 | +} |
| 19 | + |
| 20 | +print_success() { |
| 21 | + echo -e "${GREEN}[SUCCESS]${NC} $1" |
| 22 | +} |
| 23 | + |
| 24 | +print_warning() { |
| 25 | + echo -e "${YELLOW}[WARNING]${NC} $1" |
| 26 | +} |
| 27 | + |
| 28 | +print_error() { |
| 29 | + echo -e "${RED}[ERROR]${NC} $1" |
| 30 | +} |
| 31 | + |
| 32 | +# Function to run security checks |
| 33 | +run_security_checks() { |
| 34 | + print_status "Running security checks..." |
| 35 | + |
| 36 | + # Install dependencies |
| 37 | + npm ci |
| 38 | + |
| 39 | + # Run ESLint |
| 40 | + print_status "Running ESLint..." |
| 41 | + npm run lint |
| 42 | + |
| 43 | + # Run TypeScript check |
| 44 | + print_status "Running TypeScript check..." |
| 45 | + npx tsc --noEmit |
| 46 | + |
| 47 | + # Security audit |
| 48 | + print_status "Running security audit..." |
| 49 | + npm audit --audit-level=moderate |
| 50 | + |
| 51 | + print_success "Security checks completed!" |
| 52 | +} |
| 53 | + |
| 54 | +# Function to run tests |
| 55 | +run_tests() { |
| 56 | + print_status "Running tests..." |
| 57 | + |
| 58 | + # Install dependencies |
| 59 | + npm ci |
| 60 | + |
| 61 | + # Run tests |
| 62 | + print_status "Running test suite..." |
| 63 | + npm run test:ci |
| 64 | + |
| 65 | + print_success "Tests completed!" |
| 66 | +} |
| 67 | + |
| 68 | +# Function to run build |
| 69 | +run_build() { |
| 70 | + print_status "Running build..." |
| 71 | + |
| 72 | + # Install dependencies |
| 73 | + npm ci |
| 74 | + |
| 75 | + # Build application |
| 76 | + print_status "Building application..." |
| 77 | + NODE_ENV=production npm run build |
| 78 | + |
| 79 | + # Analyze bundle size |
| 80 | + print_status "Analyzing bundle size..." |
| 81 | + npm run build:analyze |
| 82 | + |
| 83 | + print_success "Build completed!" |
| 84 | +} |
| 85 | + |
| 86 | +# Function to test Vercel deployment commands |
| 87 | +test_vercel_commands() { |
| 88 | + print_status "Testing Vercel CLI commands..." |
| 89 | + |
| 90 | + # Check if Vercel CLI is installed |
| 91 | + if ! command -v vercel &> /dev/null; then |
| 92 | + print_status "Installing Vercel CLI..." |
| 93 | + npm install -g vercel@latest |
| 94 | + fi |
| 95 | + |
| 96 | + # Test Vercel CLI version |
| 97 | + print_status "Vercel CLI version:" |
| 98 | + vercel --version |
| 99 | + |
| 100 | + # Test linking (this will fail with dummy secrets, but we can see the command structure) |
| 101 | + print_status "Testing Vercel link command structure..." |
| 102 | + echo "vercel link --token \$VERCEL_TOKEN --yes" |
| 103 | + |
| 104 | + # Test deployment command structure |
| 105 | + print_status "Testing Vercel deployment command structure..." |
| 106 | + echo "vercel --token \$VERCEL_TOKEN --yes" |
| 107 | + echo "vercel --prod --token \$VERCEL_TOKEN --yes" |
| 108 | + |
| 109 | + print_success "Vercel command testing completed!" |
| 110 | +} |
| 111 | + |
| 112 | +# Function to run with act (GitHub Actions simulation) |
| 113 | +run_with_act() { |
| 114 | + print_status "Running GitHub Actions simulation with act..." |
| 115 | + |
| 116 | + # Check if act is installed |
| 117 | + if ! command -v act &> /dev/null; then |
| 118 | + print_error "act is not installed. Please install it with: brew install act" |
| 119 | + exit 1 |
| 120 | + fi |
| 121 | + |
| 122 | + # List available workflows |
| 123 | + print_status "Available workflows:" |
| 124 | + act --list |
| 125 | + |
| 126 | + # Run specific job (you can modify this) |
| 127 | + print_status "Running security job..." |
| 128 | + act -j security --secret-file .secrets --dry-run |
| 129 | + |
| 130 | + print_success "Act simulation completed!" |
| 131 | +} |
| 132 | + |
| 133 | +# Function to show help |
| 134 | +show_help() { |
| 135 | + echo "Local CI/CD Testing Script" |
| 136 | + echo "" |
| 137 | + echo "Usage: $0 [OPTION]" |
| 138 | + echo "" |
| 139 | + echo "Options:" |
| 140 | + echo " security Run security checks (lint, typescript, audit)" |
| 141 | + echo " test Run test suite" |
| 142 | + echo " build Run build process" |
| 143 | + echo " vercel Test Vercel CLI commands" |
| 144 | + echo " act Run GitHub Actions simulation with act" |
| 145 | + echo " all Run all checks (security, test, build, vercel)" |
| 146 | + echo " help Show this help message" |
| 147 | + echo "" |
| 148 | + echo "Examples:" |
| 149 | + echo " $0 security # Run only security checks" |
| 150 | + echo " $0 all # Run all checks" |
| 151 | + echo " $0 act # Simulate GitHub Actions" |
| 152 | +} |
| 153 | + |
| 154 | +# Main script logic |
| 155 | +case "${1:-help}" in |
| 156 | + "security") |
| 157 | + run_security_checks |
| 158 | + ;; |
| 159 | + "test") |
| 160 | + run_tests |
| 161 | + ;; |
| 162 | + "build") |
| 163 | + run_build |
| 164 | + ;; |
| 165 | + "vercel") |
| 166 | + test_vercel_commands |
| 167 | + ;; |
| 168 | + "act") |
| 169 | + run_with_act |
| 170 | + ;; |
| 171 | + "all") |
| 172 | + print_status "Running all CI/CD checks..." |
| 173 | + run_security_checks |
| 174 | + run_tests |
| 175 | + run_build |
| 176 | + test_vercel_commands |
| 177 | + print_success "All checks completed!" |
| 178 | + ;; |
| 179 | + "help"|*) |
| 180 | + show_help |
| 181 | + ;; |
| 182 | +esac |
0 commit comments