Skip to content

Update deployment script to reset local branch to origin/main #31

Update deployment script to reset local branch to origin/main

Update deployment script to reset local branch to origin/main #31

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 # Runs directly on your Raspberry Pi
steps:
# 1. Checkout Repository
- name: Checkout Repository
uses: actions/checkout@v3
# 2. Create and Activate Virtual Environment, then Install Dependencies
- name: Set up Python Virtual Environment and Install Dependencies
run: |
# Create a virtual environment in the revbot directory if it doesn't exist
if [ ! -d "venv" ]; then
python3.11 -m venv venv # Adjust to python3.10 if necessary
fi
# Activate the virtual environment
source venv/bin/activate
# Upgrade pip inside the virtual environment
python -m pip install --upgrade pip
# Install the required dependencies inside the virtual environment
pip install -r requirements.txt
# 3. Run Black Formatter Check
- name: Run Black Formatter Check
run: |
# Activate the virtual environment
source venv/bin/activate
# Run Black with the necessary options
python -m black --check --diff --line-length 120 .
deploy:
runs-on: self-hosted # Use the self-hosted runner for deployment too
needs: build
steps:
- name: Deploy to Raspberry Pi
env:
GOOGLE_CREDENTIALS_JSON: ${{ secrets.GOOGLE_CREDENTIALS_JSON }}
run: |
set -e # Exit immediately if a command exits with a non-zero status
REVUBOT_DIR=~/revbot
REPO_URL=https://github.com/Revulate/revbot.git
# Create revbot directory if it doesn't exist
mkdir -p $REVUBOT_DIR
cd $REVUBOT_DIR
# If .git exists, pull; else, clone the repository
if [ -d ".git" ]; then
git fetch origin
git checkout main
git reset --hard origin/main
else
git clone --branch main $REPO_URL .
fi
# Use the existing virtual environment or create one if necessary
if [ ! -d "venv" ]; then
python3.11 -m venv venv # Adjust to python3.10 if necessary
fi
# Activate the virtual environment
source venv/bin/activate
# Install dependencies within the virtual environment
pip install -r requirements.txt
# Create or update the .env file
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
# Create the credentials.json file from the secret
echo "$GOOGLE_CREDENTIALS_JSON" > credentials.json
# Set appropriate permissions for credentials.json
chmod 600 credentials.json
# Restart the Twitch bot service
sudo systemctl restart twitch-bot.service
# Add error logging for the restart command
if ! sudo systemctl restart twitch-bot.service; then
echo "Error: Failed to restart twitch-bot service"
echo "Service Status:"
sudo systemctl status twitch-bot.service
echo "Sudo Version:"
sudo --version
echo "Current User:"
whoami
echo "Sudo Permissions:"
sudo -l
exit 1
fi
echo "Deployment completed successfully"