|
| 1 | +#!/bin/bash |
| 2 | +# DEXBot2 Auto Update Script (Development Branch) |
| 3 | +# |
| 4 | +# This script automatically updates DEXBot2 from the GitHub repository dev branch |
| 5 | +# Usage: ./scripts/update-dev.sh or bash scripts/update-dev.sh |
| 6 | +# |
| 7 | +# Features: |
| 8 | +# - Checks for updates from dev branch |
| 9 | +# - Performs git pull |
| 10 | +# - Installs/updates dependencies |
| 11 | +# - Restarts PM2 if running |
| 12 | +# - Logs all operations |
| 13 | + |
| 14 | +set -e |
| 15 | + |
| 16 | +# Colors for output |
| 17 | +RED='\033[0;31m' |
| 18 | +GREEN='\033[0;32m' |
| 19 | +YELLOW='\033[1;33m' |
| 20 | +BLUE='\033[0;34m' |
| 21 | +NC='\033[0m' # No Color |
| 22 | + |
| 23 | +# Configuration |
| 24 | +REPO_URL="https://github.com/froooze/DEXBot2.git" |
| 25 | +REPO_BRANCH="dev" |
| 26 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 27 | +PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" |
| 28 | +LOG_FILE="${PROJECT_ROOT}/update-dev.log" |
| 29 | +TIMESTAMP=$(date '+%Y-%m-%d_%H-%M-%S') |
| 30 | + |
| 31 | +# Functions |
| 32 | +log_info() { |
| 33 | + echo -e "${BLUE}[INFO]${NC} $1" | tee -a "$LOG_FILE" |
| 34 | +} |
| 35 | + |
| 36 | +log_success() { |
| 37 | + echo -e "${GREEN}[SUCCESS]${NC} $1" | tee -a "$LOG_FILE" |
| 38 | +} |
| 39 | + |
| 40 | +log_warning() { |
| 41 | + echo -e "${YELLOW}[WARNING]${NC} $1" | tee -a "$LOG_FILE" |
| 42 | +} |
| 43 | + |
| 44 | +log_error() { |
| 45 | + echo -e "${RED}[ERROR]${NC} $1" | tee -a "$LOG_FILE" |
| 46 | +} |
| 47 | + |
| 48 | +# Check if we're in the project root |
| 49 | +if [ ! -f "$PROJECT_ROOT/dexbot.js" ]; then |
| 50 | + log_error "Script must be run from DEXBot2 root directory" |
| 51 | + exit 1 |
| 52 | +fi |
| 53 | + |
| 54 | +log_info "==========================================" |
| 55 | +log_info "DEXBot2 Update Script Started (Dev Branch)" |
| 56 | +log_info "==========================================" |
| 57 | +log_info "Project Root: $PROJECT_ROOT" |
| 58 | +log_info "Branch: $REPO_BRANCH" |
| 59 | +log_info "Timestamp: $TIMESTAMP" |
| 60 | + |
| 61 | +# Note: profiles/ directory is in .gitignore and is not backed up during updates |
| 62 | +# Your configuration files are safe and will not be affected by the update |
| 63 | + |
| 64 | +# Step 1: Check git status |
| 65 | +log_info "Step 1: Checking git status..." |
| 66 | +cd "$PROJECT_ROOT" |
| 67 | +if ! git rev-parse --git-dir > /dev/null 2>&1; then |
| 68 | + log_error "Not a git repository" |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) |
| 73 | +log_info "Current branch: $CURRENT_BRANCH" |
| 74 | + |
| 75 | +# Step 2: Protect profiles directory (ensure it won't be touched) |
| 76 | +log_info "Step 2: Protecting profiles directory..." |
| 77 | +if [ -d "$PROJECT_ROOT/profiles" ]; then |
| 78 | + # Ensure profiles is properly ignored by git |
| 79 | + if ! grep -q "profiles/" "$PROJECT_ROOT/.gitignore" 2>/dev/null; then |
| 80 | + log_warning "Warning: profiles/ not in .gitignore, adding it..." |
| 81 | + echo "profiles/" >> "$PROJECT_ROOT/.gitignore" |
| 82 | + fi |
| 83 | + log_success "Profiles directory is protected and will not be modified" |
| 84 | +else |
| 85 | + log_info "No profiles directory found (will be created on first run)" |
| 86 | +fi |
| 87 | + |
| 88 | +# Step 3: Stash any local changes (except profiles which is in .gitignore) |
| 89 | +log_info "Step 3: Checking for local changes..." |
| 90 | +if ! git diff --quiet; then |
| 91 | + log_warning "Uncommitted changes detected, stashing..." |
| 92 | + git stash |
| 93 | + log_info "Changes stashed" |
| 94 | +fi |
| 95 | + |
| 96 | +# Step 4: Fetch latest from GitHub |
| 97 | +log_info "Step 4: Fetching latest from GitHub..." |
| 98 | +if git fetch origin "$REPO_BRANCH"; then |
| 99 | + log_success "Fetched latest from origin" |
| 100 | +else |
| 101 | + log_error "Failed to fetch from GitHub" |
| 102 | + exit 1 |
| 103 | +fi |
| 104 | + |
| 105 | +# Step 5: Check if there are updates |
| 106 | +log_info "Step 5: Checking for available updates..." |
| 107 | +LOCAL=$(git rev-parse HEAD) |
| 108 | +REMOTE=$(git rev-parse "origin/$REPO_BRANCH") |
| 109 | + |
| 110 | +if [ "$LOCAL" = "$REMOTE" ]; then |
| 111 | + log_success "Already up to date!" |
| 112 | + log_info "==========================================" |
| 113 | + exit 0 |
| 114 | +fi |
| 115 | + |
| 116 | +log_info "Updates available, pulling changes..." |
| 117 | + |
| 118 | +# Step 6: Pull latest code (use --rebase to avoid merge prompts) |
| 119 | +if git pull --rebase origin "$REPO_BRANCH"; then |
| 120 | + log_success "Successfully pulled latest code" |
| 121 | +else |
| 122 | + log_error "Failed to pull latest code" |
| 123 | + exit 1 |
| 124 | +fi |
| 125 | + |
| 126 | +# Step 7: Install/update dependencies |
| 127 | +log_info "Step 7: Installing/updating dependencies..." |
| 128 | +if npm install --prefer-offline; then |
| 129 | + log_success "Dependencies installed successfully" |
| 130 | +else |
| 131 | + log_warning "npm install completed with warnings" |
| 132 | +fi |
| 133 | + |
| 134 | +# Step 8: Check for PM2 running |
| 135 | +log_info "Step 8: Checking PM2 status..." |
| 136 | +if command -v pm2 &> /dev/null; then |
| 137 | + if pm2 list | grep -q "bbot"; then |
| 138 | + log_info "PM2 bots detected, reloading..." |
| 139 | + if pm2 reload profiles/ecosystem.config.js 2>/dev/null; then |
| 140 | + log_success "PM2 bots reloaded successfully" |
| 141 | + else |
| 142 | + log_warning "PM2 reload failed or no ecosystem config found" |
| 143 | + fi |
| 144 | + else |
| 145 | + log_info "No running PM2 bots found" |
| 146 | + fi |
| 147 | +else |
| 148 | + log_info "PM2 not installed, skipping restart" |
| 149 | +fi |
| 150 | + |
| 151 | +# Step 9: Summary |
| 152 | +log_info "==========================================" |
| 153 | +log_success "Update completed successfully!" |
| 154 | +log_info "==========================================" |
| 155 | +log_info "" |
| 156 | +log_info "Summary:" |
| 157 | +log_info "- Code updated to latest from dev branch" |
| 158 | +log_info "- Dependencies installed" |
| 159 | +if command -v pm2 &> /dev/null && pm2 list | grep -q "bbot"; then |
| 160 | + log_info "- PM2 processes reloaded" |
| 161 | +fi |
| 162 | +log_info "- Your profiles/ directory is safe and unchanged" |
| 163 | +log_info "" |
| 164 | +log_info "Log file: $LOG_FILE" |
| 165 | +log_info "" |
| 166 | +log_info "Next steps:" |
| 167 | +log_info "- Review the changes with: git log --oneline -n 5" |
| 168 | +log_info "- Check status with: pm2 status" |
| 169 | +log_info "- View logs with: pm2 logs" |
| 170 | +log_info "" |
| 171 | + |
| 172 | +exit 0 |
0 commit comments