Update ci.yml #77
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
if [ ! -f "venv/.requirements_installed" ] || ! cmp -s requirements.txt venv/.requirements_installed; then | |
python -m pip install --upgrade pip | |
pip install -r requirements.txt | |
cp requirements.txt venv/.requirements_installed | |
fi | |
deploy: | |
runs-on: self-hosted | |
needs: build | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
steps: | |
- name: Check current user | |
run: | | |
echo "Current user: $(whoami)" | |
echo "User ID: $(id -u)" | |
echo "Groups: $(groups)" | |
- name: Set up environment | |
run: | | |
echo "Setting up environment..." | |
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH" | |
echo $PATH | |
# Add any other necessary environment variables here | |
- name: Debug output | |
run: | | |
echo "Debug: Checking sudo configuration" | |
sudo -n -l | |
echo "Debug: PM2 version" | |
pm2 -v || echo "PM2 not found" | |
echo "Debug: Node.js version" | |
node -v || echo "Node.js not found" | |
- name: Deploy to Raspberry Pi | |
env: | |
GOOGLE_CREDENTIALS_JSON: ${{ secrets.GOOGLE_CREDENTIALS_JSON }} | |
run: | | |
set -e | |
echo "Current user: $(whoami)" | |
echo "Home directory: $HOME" | |
echo "Current directory: $(pwd)" | |
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 | |
cp -r $REVUBOT_DIR $BACKUP_DIR | |
fi | |
mkdir -p $REVUBOT_DIR | |
cd $REVUBOT_DIR | |
if [ -d ".git" ]; then | |
git fetch origin | |
git checkout main | |
git reset --hard origin/main | |
else | |
git clone --branch main $REPO_URL . | |
fi | |
if [ ! -d "venv" ]; then | |
python3.11 -m venv venv | |
fi | |
source venv/bin/activate | |
pip install -r requirements.txt | |
# Ensure correct file permissions for credentials.json | |
echo "$GOOGLE_CREDENTIALS_JSON" > credentials.json | |
chmod 600 credentials.json | |
# Install Node.js if not present (assuming it's already installed) | |
node -v || echo "Node.js not found. Please install it manually." | |
# Install PM2 locally if not present | |
if ! command -v pm2 &> /dev/null; then | |
npm install pm2 | |
export PATH="$PATH:$HOME/revbot/node_modules/.bin" | |
fi | |
# Create or update 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 | |
# Set up the database to ensure all tables exist | |
python -c 'from utils import setup_database; setup_database()' | |
# Run database migrations if any | |
if [ -f "migrate.py" ]; then | |
python migrate.py | |
fi | |
# Start or restart the bot using PM2 | |
pm2 start ecosystem.config.js --update-env || pm2 delete twitch-bot && pm2 start ecosystem.config.js --update-env | |
pm2 save | |
# Health check | |
sleep 10 | |
if ! pm2 list | grep -q "online"; then | |
echo "Error: twitch-bot is not running after restart" | |
# Rollback | |
echo "Rolling back to previous version..." | |
rm -rf $REVUBOT_DIR | |
mv $BACKUP_DIR $REVUBOT_DIR | |
cd $REVUBOT_DIR | |
pm2 start ecosystem.config.js --update-env | |
exit 1 | |
fi | |
echo "Deployment completed successfully" |