#!/bin/bash # MEV Bot Auto-Update Script # Checks for updates on master branch and automatically pulls, rebuilds, and restarts set -e # Configuration BRANCH="${GIT_BRANCH:-master}" REMOTE="${GIT_REMOTE:-origin}" PROJECT_DIR="${PROJECT_DIR:-$(cd "$(dirname "$0")/.." && pwd)}" LOG_FILE="${PROJECT_DIR}/logs/auto-update.log" # Color codes GREEN='\033[0;32m' YELLOW='\033[1;33m' RED='\033[0;31m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Ensure log directory exists mkdir -p "${PROJECT_DIR}/logs" # Logging function log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" } log_color() { echo -e "$1" | tee -a "$LOG_FILE" } # Change to project directory cd "$PROJECT_DIR" log_color "${BLUE}=========================================" log "MEV Bot Auto-Update Check" log_color "${BLUE}=========================================${NC}" log "Project: $PROJECT_DIR" log "Branch: $BRANCH" log "Remote: $REMOTE" log "" # Fetch latest changes from remote log_color "${YELLOW}Fetching latest changes from $REMOTE...${NC}" if git fetch "$REMOTE" >> "$LOG_FILE" 2>&1; then log_color "${GREEN}✓ Fetch successful${NC}" else log_color "${RED}✗ Fetch failed${NC}" exit 1 fi # Get current and remote commit hashes LOCAL_COMMIT=$(git rev-parse HEAD) REMOTE_COMMIT=$(git rev-parse "$REMOTE/$BRANCH") log "Local commit: $LOCAL_COMMIT" log "Remote commit: $REMOTE_COMMIT" # Check if update is available if [ "$LOCAL_COMMIT" = "$REMOTE_COMMIT" ]; then log_color "${GREEN}✓ Already up to date${NC}" log "" exit 0 fi # Update available log_color "${YELLOW}⚠ Update available!${NC}" log "" # Show what's new log_color "${BLUE}New commits:${NC}" git log --oneline "$LOCAL_COMMIT..$REMOTE_COMMIT" | tee -a "$LOG_FILE" log "" # Check if we're on the correct branch CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) if [ "$CURRENT_BRANCH" != "$BRANCH" ]; then log_color "${RED}✗ Not on $BRANCH branch (currently on $CURRENT_BRANCH)${NC}" log "Skipping auto-update. Please switch to $BRANCH manually." exit 1 fi # Check for uncommitted changes if ! git diff-index --quiet HEAD --; then log_color "${RED}✗ Uncommitted changes detected${NC}" log "Skipping auto-update. Please commit or stash changes first." git status --short | tee -a "$LOG_FILE" exit 1 fi # Pull the changes log_color "${YELLOW}Pulling changes from $REMOTE/$BRANCH...${NC}" if git pull "$REMOTE" "$BRANCH" >> "$LOG_FILE" 2>&1; then log_color "${GREEN}✓ Pull successful${NC}" else log_color "${RED}✗ Pull failed${NC}" exit 1 fi # The post-merge hook will handle rebuild and restart log "" log_color "${YELLOW}Post-merge hook will handle rebuild and restart...${NC}" log "" # Wait for post-merge hook to complete sleep 2 # Verify container is running log_color "${YELLOW}Verifying container status...${NC}" if docker compose ps | grep -q "mev-bot.*running"; then log_color "${GREEN}✓ Container is running${NC}" else log_color "${RED}✗ Container is not running${NC}" docker compose ps | tee -a "$LOG_FILE" exit 1 fi # Show container status log "" log_color "${GREEN}=========================================" log "Update Complete!" log_color "${GREEN}=========================================${NC}" log "Updated from: $(echo $LOCAL_COMMIT | cut -c1-7)" log "Updated to: $(echo $REMOTE_COMMIT | cut -c1-7)" log "" # Send notification if configured if command -v curl &> /dev/null && [ -n "$WEBHOOK_URL" ]; then NEW_COMMITS=$(git log --oneline "$LOCAL_COMMIT..$REMOTE_COMMIT" | wc -l) MESSAGE="MEV Bot auto-updated: $NEW_COMMITS new commit(s) on $BRANCH" curl -X POST "$WEBHOOK_URL" \ -H "Content-Type: application/json" \ -d "{\"text\":\"$MESSAGE\"}" \ >> "$LOG_FILE" 2>&1 || true log "Notification sent to webhook" fi log "View logs: tail -f $LOG_FILE" log "View container logs: docker compose logs -f mev-bot" log "" exit 0