Skip to content

Update ci.yml

Update ci.yml #79

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
npm --version
pm2 --version || echo "PM2 not found"
- name: Deploy to Raspberry Pi
env:
GOOGLE_CREDENTIALS_JSON: ${{ secrets.GOOGLE_CREDENTIALS_JSON }}
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)
# 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 "Checking Node.js and PM2..."
node -v || echo "Node.js not found. Please install it manually."
npm install pm2
export PATH="$PATH:$HOME/revbot/node_modules/.bin"
echo "Creating PM2 ecosystem config..."
cat <<EOF > ecosystem.config.js
module.exports = {
apps: [{
name: "twitch-bot",
script: "bot.py",
interpreter: "$(which python)",
watch: true,
ignore_watch: ["node_modules", "logs"],
max_memory_restart: "1G",
env: {
NODE_ENV: "development",
PYTHONUNBUFFERED: "1"
},
env_production: {
NODE_ENV: "production",
PYTHONUNBUFFERED: "1"
}
}]
}
EOF
echo "Setting up database..."
python -c 'from utils import setup_database; setup_database()'
if [ -f "migrate.py" ]; then
echo "Running database migrations..."
python migrate.py
fi
echo "Starting/restarting bot with PM2..."
pm2 describe twitch-bot > /dev/null
if [ $? -eq 0 ]; then
pm2 restart twitch-bot
else
pm2 start ecosystem.config.js
fi
echo "Saving PM2 process list..."
pm2 save
echo "Performing health check..."
sleep 10
if ! pm2 list | grep -q "online"; then
echo "Error: twitch-bot is not running after restart"
echo "PM2 status:"
pm2 list
echo "PM2 logs:"
pm2 logs --lines 50
echo "Rolling back to previous version..."
if [ -d "$BACKUP_DIR" ]; then
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"