Skip to content

Update ecosystem.config.js #85

Update ecosystem.config.js

Update ecosystem.config.js #85

Workflow file for this run

name: Twitch Bot CI/CD Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: self-hosted
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Cache pip packages
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Set up Python Virtual Environment and Install Dependencies
run: |
if [ ! -d "venv" ]; then
python3.11 -m venv venv
fi
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
deploy:
runs-on: self-hosted
needs: build
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: Check current user and environment
run: |
echo "Current user: $(whoami)"
echo "User ID: $(id -u)"
echo "Groups: $(groups)"
echo "Home directory: $HOME"
echo "Current directory: $(pwd)"
echo "PATH: $PATH"
- name: Check system dependencies
run: |
python3.11 --version
pip --version
node --version || echo "Node.js not found"
npm --version || echo "npm not found"
pm2 --version || echo "PM2 not found"
- name: Deploy to Raspberry Pi
env:
GOOGLE_CREDENTIALS_JSON: ${{ secrets.GOOGLE_CREDENTIALS_JSON }}
TWITCH_CLIENT_ID: ${{ secrets.TWITCH_CLIENT_ID }}
TWITCH_CLIENT_SECRET: ${{ secrets.TWITCH_CLIENT_SECRET }}
BOT_NICK: ${{ secrets.BOT_NICK }}
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
REFRESH_TOKEN: ${{ secrets.REFRESH_TOKEN }}
TWITCH_CHANNELS: ${{ secrets.TWITCH_CHANNELS }}
COMMAND_PREFIX: ${{ secrets.COMMAND_PREFIX }}
ADMIN_USERS: ${{ secrets.ADMIN_USERS }}
BOT_USER_ID: ${{ secrets.BOT_USER_ID }}
BROADCASTER_USER_ID: ${{ secrets.BROADCASTER_USER_ID }}
run: |
set -e
echo "Starting deployment process..."
REVUBOT_DIR=$HOME/revbot
REPO_URL=https://github.com/Revulate/revbot.git
BACKUP_DIR=$HOME/revbot_backup_$(date +%Y%m%d_%H%M%S)
# Debug: Print environment variables (be careful not to expose sensitive information)
echo "TWITCH_CLIENT_ID is set: $([[ -n $TWITCH_CLIENT_ID ]] && echo 'Yes' || echo 'No')"
echo "TWITCH_CLIENT_SECRET is set: $([[ -n $TWITCH_CLIENT_SECRET ]] && echo 'Yes' || echo 'No')"
echo "BOT_NICK is set: $([[ -n $BOT_NICK ]] && echo 'Yes' || echo 'No')"
echo "ACCESS_TOKEN is set: $([[ -n $ACCESS_TOKEN ]] && echo 'Yes' || echo 'No')"
echo "REFRESH_TOKEN is set: $([[ -n $REFRESH_TOKEN ]] && echo 'Yes' || echo 'No')"
echo "TWITCH_CHANNELS is set: $([[ -n $TWITCH_CHANNELS ]] && echo 'Yes' || echo 'No')"
echo "COMMAND_PREFIX is set: $([[ -n $COMMAND_PREFIX ]] && echo 'Yes' || echo 'No')"
echo "ADMIN_USERS is set: $([[ -n $ADMIN_USERS ]] && echo 'Yes' || echo 'No')"
echo "BOT_USER_ID is set: $([[ -n $BOT_USER_ID ]] && echo 'Yes' || echo 'No')"
echo "BROADCASTER_USER_ID is set: $([[ -n $BROADCASTER_USER_ID ]] && echo 'Yes' || echo 'No')"
# Backup current version
if [ -d "$REVUBOT_DIR" ]; then
echo "Backing up current version..."
cp -r $REVUBOT_DIR $BACKUP_DIR
else
echo "No existing directory to backup."
fi
echo "Creating/updating $REVUBOT_DIR..."
mkdir -p $REVUBOT_DIR
cd $REVUBOT_DIR
if [ -d ".git" ]; then
echo "Updating existing repository..."
git fetch origin
git checkout main
git reset --hard origin/main
else
echo "Initializing repository..."
git init
git remote add origin $REPO_URL
git fetch origin
git checkout -b main origin/main
fi
echo "Setting up Python environment..."
if [ ! -d "venv" ]; then
python3.11 -m venv venv
fi
source venv/bin/activate
pip install -r requirements.txt
echo "Setting up credentials..."
echo "$GOOGLE_CREDENTIALS_JSON" > credentials.json
chmod 600 credentials.json
echo "Creating .env file..."
cat << EOF > .env
TWITCH_CLIENT_ID=$TWITCH_CLIENT_ID
TWITCH_CLIENT_SECRET=$TWITCH_CLIENT_SECRET
BOT_NICK=$BOT_NICK
ACCESS_TOKEN=$ACCESS_TOKEN
REFRESH_TOKEN=$REFRESH_TOKEN
TWITCH_CHANNELS=$TWITCH_CHANNELS
COMMAND_PREFIX=$COMMAND_PREFIX
ADMIN_USERS=$ADMIN_USERS
BOT_USER_ID=$BOT_USER_ID
BROADCASTER_USER_ID=$BROADCASTER_USER_ID
EOF
echo "Setting up database..."
python -c 'from utils import setup_database; setup_database()'
echo "Creating PM2 ecosystem config..."
cat <<EOF > ecosystem.config.js
module.exports = {
apps: [{
name: "twitch-bot",
script: "bot.py",
interpreter: process.platform === "win32" ? "python" : "/home/actionsrunner/revbot/venv/bin/python",
interpreter_args: "-u", // Use unbuffered mode
watch: true,
ignore_watch: [
"node_modules",
"logs",
"*.log",
"*.db",
"*.db-journal",
".git"
],
instances: 1, // Ensure only one instance is started
exec_mode: "fork",
max_memory_restart: "1G",
env: {
NODE_ENV: "development",
PYTHONUNBUFFERED: "1",
PYTHONPATH: process.platform === "win32" ? "./env/Lib/site-packages" : "/home/actionsrunner/revbot/venv/lib/python3.11/site-packages"
},
env_production: {
NODE_ENV: "production",
PYTHONUNBUFFERED: "1",
PYTHONPATH: process.platform === "win32" ? "./env/Lib/site-packages" : "/home/actionsrunner/revbot/venv/lib/python3.11/site-packages"
},
env_file: process.platform === "win32" ? ".env" : "/home/actionsrunner/revbot/.env",
error_file: process.platform === "win32" ? "./logs/err.log" : "/home/actionsrunner/revbot/logs/err.log",
out_file: process.platform === "win32" ? "./logs/out.log" : "/home/actionsrunner/revbot/logs/out.log",
log_date_format: "YYYY-MM-DD HH:mm:ss Z",
restart_delay: 5000,
autorestart: true,
instance_var: 'INSTANCE_ID',
merge_logs: true,
kill_timeout: 5000,
shutdown_with_message: true,
wait_ready: true,
max_restarts: 10,
min_uptime: "1m",
listen_timeout: 8000,
}]
}
EOF
echo "Stopping any existing bot instances..."
pm2 stop twitch-bot || true
pm2 delete twitch-bot || true
echo "Starting bot with PM2..."
pm2 start ecosystem.config.js
echo "Saving PM2 process list..."
pm2 save
echo "Performing health check..."
sleep 30
INSTANCE_COUNT=$(pm2 list | grep -c "twitch-bot")
if [ "$INSTANCE_COUNT" -ne 1 ]; then
echo "Error: Unexpected number of twitch-bot instances running: $INSTANCE_COUNT"
echo "PM2 status:"
pm2 list
echo "PM2 logs:"
pm2 logs --lines 50
echo "Rolling back to previous version..."
if [ -d "$BACKUP_DIR" ]; then
pm2 stop twitch-bot || true
pm2 delete twitch-bot || true
rm -rf $REVUBOT_DIR
mv $BACKUP_DIR $REVUBOT_DIR
cd $REVUBOT_DIR
pm2 start ecosystem.config.js
echo "Rollback completed."
else
echo "No backup directory found. Cannot rollback."
fi
exit 1
fi
echo "Deployment completed successfully"
- name: Cleanup old backups
run: |
find $HOME/revbot_backup_* -maxdepth 0 -type d -mtime +7 -exec rm -rf {} +
- name: Notify on success
if: success()
run: echo "Deployment successful"
- name: Notify on failure
if: failure()
run: echo "Deployment failed"