Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
Β·327 lines (283 loc) Β· 9.12 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
Β·327 lines (283 loc) Β· 9.12 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
#!/bin/bash
set -e
if [ -f ./.env ]; then
set -a
source ./.env
set +a
fi
TARGET=${1:-auto}
PROVIDER_OVERRIDE=$2 # optional: ./deploy.sh backend gcp
BACKEND_PROVIDER=${PROVIDER_OVERRIDE:-${BACKEND_PROVIDER:-fly}}
DEPLOY_SHA_FILE=".last-deploy-sha"
LAST_GCP_VM_FILE=".last-gcp-vm"
echo "π Deploy mode: $TARGET (backend: $BACKEND_PROVIDER)"
# ---------------------------------------
# Helpers
# ---------------------------------------
last_deploy_sha () {
if [ -f "$DEPLOY_SHA_FILE" ]; then
cat "$DEPLOY_SHA_FILE"
else
git rev-list --max-parents=0 HEAD | tail -1
fi
}
changed_www () {
git diff --name-only "$(last_deploy_sha)"...HEAD | grep -q "^www/"
}
changed_server () {
git diff --name-only "$(last_deploy_sha)"...HEAD | grep -q "^server/"
}
record_deploy () {
git rev-parse HEAD > "$DEPLOY_SHA_FILE"
}
# ---------------------------------------
# Backend URL resolution
# ---------------------------------------
get_backend_url () {
if [ "$BACKEND_PROVIDER" = "fly" ]; then
echo "https://${FLY_APP}.fly.dev"
elif [ "$BACKEND_PROVIDER" = "gcp" ]; then
local IP
IP=$(gcloud compute addresses describe pocketbase-ip --region=us-east1 --format="value(address)")
echo "https://${IP}.nip.io"
fi
}
# ---------------------------------------
# Frontend (Vercel) β always points at whichever backend is active
# ---------------------------------------
deploy_frontend () {
local PB_URL
PB_URL=$(get_backend_url)
echo "π¦ Deploying Astro frontend to Vercel (backend: $BACKEND_PROVIDER β $PB_URL)..."
(
cd www
vercel env rm PUBLIC_PB_URL production --yes >/dev/null 2>&1 || true
echo "$PB_URL" | vercel env add PUBLIC_PB_URL production
vercel --prod
)
}
# ---------------------------------------
# GCP safety checks
# ---------------------------------------
# Fix #2: guard against GCP_VM_NAME silently pointing at a different VM than
# the one this project has been deploying to. If the VM name changes without
# anyone noticing, the "new" VM's pb_data starts empty β which looks
# identical to data loss even though nothing was technically overwritten.
check_gcp_vm_identity () {
if [ -z "$GCP_VM_NAME" ]; then
echo "β GCP_VM_NAME is not set. Refusing to deploy without an explicit target VM."
exit 1
fi
if [ -f "$LAST_GCP_VM_FILE" ]; then
local PREVIOUS_VM
PREVIOUS_VM=$(cat "$LAST_GCP_VM_FILE")
if [ "$PREVIOUS_VM" != "$GCP_VM_NAME" ]; then
echo "β οΈ GCP_VM_NAME has changed since the last deploy:"
echo " previous: $PREVIOUS_VM"
echo " current: $GCP_VM_NAME"
echo " Deploying to a different VM means its pb_data will be whatever"
echo " already exists there (likely empty on a fresh VM), NOT the data"
echo " from $PREVIOUS_VM."
read -r -p " Continue anyway? [y/N] " CONFIRM
if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then
echo "β Aborted."
exit 1
fi
fi
fi
echo "$GCP_VM_NAME" > "$LAST_GCP_VM_FILE"
}
# Fix #1: snapshot pb_data to GCS immediately before any deploy that could
# touch the running container, so a bad deploy or host-level accident is
# always recoverable. Requires GCS_BACKUP_BUCKET to be set in .env and the
# VM's service account to have write access to that bucket.
backup_pb_data_gcp () {
if [ -z "$GCS_BACKUP_BUCKET" ]; then
echo "β GCS_BACKUP_BUCKET is not set."
echo " Refusing to deploy without a PocketBase backup."
exit 1
fi
local TIMESTAMP
TIMESTAMP=$(date -u +"%Y%m%dT%H%M%SZ")
local BACKUP_PATH="gs://${GCS_BACKUP_BUCKET}/pb_data-backups/${GCP_VM_NAME}/${TIMESTAMP}.tar.gz"
echo "πΎ Backing up pb_data on $GCP_VM_NAME to $BACKUP_PATH ..."
gcloud compute ssh "$GCP_VM_NAME" --zone="$GCP_ZONE" --command="
set -e
if [ ! -d \$HOME/pb_data ] || [ -z \"\$(ls -A \$HOME/pb_data 2>/dev/null)\" ]; then
echo 'β οΈ pb_data is missing or empty on this VM β nothing to back up.'
exit 0
fi
tar -czf /tmp/pb_data_backup.tar.gz -C \$HOME pb_data
gcloud storage cp /tmp/pb_data_backup.tar.gz '$BACKUP_PATH'
rm -f /tmp/pb_data_backup.tar.gz
"
echo "β
Backup complete: $BACKUP_PATH"
}
# Optional companion to backup_pb_data_gcp β restores the most recent backup
# for the current GCP_VM_NAME. Stops the container first since PocketBase
# shouldn't be writing to pb_data mid-restore.
restore_pb_data_gcp () {
if [ -z "$GCS_BACKUP_BUCKET" ]; then
echo "β GCS_BACKUP_BUCKET is not set β nowhere to restore from."
exit 1
fi
echo "β οΈ This will REPLACE the current pb_data on $GCP_VM_NAME with the"
echo " most recent backup for that VM. The running container will be"
echo " stopped during the restore."
read -r -p " Continue? [y/N] " CONFIRM
if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then
echo "β Aborted."
exit 1
fi
gcloud compute ssh "$GCP_VM_NAME" --zone="$GCP_ZONE" --command="
set -e
LATEST=\$(gcloud storage ls 'gs://${GCS_BACKUP_BUCKET}/pb_data-backups/${GCP_VM_NAME}/*.tar.gz' | sort | tail -1)
if [ -z \"\$LATEST\" ]; then
echo 'β No backups found for ${GCP_VM_NAME}.'
exit 1
fi
echo \"β¬οΈ Restoring \$LATEST ...\"
docker stop pocketbase || true
gcloud storage cp \"\$LATEST\" /tmp/pb_data_restore.tar.gz
rm -rf \$HOME/pb_data
tar -xzf /tmp/pb_data_restore.tar.gz -C \$HOME
rm -f /tmp/pb_data_restore.tar.gz
docker start pocketbase
"
echo "β
Restore complete."
}
# ---------------------------------------
# Backend β dispatches to fly or gcp
# ---------------------------------------
deploy_backend () {
if [ "$BACKEND_PROVIDER" = "fly" ]; then
deploy_backend_fly
elif [ "$BACKEND_PROVIDER" = "gcp" ]; then
deploy_backend_gcp
else
echo "β Unknown BACKEND_PROVIDER: $BACKEND_PROVIDER"
exit 1
fi
}
deploy_backend_fly () {
echo "π¦ Deploying PocketBase to Fly.io..."
(cd server && fly deploy)
}
deploy_backend_gcp () {
if [ ! -f ./server/Dockerfile ]; then
echo "β server/Dockerfile is missing β cannot build GCP image."
exit 1
fi
check_gcp_vm_identity
backup_pb_data_gcp
echo "π¦ Deploying PocketBase to GCP VM..."
PROJECT_ID=$(gcloud config get-value project)
REPO="phcf-scratch-pad"
IMAGE="us-docker.pkg.dev/$PROJECT_ID/$REPO/pocketbase"
echo "π³ Building and pushing image..."
gcloud builds submit ./server --tag "$IMAGE"
echo "βοΈ Deploying to $GCP_VM_NAME..."
gcloud compute ssh "$GCP_VM_NAME" --zone="$GCP_ZONE" --command="
set -e
docker pull $IMAGE
docker stop pocketbase || true
docker rm pocketbase || true
docker run -d \
--name pocketbase \
-p 8080:8080 \
-v \$HOME/pb_data:/app/pb_data \
--restart unless-stopped \
$IMAGE
sleep 2
docker logs --tail 20 pocketbase
"
}
# ---------------------------------------
# Superuser β dispatches to fly or gcp
# ---------------------------------------
superuser () {
local EMAIL=${1:-$SUPERUSER_EMAIL}
local PASS=${2:-$SUPERUSER_PASSWORD}
if [ -z "$EMAIL" ] || [ -z "$PASS" ]; then
echo "β No superuser credentials found. Set SUPERUSER_EMAIL / SUPERUSER_PASSWORD in .env, or pass them as arguments."
exit 1
fi
echo "π Upserting superuser $EMAIL on $BACKEND_PROVIDER backend..."
if [ "$BACKEND_PROVIDER" = "fly" ]; then
fly ssh console -a "$FLY_APP" -C "/app/app superuser upsert $EMAIL $PASS"
elif [ "$BACKEND_PROVIDER" = "gcp" ]; then
gcloud compute ssh "$GCP_VM_NAME" --zone="$GCP_ZONE" --command="
docker stop pocketbase || true
docker run --rm -v \$HOME/pb_data:/app/pb_data \
us-docker.pkg.dev/\$(gcloud config get-value project)/phcf-scratch-pad/pocketbase \
/app/app superuser upsert $EMAIL $PASS
docker start pocketbase
"
fi
}
# ---------------------------------------
# Dispatch
# ---------------------------------------
case "$TARGET" in
frontend)
deploy_frontend
record_deploy
;;
backend)
deploy_backend
record_deploy
;;
all)
deploy_backend
deploy_frontend
record_deploy
;;
superuser)
superuser "$3" "$4"
;;
backup)
if [ "$BACKEND_PROVIDER" != "gcp" ]; then
echo "β backup is only implemented for the gcp backend."
exit 1
fi
check_gcp_vm_identity
backup_pb_data_gcp
;;
restore)
if [ "$BACKEND_PROVIDER" != "gcp" ]; then
echo "β restore is only implemented for the gcp backend."
exit 1
fi
check_gcp_vm_identity
restore_pb_data_gcp
;;
auto)
echo "π Detecting changes since last deploy..."
WWW_CHANGED=false
SERVER_CHANGED=false
changed_www && WWW_CHANGED=true
changed_server && SERVER_CHANGED=true
if $SERVER_CHANGED; then
echo "π¦ /server changed β deploying backend"
deploy_backend
else
echo "β
/server unchanged"
fi
if $WWW_CHANGED; then
echo "π¦ /www changed β deploying frontend"
deploy_frontend
else
echo "β
/www unchanged"
fi
if $WWW_CHANGED || $SERVER_CHANGED; then
record_deploy
else
echo "π‘ No changes detected β skipping deploy"
fi
;;
*)
echo "Unknown target: $TARGET"
exit 1
;;
esac
echo "β
Done!"