modified: cogs/afk.py #63
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 # Runs directly on your Raspberry Pi | |
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" ] && [ ! -d "env" ]; then | |
python3.11 -m venv venv | |
fi | |
if [ -d "venv" ]; then | |
source venv/bin/activate | |
elif [ -d "env" ]; then | |
source env/bin/activate | |
fi | |
python -m 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: Deploy to Raspberry Pi | |
env: | |
GOOGLE_CREDENTIALS_JSON: ${{ secrets.GOOGLE_CREDENTIALS_JSON }} | |
run: | | |
set -e | |
REVUBOT_DIR=~/revbot | |
REPO_URL=https://github.com/Revulate/revbot.git | |
BACKUP_DIR=~/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" ] && [ ! -d "env" ]; then | |
python3.11 -m venv venv | |
fi | |
if [ -d "venv" ]; then | |
source venv/bin/activate | |
elif [ -d "env" ]; then | |
source env/bin/activate | |
fi | |
pip install -r requirements.txt | |
# Create .env file (using existing secrets) | |
cat <<EOF > .env | |
TWITCH_CLIENT_ID=${{ secrets.TWITCH_CLIENT_ID }} | |
TWITCH_CLIENT_SECRET=${{ secrets.TWITCH_CLIENT_SECRET }} | |
ACCESS_TOKEN=${{ secrets.ACCESS_TOKEN }} | |
REFRESH_TOKEN=${{ secrets.REFRESH_TOKEN }} | |
BOT_NICK=${{ secrets.BOT_NICK }} | |
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 }} | |
LOG_LEVEL=${{ secrets.LOG_LEVEL }} | |
LOG_FILE=${{ secrets.LOG_FILE }} | |
LOG_MAX_BYTES=${{ secrets.LOG_MAX_BYTES }} | |
LOG_BACKUP_COUNT=${{ secrets.LOG_BACKUP_COUNT }} | |
API_STEAM_KEY=${{ secrets.API_STEAM_KEY }} | |
LOGDNA_INGESTION_KEY=${{ secrets.LOGDNA_INGESTION_KEY }} | |
OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }} | |
WEATHER_API_KEY=${{ secrets.WEATHER_API_KEY }} | |
NUULS_API_KEY=${{ secrets.NUULS_API_KEY }} | |
GOOGLE_SHEET_ID=${{ secrets.GOOGLE_SHEET_ID }} | |
GOOGLE_CREDENTIALS_FILE=credentials.json | |
EOF | |
echo "$GOOGLE_CREDENTIALS_JSON" > credentials.json | |
chmod 600 credentials.json | |
if ! command -v node &> /dev/null; then | |
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - | |
sudo apt-get install -y nodejs | |
fi | |
if ! command -v pm2 &> /dev/null; then | |
sudo npm install -g pm2 | |
fi | |
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 | |
# Run database migrations if any | |
if [ -f "migrate.py" ]; then | |
python migrate.py | |
fi | |
pm2 start ecosystem.config.js --update-env | |
pm2 save | |
sudo pm2 startup systemd -u $USER --hp $HOME | |
# 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" | |
- name: Cleanup old backups | |
run: | | |
find ~/revbot_backup_* -maxdepth 0 -type d -mtime +7 -exec rm -rf {} + | |
- name: Notify on success | |
if: success() | |
run: echo "Deployment successful" # Replace with your notification method | |
- name: Notify on failure | |
if: failure() | |
run: echo "Deployment failed" # Replace with your notification method |