Skip to content

Commit 07c9bae

Browse files
committed
rollback
1 parent 6f6f043 commit 07c9bae

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

pocketbase/pb_hooks/main.pb.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,15 @@ routerAdd("POST", "/api/site/rollback", function (e) {
163163
helpers.requireDeployToken(e);
164164
var siteRoot = helpers.getSiteRoot();
165165

166-
var body = {};
166+
var revisionId = "";
167167
try {
168-
e.bindBody(body);
168+
var raw = readerToString(e.request().body);
169+
if (raw) {
170+
var body = JSON.parse(raw);
171+
revisionId = (body && body.revisionId) ? String(body.revisionId).trim() : "";
172+
}
169173
} catch (err) {}
170-
var revisionId = body.revisionId;
171-
if (!revisionId || typeof revisionId !== "string") throw new BadRequestError("missing revisionId");
174+
if (!revisionId) throw new BadRequestError("Missing revisionId.");
172175

173176
var record = $app.findRecordById("revisions", revisionId);
174177
var pathVal = record.get("path");

scripts/rollback.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env sh
2+
# Rollback to the previous revision (skills.md).
3+
# Usage: ./scripts/rollback.sh [revisionId]
4+
# With no argument: rollback to the previous revision.
5+
# With revisionId: rollback to that revision.
6+
# Env: PB_BASE_URL (default http://127.0.0.1:8090), SITE_DEPLOY_TOKEN (required).
7+
8+
set -e
9+
cd "$(dirname "$0")/.."
10+
11+
if [ -z "$SITE_DEPLOY_TOKEN" ]; then
12+
if [ -f .env ]; then
13+
set -a
14+
. .env
15+
set +a
16+
fi
17+
fi
18+
19+
if [ -z "$SITE_DEPLOY_TOKEN" ]; then
20+
echo "SITE_DEPLOY_TOKEN is not set. Set it in .env or the environment."
21+
exit 1
22+
fi
23+
24+
BASE="${PB_BASE_URL:-http://127.0.0.1:8090}"
25+
26+
if [ -n "$1" ]; then
27+
REVISION_ID="$1"
28+
else
29+
echo "Fetching revisions..."
30+
REV=$(curl -s -H "Authorization: Bearer $SITE_DEPLOY_TOKEN" "$BASE/api/site/revisions?limit=20")
31+
REVISION_ID=$(echo "$REV" | jq -r '.items[1].id // empty')
32+
if [ -z "$REVISION_ID" ]; then
33+
if ! echo "$REV" | grep -q '"items"'; then
34+
echo "Failed to fetch revisions (check SITE_DEPLOY_TOKEN and PB_BASE_URL): $REV"
35+
exit 1
36+
fi
37+
echo "No previous revision to rollback to."
38+
exit 1
39+
fi
40+
echo "Rolling back to previous revision: $REVISION_ID"
41+
fi
42+
43+
OUT=".deploy-out.$$"
44+
CODE=$(curl -s -o "$OUT" -w "%{http_code}" -X POST "$BASE/api/site/rollback" \
45+
-H "Authorization: Bearer $SITE_DEPLOY_TOKEN" \
46+
-H "Content-Type: application/json" \
47+
-d "{\"revisionId\": \"$REVISION_ID\"}")
48+
cat "$OUT"; rm -f "$OUT"
49+
echo "HTTP $CODE"
50+
if [ "$CODE" = "000" ]; then
51+
echo "Connection failed. Is PocketBase running?"
52+
exit 1
53+
fi
54+
if [ "$CODE" -ge 400 ] 2>/dev/null; then
55+
exit 1
56+
fi
57+
if [ "$CODE" = "200" ]; then
58+
echo "Rollback complete."
59+
fi

skills.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Site deploy API (Cursor)
22

3-
Use these placeholders when making requests. Substitute from environment at execution time; never print secret values.
3+
Use these placeholders when making requests. Substitute from environment at execution time; never print secret values. Always create the site in site-build folder.
44

55
- **Base:** `{{PB_BASE_URL}}`
66
- **Auth:** `Authorization: Bearer {{SITE_DEPLOY_TOKEN}}`
@@ -23,4 +23,4 @@ Use these placeholders when making requests. Substitute from environment at exec
2323

2424
## Cleanup
2525

26-
- No DELETE endpoint. Remove old revisions via PocketBase Admin UI (delete record); a delete hook will remove the release directory on disk. Do not delete the current revision.
26+
- No DELETE endpoint. User can remove old revisions via PocketBase Admin UI (delete record); a delete hook will remove the release directory on disk. Do not delete the current revision.

0 commit comments

Comments
 (0)