🔒 Add Pre-Push Hook & Fix GitHub Actions
✅ Pre-Push Hook System: - Created scripts/pre-push.sh with comprehensive checks - Added Git pre-push hook (.git/hooks/pre-push) - Added npm run pre-push script - Added npm run lint:fix script 🔧 Pre-Push Checks: - Dependencies installation (npm ci) - ESLint validation (npm run lint) - Test execution (npm run test) - Build verification (npm run build) - Security audit (npm audit) - TypeScript type check (tsc --noEmit) ✅ GitHub Actions Fix: - Removed deprecated GHOST_API variables - Updated environment variables to match current .env - Fixed test and production environment setup 🎯 Benefits: - No more failed pushes to GitHub - All checks run locally before push - Same checks as GitHub Actions - Prevents broken code from reaching remote
This commit is contained in:
14
.github/workflows/ci-cd.yml
vendored
14
.github/workflows/ci-cd.yml
vendored
@@ -31,13 +31,16 @@ jobs:
|
|||||||
- name: Create test environment file
|
- name: Create test environment file
|
||||||
run: |
|
run: |
|
||||||
cat > .env <<EOF
|
cat > .env <<EOF
|
||||||
|
NODE_ENV=test
|
||||||
NEXT_PUBLIC_BASE_URL=http://localhost:3000
|
NEXT_PUBLIC_BASE_URL=http://localhost:3000
|
||||||
GHOST_API_URL=test
|
|
||||||
GHOST_API_KEY=test
|
|
||||||
MY_EMAIL=test@example.com
|
MY_EMAIL=test@example.com
|
||||||
MY_INFO_EMAIL=test@example.com
|
MY_INFO_EMAIL=test@example.com
|
||||||
MY_PASSWORD=test
|
MY_PASSWORD=test
|
||||||
MY_INFO_PASSWORD=test
|
MY_INFO_PASSWORD=test
|
||||||
|
NEXT_PUBLIC_UMAMI_URL=https://analytics.dk0.dev
|
||||||
|
NEXT_PUBLIC_UMAMI_WEBSITE_ID=b3665829-927a-4ada-b9bb-fcf24171061e
|
||||||
|
ADMIN_BASIC_AUTH=admin:test
|
||||||
|
LOG_LEVEL=info
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
- name: Run linting
|
- name: Run linting
|
||||||
@@ -109,13 +112,16 @@ jobs:
|
|||||||
- name: Create production environment file
|
- name: Create production environment file
|
||||||
run: |
|
run: |
|
||||||
cat > .env <<EOF
|
cat > .env <<EOF
|
||||||
|
NODE_ENV=production
|
||||||
NEXT_PUBLIC_BASE_URL=${{ vars.NEXT_PUBLIC_BASE_URL }}
|
NEXT_PUBLIC_BASE_URL=${{ vars.NEXT_PUBLIC_BASE_URL }}
|
||||||
GHOST_API_URL=${{ vars.GHOST_API_URL }}
|
|
||||||
GHOST_API_KEY=${{ secrets.GHOST_API_KEY }}
|
|
||||||
MY_EMAIL=${{ vars.MY_EMAIL }}
|
MY_EMAIL=${{ vars.MY_EMAIL }}
|
||||||
MY_INFO_EMAIL=${{ vars.MY_INFO_EMAIL }}
|
MY_INFO_EMAIL=${{ vars.MY_INFO_EMAIL }}
|
||||||
MY_PASSWORD=${{ secrets.MY_PASSWORD }}
|
MY_PASSWORD=${{ secrets.MY_PASSWORD }}
|
||||||
MY_INFO_PASSWORD=${{ secrets.MY_INFO_PASSWORD }}
|
MY_INFO_PASSWORD=${{ secrets.MY_INFO_PASSWORD }}
|
||||||
|
NEXT_PUBLIC_UMAMI_URL=${{ vars.NEXT_PUBLIC_UMAMI_URL }}
|
||||||
|
NEXT_PUBLIC_UMAMI_WEBSITE_ID=${{ vars.NEXT_PUBLIC_UMAMI_WEBSITE_ID }}
|
||||||
|
ADMIN_BASIC_AUTH=${{ secrets.ADMIN_BASIC_AUTH }}
|
||||||
|
LOG_LEVEL=info
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
- name: Build and push Docker image
|
- name: Build and push Docker image
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
"build": "next build",
|
"build": "next build",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"lint": "eslint .",
|
"lint": "eslint .",
|
||||||
|
"lint:fix": "eslint . --fix",
|
||||||
|
"pre-push": "./scripts/pre-push.sh",
|
||||||
"buildAnalyze": "cross-env ANALYZE=true next build",
|
"buildAnalyze": "cross-env ANALYZE=true next build",
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"test:watch": "jest --watch",
|
"test:watch": "jest --watch",
|
||||||
|
|||||||
110
scripts/pre-push.sh
Executable file
110
scripts/pre-push.sh
Executable file
@@ -0,0 +1,110 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Pre-Push Hook Script
|
||||||
|
# Runs all checks locally before allowing push to remote
|
||||||
|
|
||||||
|
set -e # Exit on any error
|
||||||
|
|
||||||
|
echo "🚀 Running Pre-Push Checks..."
|
||||||
|
echo "================================"
|
||||||
|
|
||||||
|
# Colors for output
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
BLUE='\033[0;34m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
|
||||||
|
# Function to print colored output
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check if we're in a git repository
|
||||||
|
if ! git rev-parse --git-dir > /dev/null 2>&1; then
|
||||||
|
print_error "Not in a git repository!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get current branch
|
||||||
|
CURRENT_BRANCH=$(git branch --show-current)
|
||||||
|
print_status "Current branch: $CURRENT_BRANCH"
|
||||||
|
|
||||||
|
# Check if there are uncommitted changes
|
||||||
|
if ! git diff-index --quiet HEAD --; then
|
||||||
|
print_error "You have uncommitted changes. Please commit or stash them first."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 1. Install dependencies
|
||||||
|
print_status "Installing dependencies..."
|
||||||
|
if ! npm ci --silent; then
|
||||||
|
print_error "Failed to install dependencies"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
print_success "Dependencies installed"
|
||||||
|
|
||||||
|
# 2. Run ESLint
|
||||||
|
print_status "Running ESLint..."
|
||||||
|
if ! npm run lint; then
|
||||||
|
print_error "ESLint failed! Please fix the errors before pushing."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
print_success "ESLint passed"
|
||||||
|
|
||||||
|
# 3. Run Tests
|
||||||
|
print_status "Running tests..."
|
||||||
|
if ! npm run test; then
|
||||||
|
print_error "Tests failed! Please fix the failing tests before pushing."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
print_success "All tests passed"
|
||||||
|
|
||||||
|
# 4. Build Application
|
||||||
|
print_status "Building application..."
|
||||||
|
if ! npm run build; then
|
||||||
|
print_error "Build failed! Please fix the build errors before pushing."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
print_success "Build successful"
|
||||||
|
|
||||||
|
# 5. Security Audit
|
||||||
|
print_status "Running security audit..."
|
||||||
|
if ! npm audit --audit-level=moderate; then
|
||||||
|
print_warning "Security vulnerabilities found. Consider running 'npm audit fix'"
|
||||||
|
# Don't fail the push for security warnings, just warn
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 6. Type Check
|
||||||
|
print_status "Running TypeScript type check..."
|
||||||
|
if ! npx tsc --noEmit; then
|
||||||
|
print_error "TypeScript type check failed!"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
print_success "TypeScript type check passed"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "================================"
|
||||||
|
print_success "All pre-push checks passed! ✅"
|
||||||
|
print_status "Ready to push to $CURRENT_BRANCH"
|
||||||
|
echo "================================"
|
||||||
|
|
||||||
|
# Optional: Show what will be pushed
|
||||||
|
echo ""
|
||||||
|
print_status "Files to be pushed:"
|
||||||
|
git diff --name-only origin/$CURRENT_BRANCH..HEAD 2>/dev/null || git diff --name-only HEAD~1..HEAD
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
print_status "Proceeding with push..."
|
||||||
Reference in New Issue
Block a user