-
Notifications
You must be signed in to change notification settings - Fork 0
440 lines (373 loc) Β· 15.2 KB
/
Copy pathdeploy.yml
File metadata and controls
440 lines (373 loc) Β· 15.2 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
name: Deploy
on:
push:
branches: [main]
pull_request:
types: [closed]
workflow_dispatch:
inputs:
dry_run:
description: 'Dry run β skip VPS changes, just run quality gates and notify'
required: false
default: 'false'
skip_tests:
description: 'Skip unit tests (emergency deploy only)'
required: false
default: 'false'
permissions:
contents: write
pull-requests: write
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
# ADMIN_IDS stored as a GitHub secret to avoid exposing Telegram user IDs in plaintext
ADMIN_IDS: ${{ secrets.ADMIN_IDS }}
AFFILIATE_SOURCE: GambleCodez
DISCORD_CODE_GENERATION_IMAGE_URL: "https://raw.githubusercontent.com/gamblecodezcom/Runewager/main/images/discord_code_generation.png"
DISCORD_VERIFY_IMAGE_URL: "https://raw.githubusercontent.com/gamblecodezcom/Runewager/main/images/discord_verify.png"
MAX_ANALYTICS_EVENTS: "2000"
MAX_ONBOARDING_STEPS_HISTORY: "500"
MINI_APP_CLAIM_URL: "https://t.me/RuneWager_bot/claim"
MINI_APP_PLAY_URL: "https://t.me/RuneWager_bot/Play"
MINI_APP_PROFILE_URL: "https://t.me/RuneWager_bot/profile"
PORT: "3000"
PROMO_AMOUNT_SC: "3.5"
PROMO_BONUS_RULE: "Bonus added once your account is verified and meets eligibility requirements. Limit once per account/IP. New users are defined as accounts that have never redeemed a promo code before."
PROMO_CLAIM_LIMIT: "600"
PROMO_CODE: SPORTS3.5
PROMO_ENTRY_IMAGE_URL: "https://raw.githubusercontent.com/gamblecodezcom/Runewager/main/images/promo_entry.png"
RW_DISCORD_JOIN: "https://discord.gg/runewagers"
RW_DISCORD_LINK: "https://discord.com/channels/1100486422395355197/1249181934811349052"
RW_DISCORD_SUPPORT: "https://discord.com/channels/1100486422395355197/1249182067296567338"
DEPLOY_MODE: normal
jobs:
# ---------------------------------------------------------------------------
# Job 1: Quality Gates β syntax, tests, audit, metadata, secrets, guards
# ---------------------------------------------------------------------------
quality-gates:
name: Quality Gates
runs-on: ubuntu-latest
# Run on: direct push to main, merged PR to main, or manual workflow_dispatch.
# Closed-without-merge PRs are excluded by the merged == true check.
if: >
github.event_name == 'push' ||
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' &&
github.event.pull_request.merged == true)
outputs:
commit_hash: ${{ steps.meta.outputs.commit_hash }}
deploy_time: ${{ steps.meta.outputs.deploy_time }}
version: ${{ steps.meta.outputs.version }}
started_at: ${{ steps.start.outputs.started_at }}
duration_seconds: ${{ steps.duration.outputs.duration_seconds }}
steps:
- name: Record start time
id: start
run: echo "started_at=$(date +%s)" >> "$GITHUB_OUTPUT"
- name: Guard β verify valid merge to main
if: github.event_name == 'pull_request'
run: |
echo "PR #${{ github.event.pull_request.number }}: ${{ github.event.pull_request.title }}"
echo "Merged into: ${{ github.event.pull_request.base.ref }}"
echo "Merge commit: ${{ github.event.pull_request.merge_commit_sha }}"
echo "β
Valid merge to main confirmed β proceeding with quality gates"
- uses: actions/checkout@v4
- name: Guard β skip if commit says so
run: |
MSG=$(git log -1 --format="%s %b")
if echo "$MSG" | grep -qi '\[skip deploy\]'; then
echo "Commit message contains [skip deploy] β aborting."
exit 1
fi
echo "β
No [skip deploy] flag found"
- name: Verify required secrets exist
env:
_BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
_SERVER_KEY: ${{ secrets.SERVER_KEY }}
_SERVER_HOST: ${{ secrets.SERVER_HOST }}
_SERVER_USER: ${{ secrets.SERVER_USER }}
run: |
MISSING=0
[ -z "$_BOT_TOKEN" ] && echo "β Missing secret: BOT_TOKEN" && MISSING=1
[ -z "$_SERVER_KEY" ] && echo "β Missing secret: SERVER_KEY" && MISSING=1
[ -z "$_SERVER_HOST" ]&& echo "β Missing secret: SERVER_HOST"&& MISSING=1
[ -z "$_SERVER_USER" ]&& echo "β Missing secret: SERVER_USER"&& MISSING=1
[ "$MISSING" -eq 1 ] && exit 1
echo "β
All required secrets present"
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Verify Node version is 20.x
run: |
echo "Node: $(node -v)"
echo "npm: $(npm -v)"
NODE_MAJOR=$(node -v | sed 's/v\([0-9]*\).*/\1/')
if [ "$NODE_MAJOR" -lt 20 ]; then
echo "ERROR: Node 20+ required, found $(node -v)" >&2
exit 1
fi
echo "β
Node $(node -v) OK"
- name: Install deps
run: npm ci
- name: Build metadata
id: meta
run: |
echo "commit_hash=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
echo "deploy_time=$(date -u '+%Y-%m-%dT%H:%M:%SZ')" >> "$GITHUB_OUTPUT"
echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
- name: Syntax check
run: npm run check
- name: Tests
if: github.event.inputs.skip_tests != 'true'
run: npm test
- name: Security audit (non-blocking)
run: npm audit --audit-level=high || true
- name: Compute duration
id: duration
run: |
STARTED="${{ steps.start.outputs.started_at }}"
NOW=$(date +%s)
echo "duration_seconds=$((NOW - STARTED))" >> "$GITHUB_OUTPUT"
- name: Notify deploy start
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
chmod +x scripts/notify-telegram.sh
./scripts/notify-telegram.sh "π *Deploy Starting*
Repo: \`${{ github.repository }}\`
Branch: \`${{ github.ref_name }}\`
Commit: \`${{ steps.meta.outputs.commit_hash }}\`
Version: \`${{ steps.meta.outputs.version }}\`
Mode: \`${DEPLOY_MODE}\`
By: \`${{ github.actor }}\`"
# ---------------------------------------------------------------------------
# Job 2: Deploy to VPS β pull latest main, restart service, health check
# Only triggers after quality gates pass on a valid PR merge to main.
# ---------------------------------------------------------------------------
deploy:
name: Deploy to VPS
runs-on: ubuntu-latest
needs: quality-gates
environment: Production
if: needs.quality-gates.result == 'success'
env:
DEPLOY_PASS: ${{ secrets.DEPLOY_PASS }}
steps:
- uses: actions/checkout@v4
- name: Install sshpass (password fallback)
run: sudo apt-get install -y sshpass
- name: Setup SSH
env:
SERVER_KEY: ${{ secrets.SERVER_KEY }}
SERVER_HOST: ${{ secrets.SERVER_HOST }}
run: |
mkdir -p ~/.ssh
chmod 700 ~/.ssh
printf '%s\n' "$SERVER_KEY" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -H "$SERVER_HOST" >> ~/.ssh/known_hosts
cat >> ~/.ssh/config <<EOF
Host deploy_target
HostName $SERVER_HOST
User ${{ secrets.SERVER_USER }}
IdentityFile ~/.ssh/deploy_key
StrictHostKeyChecking yes
ConnectTimeout 15
EOF
- name: Notify β deploy job started
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
chmod +x scripts/notify-telegram.sh
./scripts/notify-telegram.sh "π *Deploy Job Started*
Repo: \`${{ github.repository }}\`
Branch: \`${{ github.ref_name }}\`
Commit: \`${{ needs.quality-gates.outputs.commit_hash }}\`
Version: \`${{ needs.quality-gates.outputs.version }}\`
Actor: \`${{ github.actor }}\`"
- name: Save pre-deploy SHA
id: pre
run: |
SHA=$(ssh deploy_target "cd /var/www/html/Runewager && git rev-parse HEAD || echo none")
echo "sha=$SHA" >> "$GITHUB_OUTPUT"
- name: Pre-deploy health snapshot
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
ssh deploy_target "
set -e
cd /var/www/html/Runewager
echo 'Checking .env exists...'
if [ ! -f .env ]; then
echo 'ERROR: .env missing on VPS' >&2
exit 1
fi
echo 'Checking port 3000 usage...'
lsof -i :3000 || true
echo 'Checking memory/disk...'
free -m || true
df -h . || true
"
chmod +x scripts/notify-telegram.sh
./scripts/notify-telegram.sh "π *Pre-Deploy Health Snapshot*
Path: \`/var/www/html/Runewager\`
Commit (pre): \`${{ steps.pre.outputs.sha }}\`"
- name: Deploy via deploy.sh (key first, then password fallback)
env:
SERVER_HOST: ${{ secrets.SERVER_HOST }}
SERVER_USER: ${{ secrets.SERVER_USER }}
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
chmod +x scripts/notify-telegram.sh
DEPLOY_CMD='
set -e
cd /var/www/html/Runewager
echo "Pulling latest main..."
git fetch origin main
git reset --hard origin/main
echo "Ensuring .env exists and is preserved..."
if [ ! -f .env ]; then
echo "ERROR: .env missing on VPS" >&2
exit 1
fi
echo "Stopping systemd service (if running)..."
sudo systemctl stop runewager.service || true
echo "Killing any stray bot processes (scoped to this project only)..."
pkill -f "Runewager/index\.js" || true
echo "Installing production dependencies..."
npm ci --omit=dev
echo "Starting systemd service cleanly..."
sudo systemctl daemon-reload || true
sudo systemctl start runewager.service
echo "Checking systemd status..."
sudo systemctl is-active --quiet runewager.service
'
./scripts/notify-telegram.sh "π *Deploy Phase β SSH Key Attempt*
Host: \`${SERVER_HOST}\`"
if ssh deploy_target "$DEPLOY_CMD"; then
echo "β
Deployed via SSH key"
./scripts/notify-telegram.sh "β
*Deploy Succeeded via SSH Key*
Host: \`${SERVER_HOST}\`"
else
echo "β οΈ SSH key failed β waiting 120 seconds before password retry..."
./scripts/notify-telegram.sh "β οΈ *SSH Key Failed β Waiting 2 Minutes Before Password Fallback*
Host: \`${SERVER_HOST}\`"
sleep 120
if sshpass -p "$DEPLOY_PASS" ssh \
-o StrictHostKeyChecking=yes \
-o ConnectTimeout=15 \
-o UserKnownHostsFile=~/.ssh/known_hosts \
"${SERVER_USER}@${SERVER_HOST}" "$DEPLOY_CMD"; then
echo "β
Deployed via password fallback"
./scripts/notify-telegram.sh "β
*Deploy Succeeded via Password Fallback*
Host: \`${SERVER_HOST}\`"
else
echo "β Both SSH attempts failed"
./scripts/notify-telegram.sh "β *Deploy Failed β SSH Key + Password Fallback Both Failed*
Host: \`${SERVER_HOST}\`
Action required: manual SSH + service check."
exit 1
fi
fi
- name: Download deploy report from VPS
if: always()
run: |
ssh deploy_target "cat /tmp/deploy-report.txt 2>/dev/null || echo 'No deploy report file found.'" \
> /tmp/deploy-report.txt || echo 'No deploy report available.' > /tmp/deploy-report.txt
- name: Upload deploy report as artifact
if: always()
uses: actions/upload-artifact@v4
with:
name: deploy-summary
path: /tmp/deploy-report.txt
if-no-files-found: ignore
retention-days: 7
- name: Post-deploy health check (30s delay)
if: success()
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
chmod +x scripts/notify-telegram.sh
./scripts/notify-telegram.sh "β³ *Post-Deploy Health Check in 30s*"
sleep 30
HEALTH_STATUS="FAIL"
if ssh deploy_target "curl -fsS http://127.0.0.1:3000/health" >/dev/null 2>&1; then
HEALTH_STATUS="OK"
fi
./scripts/notify-telegram.sh "π‘ *Post-Deploy Health Check*
Status: \`${HEALTH_STATUS}\`
Commit: \`${{ needs.quality-gates.outputs.commit_hash }}\`"
- name: Rollback on failure
if: failure()
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
PRE="${{ steps.pre.outputs.sha }}"
chmod +x scripts/notify-telegram.sh
./scripts/notify-telegram.sh "β *Deploy Failed β Starting Rollback*
Previous SHA: \`${PRE}\`"
ssh deploy_target "
cd /var/www/html/Runewager
if [ \"$PRE\" != \"none\" ] && [ -n \"$PRE\" ]; then
git reset --hard \"$PRE\"
npm ci --omit=dev || true
sudo systemctl stop runewager.service || true
pkill -f \"Runewager/index\.js\" || true
sudo systemctl daemon-reload || true
sudo systemctl start runewager.service || true
else
echo 'No previous SHA recorded, skipping git rollback'
fi
"
./scripts/notify-telegram.sh "π *Rollback Complete*
Restored to: \`${PRE}\`"
- name: Final deploy report
if: always()
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
STATUS="${{ job.status }}"
COMMIT="${{ needs.quality-gates.outputs.commit_hash }}"
VERSION="${{ needs.quality-gates.outputs.version }}"
TIME="${{ needs.quality-gates.outputs.deploy_time }}"
REPORT_BODY=$(cat /tmp/deploy-report.txt 2>/dev/null || echo "No deploy report available.")
if [ "$STATUS" = "success" ]; then
MSG="π *Final Deploy Report*
Status: β
Success
Commit: \`$COMMIT\`
Version: \`$VERSION\`
Time: \`$TIME\`
$REPORT_BODY"
else
MSG="β οΈ *Final Deploy Report*
Status: β Failure
Commit: \`$COMMIT\`
Version: \`$VERSION\`
Time: \`$TIME\`
$REPORT_BODY"
fi
chmod +x scripts/notify-telegram.sh
./scripts/notify-telegram.sh "$MSG"
# ---------------------------------------------------------------------------
# Job 3: Dry-run report (only when dry_run=true)
# ---------------------------------------------------------------------------
dry-run-report:
name: Dry Run Report
runs-on: ubuntu-latest
needs: quality-gates
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.dry_run == 'true' }}
steps:
- uses: actions/checkout@v4
- name: Send dry-run notification
env:
BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
run: |
chmod +x scripts/notify-telegram.sh
./scripts/notify-telegram.sh "π§ͺ *Dry Run Only β No VPS Changes*
Repo: \`${{ github.repository }}\`
Branch: \`${{ github.ref_name }}\`
Commit: \`${{ needs.quality-gates.outputs.commit_hash }}\`
Version: \`${{ needs.quality-gates.outputs.version }}\`
Time: \`${{ needs.quality-gates.outputs.deploy_time }}\`
By: \`${{ github.actor }}\`
Quality gates passed. VPS was NOT modified."