feat: Add test deployment workflow and setup documentation
- Implemented a new GitHub Actions workflow for testing deployments (`test-deployment.yml`). - Created detailed deployment documentation (`DEPLOYMENT-README.md`, `DEPLOYMENT.md`, `QUICK-START.md`) for setting up the testing server and configuring GitHub secrets. - Added a setup script (`setup-testing-server.sh`) for automating the environment setup on the testing server, including Docker, Nginx, and user configurations. - Included monitoring and cleanup scripts for managing deployments on the server.
This commit is contained in:
293
scripts/setup-testing-server.sh
Executable file
293
scripts/setup-testing-server.sh
Executable file
@ -0,0 +1,293 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Quick setup script for testing server
|
||||
# Run this script on your testing server to set up the environment
|
||||
|
||||
set -e
|
||||
|
||||
echo "🚀 Setting up testing server for Low Code Engine deployments..."
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Function to print colored output
|
||||
print_status() {
|
||||
echo -e "${GREEN}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# Check if running as root
|
||||
if [[ $EUID -eq 0 ]]; then
|
||||
print_error "This script should not be run as root for security reasons"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Update system
|
||||
print_status "Updating system packages..."
|
||||
sudo apt update && sudo apt upgrade -y
|
||||
|
||||
# Install Docker
|
||||
print_status "Installing Docker..."
|
||||
if ! command -v docker &> /dev/null; then
|
||||
curl -fsSL https://get.docker.com -o get-docker.sh
|
||||
sudo sh get-docker.sh
|
||||
sudo usermod -aG docker $USER
|
||||
rm get-docker.sh
|
||||
print_status "Docker installed successfully"
|
||||
else
|
||||
print_status "Docker is already installed"
|
||||
fi
|
||||
|
||||
# Install Docker Compose
|
||||
print_status "Installing Docker Compose..."
|
||||
if ! command -v docker-compose &> /dev/null; then
|
||||
sudo apt install docker-compose-plugin -y
|
||||
print_status "Docker Compose installed successfully"
|
||||
else
|
||||
print_status "Docker Compose is already installed"
|
||||
fi
|
||||
|
||||
# Create deploy user
|
||||
print_status "Creating deploy user..."
|
||||
if ! id "deploy" &>/dev/null; then
|
||||
sudo useradd -m -s /bin/bash deploy
|
||||
sudo usermod -aG docker deploy
|
||||
print_status "Deploy user created successfully"
|
||||
else
|
||||
print_status "Deploy user already exists"
|
||||
fi
|
||||
|
||||
# Setup SSH directory for deploy user
|
||||
print_status "Setting up SSH for deploy user..."
|
||||
sudo mkdir -p /home/deploy/.ssh
|
||||
sudo chmod 700 /home/deploy/.ssh
|
||||
sudo touch /home/deploy/.ssh/authorized_keys
|
||||
sudo chmod 600 /home/deploy/.ssh/authorized_keys
|
||||
sudo chown -R deploy:deploy /home/deploy/.ssh
|
||||
|
||||
# Create application directory
|
||||
print_status "Creating application directory..."
|
||||
sudo mkdir -p /opt/low-code-engine
|
||||
sudo chown deploy:deploy /opt/low-code-engine
|
||||
|
||||
# Install nginx (optional)
|
||||
print_status "Installing Nginx..."
|
||||
if ! command -v nginx &> /dev/null; then
|
||||
sudo apt install nginx -y
|
||||
print_status "Nginx installed successfully"
|
||||
else
|
||||
print_status "Nginx is already installed"
|
||||
fi
|
||||
|
||||
# Create nginx configuration for testing apps
|
||||
print_status "Creating Nginx configuration..."
|
||||
sudo tee /etc/nginx/sites-available/testing-apps > /dev/null <<EOF
|
||||
# Configuration for testing deployments
|
||||
# This allows access via pr-123.testing.yourdomain.com
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
server_name ~^pr-(?<pr_number>\d+)\.testing\.(.+)$;
|
||||
|
||||
# Health check endpoint
|
||||
location /health {
|
||||
set \$app_port 3000\$pr_number;
|
||||
proxy_pass http://127.0.0.1:\$app_port;
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
|
||||
# Add CORS headers for API calls
|
||||
add_header 'Access-Control-Allow-Origin' '*' always;
|
||||
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
|
||||
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization' always;
|
||||
}
|
||||
|
||||
# Main application
|
||||
location / {
|
||||
set \$app_port 3000\$pr_number;
|
||||
proxy_pass http://127.0.0.1:\$app_port;
|
||||
proxy_set_header Host \$host;
|
||||
proxy_set_header X-Real-IP \$remote_addr;
|
||||
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto \$scheme;
|
||||
|
||||
# WebSocket support
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade \$http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
|
||||
# Timeouts
|
||||
proxy_connect_timeout 60s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
}
|
||||
}
|
||||
|
||||
# Direct port access (fallback)
|
||||
server {
|
||||
listen 80 default_server;
|
||||
server_name _;
|
||||
|
||||
location / {
|
||||
return 200 'Testing Server is running. Use pr-{NUMBER}.testing.yourdomain.com to access deployments.';
|
||||
add_header Content-Type text/plain;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
# Enable nginx configuration
|
||||
if [ ! -L /etc/nginx/sites-enabled/testing-apps ]; then
|
||||
sudo ln -s /etc/nginx/sites-available/testing-apps /etc/nginx/sites-enabled/
|
||||
fi
|
||||
|
||||
# Remove default nginx site
|
||||
if [ -L /etc/nginx/sites-enabled/default ]; then
|
||||
sudo rm /etc/nginx/sites-enabled/default
|
||||
fi
|
||||
|
||||
# Test and reload nginx
|
||||
sudo nginx -t && sudo systemctl reload nginx
|
||||
print_status "Nginx configured successfully"
|
||||
|
||||
# Install useful tools
|
||||
print_status "Installing additional tools..."
|
||||
sudo apt install -y curl wget htop netstat-nat jq
|
||||
|
||||
# Create cleanup script
|
||||
print_status "Creating cleanup script..."
|
||||
sudo tee /usr/local/bin/cleanup-old-deployments > /dev/null <<'EOF'
|
||||
#!/bin/bash
|
||||
# Cleanup script for old testing deployments
|
||||
|
||||
DAYS_OLD=7
|
||||
DEPLOYMENT_DIR="/opt/low-code-engine"
|
||||
|
||||
echo "Cleaning up deployments older than ${DAYS_OLD} days..."
|
||||
|
||||
find ${DEPLOYMENT_DIR} -name "testing-pr-*" -type d -mtime +${DAYS_OLD} | while read dir; do
|
||||
echo "Cleaning up: $dir"
|
||||
|
||||
# Stop containers if running
|
||||
if [ -f "$dir/docker-compose.yml" ]; then
|
||||
cd "$dir"
|
||||
docker-compose down -v 2>/dev/null || true
|
||||
fi
|
||||
|
||||
# Remove directory
|
||||
rm -rf "$dir"
|
||||
echo "Removed: $dir"
|
||||
done
|
||||
|
||||
# Clean up unused Docker resources
|
||||
docker system prune -f
|
||||
docker image prune -f
|
||||
|
||||
echo "Cleanup completed"
|
||||
EOF
|
||||
|
||||
sudo chmod +x /usr/local/bin/cleanup-old-deployments
|
||||
|
||||
# Create cron job for cleanup
|
||||
print_status "Setting up automatic cleanup..."
|
||||
(crontab -l 2>/dev/null; echo "0 2 * * * /usr/local/bin/cleanup-old-deployments") | crontab -
|
||||
|
||||
# Create monitoring script
|
||||
print_status "Creating monitoring script..."
|
||||
sudo tee /usr/local/bin/monitor-deployments > /dev/null <<'EOF'
|
||||
#!/bin/bash
|
||||
# Monitoring script for testing deployments
|
||||
|
||||
DEPLOYMENT_DIR="/opt/low-code-engine"
|
||||
|
||||
echo "=== Testing Deployments Status ==="
|
||||
echo "Date: $(date)"
|
||||
echo
|
||||
|
||||
# Show active deployments
|
||||
echo "Active Deployments:"
|
||||
find ${DEPLOYMENT_DIR} -name "testing-pr-*" -type d | sort | while read dir; do
|
||||
pr_number=$(basename "$dir" | sed 's/testing-pr-//')
|
||||
if [ -f "$dir/docker-compose.yml" ]; then
|
||||
cd "$dir"
|
||||
status=$(docker-compose ps -q | wc -l)
|
||||
if [ "$status" -gt 0 ]; then
|
||||
app_port=$((3000 + pr_number))
|
||||
echo " PR #${pr_number}: Running on port ${app_port}"
|
||||
# Check if app is responding
|
||||
if curl -s --max-time 5 "http://localhost:${app_port}/health" > /dev/null 2>&1; then
|
||||
echo " Status: ✅ Healthy"
|
||||
else
|
||||
echo " Status: ❌ Unhealthy"
|
||||
fi
|
||||
else
|
||||
echo " PR #${pr_number}: Stopped"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
echo
|
||||
echo "=== System Resources ==="
|
||||
echo "Disk Usage:"
|
||||
df -h /opt/low-code-engine
|
||||
echo
|
||||
echo "Docker Usage:"
|
||||
docker system df
|
||||
echo
|
||||
echo "Memory Usage:"
|
||||
free -h
|
||||
EOF
|
||||
|
||||
sudo chmod +x /usr/local/bin/monitor-deployments
|
||||
|
||||
# Setup firewall (optional but recommended)
|
||||
print_status "Configuring firewall..."
|
||||
if command -v ufw &> /dev/null; then
|
||||
sudo ufw --force enable
|
||||
sudo ufw allow ssh
|
||||
sudo ufw allow 80/tcp
|
||||
sudo ufw allow 443/tcp
|
||||
# Allow port range for testing apps (3000-3999)
|
||||
sudo ufw allow 3000:3999/tcp
|
||||
# Allow port range for databases (3300-3999)
|
||||
sudo ufw allow 3300:3999/tcp
|
||||
# Allow port range for redis (6300-6999)
|
||||
sudo ufw allow 6300:6999/tcp
|
||||
print_status "Firewall configured"
|
||||
else
|
||||
print_warning "UFW not found, skipping firewall configuration"
|
||||
fi
|
||||
|
||||
print_status "Setup completed successfully!"
|
||||
echo
|
||||
echo "🎉 Your testing server is ready!"
|
||||
echo
|
||||
echo "Next steps:"
|
||||
echo "1. Add your public SSH key to /home/deploy/.ssh/authorized_keys"
|
||||
echo "2. Configure your GitHub repository secrets:"
|
||||
echo " - TESTING_SERVER_HOST: $(curl -s ifconfig.me 2>/dev/null || echo 'YOUR_SERVER_IP')"
|
||||
echo " - TESTING_SERVER_USER: deploy"
|
||||
echo " - TESTING_SERVER_SSH_KEY: (your private SSH key)"
|
||||
echo " - TESTING_DB_ROOT_PASSWORD: (choose a strong password)"
|
||||
echo " - TESTING_DB_USERNAME: app_user"
|
||||
echo " - TESTING_DB_PASSWORD: (choose a strong password)"
|
||||
echo
|
||||
echo "Useful commands:"
|
||||
echo " - Monitor deployments: sudo /usr/local/bin/monitor-deployments"
|
||||
echo " - Cleanup old deployments: sudo /usr/local/bin/cleanup-old-deployments"
|
||||
echo " - Check nginx status: sudo systemctl status nginx"
|
||||
echo " - View nginx logs: sudo journalctl -u nginx -f"
|
||||
echo
|
||||
print_warning "Please reboot the server or run 'newgrp docker' to apply Docker group changes"
|
||||
EOF
|
||||
Reference in New Issue
Block a user