-
Notifications
You must be signed in to change notification settings - Fork 1
153 lines (132 loc) · 5.51 KB
/
deploy-develop-selfhost.yml
File metadata and controls
153 lines (132 loc) · 5.51 KB
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
name: Deploy AlyaBot to Self-Hosted Server (Development)
on:
push:
branches:
- development
jobs:
deploy:
runs-on: self-hosted
steps:
- name: Checkout latest code
uses: actions/checkout@v4
- name: Setup Python environment
run: |
VENV_DIR="/opt/dev-Alya-Bot-Telegram/venv"
if [ -d "$VENV_DIR" ]; then
VER=$($VENV_DIR/bin/python --version 2>&1)
if [[ "$VER" != *"3.10"* ]]; then
echo "Version mismatch ($VER). Rebuilding venv for Python 3.10..."
sudo rm -rf "$VENV_DIR"
fi
fi
if [ ! -d "$VENV_DIR" ]; then
python3.10 -m venv "$VENV_DIR"
fi
- name: Prepare and update codebase
run: |
sudo mkdir -p /opt/dev-Alya-Bot-Telegram
sudo chown $USER:$USER /opt/dev-Alya-Bot-Telegram
cd /opt/dev-Alya-Bot-Telegram
if [ ! -d ".git" ]; then
git init
git remote add origin https://github.com/Afdaan/Alya-Bot-Telegram.git
fi
git stash --include-untracked || true
git fetch origin
git checkout development || git checkout -b development origin/development
git pull origin development
- name: Install Python dependencies
run: |
cd /opt/dev-Alya-Bot-Telegram
source venv/bin/activate
python -m pip install "pip<24.1"
pip uninstall -y omegaconf || true
pip install -r requirements.txt
echo "Dependencies installed successfully"
- name: Restart AlyaBot via systemd
run: |
sudo systemctl restart dev-alya-bot.service
echo "AlyaBot restarted via systemd (dev-alya-bot.service)"
- name: Verify AlyaBot systemd service
run: |
sleep 5
sudo systemctl status dev-alya-bot.service --no-pager
if sudo systemctl is-active --quiet dev-alya-bot.service; then
echo "✅ AlyaBot is running successfully via systemd (dev-alya-bot.service)"
else
echo "❌ AlyaBot failed to start via systemd (dev-alya-bot.service)"
exit 1
fi
- name: Notify deployment success via Telegram
if: success()
run: |
cd /opt/dev-Alya-Bot-Telegram
# Load credentials dari .env (no echo untuk security)
if [ -f .env ]; then
set -a
source .env
set +a
fi
# Validate credentials tersedia
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$ADMIN_IDS" ]; then
exit 0
fi
# Get git information
GIT_INFO=$(git log -1 --pretty=format:"%s|%h|%an|%ar")
LAST_COMMIT_MSG=$(echo "$GIT_INFO" | cut -d'|' -f1 | sed 's/&/\&/g;s/</\</g;s/>/\>/g')
LAST_COMMIT_HASH=$(echo "$GIT_INFO" | cut -d'|' -f2)
LAST_COMMIT_AUTHOR=$(echo "$GIT_INFO" | cut -d'|' -f3 | sed 's/&/\&/g;s/</\</g;s/>/\>/g')
LAST_COMMIT_TIME=$(echo "$GIT_INFO" | cut -d'|' -f4)
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD | sed 's/&/\&/g;s/</\</g;s/>/\>/g')
# Prepare message
TELEGRAM_API="https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage"
MSG="🚀 <b>AlyaBot Deploy Sukses (Development)!</b> 🚀%0A%0A"
MSG="${MSG}📦 <b>Branch:</b> <code>$BRANCH_NAME</code>%0A"
MSG="${MSG}🔗 <b>Commit:</b> <code>$LAST_COMMIT_HASH</code>%0A"
MSG="${MSG}👤 <b>Author:</b> $LAST_COMMIT_AUTHOR%0A"
MSG="${MSG}⏰ <b>Time:</b> $LAST_COMMIT_TIME%0A%0A"
MSG="${MSG}📝 <b>Changelog:</b>%0A$LAST_COMMIT_MSG%0A%0A"
MSG="${MSG}✨ Bot is now running via systemd: <code>dev-alya-bot.service</code>"
# Send ke setiap admin (hide all output)
set +x
IFS=',' read -ra ADMIN_ARRAY <<< "$ADMIN_IDS"
for ADMIN_ID in "${ADMIN_ARRAY[@]}"; do
curl -s -X POST "$TELEGRAM_API" \
-d chat_id="$ADMIN_ID" \
-d text="$MSG" \
-d parse_mode="HTML" > /dev/null 2>&1
done
set -x
- name: Notify deployment failure via Telegram
if: failure()
run: |
cd /opt/dev-Alya-Bot-Telegram || cd /opt/dev-Alya-Bot-Telegram
# Load credentials dari .env (no echo)
if [ -f .env ]; then
set -a
source .env
set +a
fi
# Validate credentials
if [ -z "$TELEGRAM_BOT_TOKEN" ] || [ -z "$ADMIN_IDS" ]; then
exit 0
fi
# Get basic info
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
COMMIT_HASH=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
# Prepare failure message
TELEGRAM_API="https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage"
MSG="❌ <b>AlyaBot Deploy GAGAL (Development)!</b> ❌%0A%0A"
MSG="${MSG}📦 <b>Branch:</b> <code>$BRANCH_NAME</code>%0A"
MSG="${MSG}🔗 <b>Commit:</b> <code>$COMMIT_HASH</code>%0A%0A"
MSG="${MSG}🔍 Cek GitHub Actions logs untuk detail error."
# Send ke setiap admin (hide all output)
set +x
IFS=',' read -ra ADMIN_ARRAY <<< "$ADMIN_IDS"
for ADMIN_ID in "${ADMIN_ARRAY[@]}"; do
curl -s -X POST "$TELEGRAM_API" \
-d chat_id="$ADMIN_ID" \
-d text="$MSG" \
-d parse_mode="HTML" > /dev/null 2>&1
done
set -x