156 lines
4.9 KiB
Bash
156 lines
4.9 KiB
Bash
#!/bin/bash
|
|
|
|
# Script for updating the Telegram Tinder Bot
|
|
# This script updates the code from Git, applies migrations, and prepares the bot for running
|
|
# Usage: ./bin/update.sh [branch] [--restart-service]
|
|
# If branch is not specified, 'main' is used
|
|
# Use --restart-service flag to restart PM2 service after update (for production deployments)
|
|
|
|
set -e # Exit immediately if a command exits with a non-zero status
|
|
|
|
# Define colors for pretty output
|
|
GREEN='\033[0;32m'
|
|
BLUE='\033[0;34m'
|
|
YELLOW='\033[0;33m'
|
|
RED='\033[0;31m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo -e "${BLUE}========================================${NC}"
|
|
echo -e "${BLUE} Telegram Tinder Bot Updater ${NC}"
|
|
echo -e "${BLUE}========================================${NC}"
|
|
|
|
# Parse command line arguments
|
|
BRANCH="main"
|
|
RESTART_SERVICE=false
|
|
|
|
for arg in "$@"; do
|
|
if [[ "$arg" == "--restart-service" ]]; then
|
|
RESTART_SERVICE=true
|
|
elif [[ "$arg" != --* ]]; then
|
|
BRANCH="$arg"
|
|
fi
|
|
done
|
|
|
|
echo -e "${YELLOW}Updating from branch: ${BRANCH}${NC}"
|
|
if [ "$RESTART_SERVICE" = true ]; then
|
|
echo -e "${YELLOW}Will restart service after update${NC}"
|
|
fi
|
|
|
|
# Store the current directory
|
|
CURRENT_DIR=$(pwd)
|
|
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
|
|
PROJECT_DIR=$(dirname "$SCRIPT_DIR")
|
|
|
|
# Check if running on Ubuntu
|
|
IS_UBUNTU=false
|
|
if [ -f /etc/os-release ]; then
|
|
source /etc/os-release
|
|
if [[ "$ID" == "ubuntu" ]]; then
|
|
IS_UBUNTU=true
|
|
echo -e "${BLUE}Detected Ubuntu: ${VERSION_ID}${NC}"
|
|
fi
|
|
fi
|
|
|
|
# Navigate to the project directory
|
|
cd "$PROJECT_DIR"
|
|
echo -e "${BLUE}Working directory: ${PROJECT_DIR}${NC}"
|
|
|
|
# Check if we're in a git repository
|
|
if [ ! -d ".git" ]; then
|
|
echo -e "${RED}Error: Not a git repository${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
echo -e "\n${BLUE}Step 1: Pulling latest changes from Git repository...${NC}"
|
|
# Save any local changes
|
|
git stash save "Auto-stash before update: $(date)"
|
|
|
|
# Fetch all branches
|
|
git fetch --all
|
|
|
|
# Check if the branch exists
|
|
if ! git rev-parse --verify "$BRANCH" &>/dev/null && ! git rev-parse --verify "origin/$BRANCH" &>/dev/null; then
|
|
echo -e "${RED}Error: Branch '$BRANCH' does not exist locally or remotely${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Checkout the specified branch
|
|
git checkout "$BRANCH"
|
|
|
|
# Pull the latest changes
|
|
git pull origin "$BRANCH"
|
|
echo -e "${GREEN}✓ Successfully pulled latest changes${NC}"
|
|
|
|
echo -e "\n${BLUE}Step 2: Installing dependencies...${NC}"
|
|
npm ci
|
|
echo -e "${GREEN}✓ Dependencies installed${NC}"
|
|
|
|
echo -e "\n${BLUE}Step 3: Running database migrations...${NC}"
|
|
# Check if migrations directory exists
|
|
if [ -d "./migrations" ]; then
|
|
echo "Applying database migrations..."
|
|
npm run migrate:up
|
|
echo -e "${GREEN}✓ Migrations applied successfully${NC}"
|
|
else
|
|
echo -e "${YELLOW}⚠ No migrations directory found, running database initialization script...${NC}"
|
|
npm run init:db
|
|
echo -e "${GREEN}✓ Database initialized${NC}"
|
|
fi
|
|
|
|
echo -e "\n${BLUE}Step 4: Building the project...${NC}"
|
|
npm run build
|
|
echo -e "${GREEN}✓ Project built successfully${NC}"
|
|
|
|
echo -e "\n${BLUE}Step 5: Checking for .env file...${NC}"
|
|
if [ -f .env ]; then
|
|
echo -e "${GREEN}✓ .env file exists${NC}"
|
|
else
|
|
echo -e "${YELLOW}⚠ .env file not found${NC}"
|
|
if [ -f .env.example ]; then
|
|
echo "Creating .env from .env.example"
|
|
cp .env.example .env
|
|
echo -e "${YELLOW}⚠ Please update the .env file with your configuration!${NC}"
|
|
else
|
|
echo -e "${RED}Error: .env.example file not found${NC}"
|
|
exit 1
|
|
fi
|
|
fi
|
|
|
|
echo -e "\n${BLUE}Step 6: Checking for services...${NC}"
|
|
# Check if Docker is being used
|
|
if [ -f docker-compose.yml ]; then
|
|
echo "Docker Compose configuration found"
|
|
echo "You might want to restart containers with: docker-compose down && docker-compose up -d"
|
|
fi
|
|
|
|
# Check for PM2 process on Ubuntu
|
|
if [ "$IS_UBUNTU" = true ] && command -v pm2 &>/dev/null; then
|
|
echo -e "\n${BLUE}Step 7: Checking PM2 service...${NC}"
|
|
if pm2 list | grep -q "tg_tinder_bot"; then
|
|
echo "PM2 service for tg_tinder_bot found"
|
|
if [ "$RESTART_SERVICE" = true ]; then
|
|
echo "Restarting PM2 service..."
|
|
pm2 restart tg_tinder_bot
|
|
echo -e "${GREEN}✓ PM2 service restarted${NC}"
|
|
else
|
|
echo "To restart the service, run: ${YELLOW}pm2 restart tg_tinder_bot${NC}"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
echo -e "\n${GREEN}========================================${NC}"
|
|
echo -e "${GREEN} Update completed successfully! ${NC}"
|
|
echo -e "${GREEN}========================================${NC}"
|
|
|
|
if [ "$IS_UBUNTU" = true ] && command -v pm2 &>/dev/null; then
|
|
echo -e "To start the bot with PM2, run: ${YELLOW}pm2 start dist/bot.js --name tg_tinder_bot${NC}"
|
|
echo -e "To restart the bot, run: ${YELLOW}pm2 restart tg_tinder_bot${NC}"
|
|
echo -e "To view logs, run: ${YELLOW}pm2 logs tg_tinder_bot${NC}"
|
|
else
|
|
echo -e "To start the bot, run: ${YELLOW}npm run start${NC}"
|
|
echo -e "For development mode: ${YELLOW}npm run dev${NC}"
|
|
fi
|
|
|
|
# Return to the original directory
|
|
cd "$CURRENT_DIR"
|