-
Notifications
You must be signed in to change notification settings - Fork 0
163 lines (138 loc) · 5.4 KB
/
ci.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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 if it doesn't exist
if [ ! -d "venv" ] && [ ! -d "env" ]; then
python3.11 -m venv venv # Adjust to python3.10 if necessary
fi
# Activate the virtual environment
if [ -d "venv" ]; then
source venv/bin/activate
elif [ -d "env" ]; then
source env/bin/activate
fi
# 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
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" ] && [ ! -d "env" ]; then
python3.11 -m venv venv # Adjust to python3.10 if necessary
fi
# Activate the virtual environment
if [ -d "venv" ]; then
source venv/bin/activate
elif [ -d "env" ]; then
source env/bin/activate
fi
# 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
# Install Node.js and npm if not already installed
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
# Install PM2 globally if not already installed
if ! command -v pm2 &> /dev/null; then
sudo npm install -g pm2
fi
# Create ecosystem.config.js file
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
# Start or restart the bot using PM2
pm2 start ecosystem.config.js --update-env
# Save PM2 process list and environment
pm2 save
# Ensure PM2 starts on system boot
sudo pm2 startup systemd -u $USER --hp $HOME
# Add a simple health check
sleep 10 # Give the bot some time to start up
if ! pm2 list | grep -q "online"; then
echo "Error: twitch-bot is not running after restart"
exit 1
fi
echo "Deployment completed successfully"