-
Notifications
You must be signed in to change notification settings - Fork 3
126 lines (105 loc) · 3.53 KB
/
Copy pathdeploy.yml
File metadata and controls
126 lines (105 loc) · 3.53 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
name: Deploy
on:
push:
branches: [main]
workflow_dispatch:
inputs:
ref:
description: 'Git ref (commit SHA or tag) to deploy. Leave blank for latest main.'
required: false
default: ''
concurrency:
group: deploy-${{ github.ref }}
cancel-in-progress: false
env:
MAX_BACKUPS: 4
DIR: /home/${{ secrets.VPS_USER }}/webdev-bot-deploy
jobs:
lint:
name: Lint & Format Check
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup pnpm
uses: pnpm/action-setup@v4
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Prisma generate (sanity check schema)
run: pnpm run prisma:generate
env:
DATABASE_URL: file:/tmp/ci.db
- name: Lint
run: pnpm lint
- name: Format
run: pnpm fmt:check
- name: Typecheck
run: pnpm typecheck
deploy:
name: Deploy to VPS
runs-on: ubuntu-latest
needs: [lint]
permissions:
contents: read
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.VPS_HOST }}
username: ${{ secrets.VPS_USER }}
key: ${{ secrets.VPS_SSH_KEY }}
envs: REPO_URL,REF
script: |
set -eu
mkdir -p ${{ env.DIR }}
cd ${{ env.DIR }}
if [ -d .git ]; then
git stash push -m "Auto-deploy $(date)" 2>/dev/null || true
git fetch origin
git checkout "$REF"
git reset --hard "origin/$REF" 2>/dev/null || git reset --hard "$REF"
else
git clone "$REPO_URL" .
git checkout "$REF"
fi
mkdir -p ${{ env.DIR }}/backups
export NODE_VERSION=$(cat .nvmrc | tr -d 'v')
echo "Using Node version: $NODE_VERSION"
cat > .env <<'EOF'
DISCORD_TOKEN=${{ secrets.DISCORD_TOKEN }}
CLIENT_ID=${{ secrets.CLIENT_ID }}
EOF
chmod 600 .env
# Backup the SQLite DB before touching anything
if docker volume inspect webdev-bot_guides-data >/dev/null 2>&1; then
docker run --rm \
-v webdev-bot_guides-data:/data \
-v ${{ env.DIR }}/backups:/backup \
alpine sh -c "if [ -f /data/db.sqlite ]; then cp /data/db.sqlite /backup/db-\$(date +%Y%m%d%H%M%S).sqlite; fi"
ls -1t ${{ env.DIR }}/backups/*.sqlite 2>/dev/null \
| tail -n +$((env.MAX_BACKUPS+1)) \
| xargs -r rm --
fi
docker compose config >/dev/null
docker compose build bot
if ! docker compose run --rm migrate; then
echo "Migration failed — leaving existing bot container untouched."
exit 1
fi
docker compose up -d --remove-orphans bot
sleep 8
if ! docker compose ps bot --status running | grep -q bot; then
echo "Bot container is not running after deploy!"
docker compose logs --tail=50 bot
exit 1
fi
docker compose ps
docker image prune -f
env:
REPO_URL: https://github.com/${{ github.repository }}.git
REF: ${{ github.event.inputs.ref != '' && github.event.inputs.ref || 'main' }}