Skip to content

Commit 2568a96

Browse files
authored
ci: add authentication support to snapshot-release script (#519)
# Enhanced Snapshot Release Process with Authentication and Improved Output This PR improves the snapshot release script with several key enhancements: 1. Added NPM authentication support in `.npmrc` to use the `NPM_TOKEN` environment variable 2. Implemented comprehensive authentication checks for both NPM and JSR before publishing 3. Added support for both token-based and browser-based authentication flows 4. Excluded demo and website packages from the build process to speed up releases 5. Improved error handling during the publishing process 6. Enhanced the installation instructions output with: - Clear version information - Simplified npx CLI installation command - Properly formatted Deno/Supabase import examples - Complete deno.json import map configuration 7. Added an optional feature to spawn a test Supabase project with the released version These changes make the snapshot release process more robust for both local development and CI/CD environments.
1 parent bfb393b commit 2568a96

File tree

2 files changed

+139
-37
lines changed

2 files changed

+139
-37
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
strict-peer-dependencies=false
22
auto-install-peers=true
33
@jsr:registry=https://npm.jsr.io
4+
//registry.npmjs.org/:_authToken=${NPM_TOKEN}

scripts/snapshot-release.sh

Lines changed: 138 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,63 @@ while [[ $# -gt 0 ]]; do
6969
esac
7070
done
7171

72+
# ------------------------------------------------------------------
73+
# Authenticate for publishing (token env vars or browser login)
74+
# ------------------------------------------------------------------
75+
echo "Checking authentication..."
76+
77+
# NPM authentication
78+
if [[ -n "${NPM_TOKEN:-}" ]]; then
79+
# Token provided - try to get user info (may fail with some token types)
80+
if NPM_USER=$(npm whoami 2>/dev/null); then
81+
echo -e " ${GREEN}${NC} npm: authenticated as ${BOLD}$NPM_USER${NC} (via token)"
82+
else
83+
echo -e " ${YELLOW}!${NC} npm: NPM_TOKEN set (whoami check failed, will verify at publish)"
84+
fi
85+
else
86+
# No token - check if already logged in, otherwise prompt browser login
87+
if NPM_USER=$(npm whoami 2>/dev/null); then
88+
echo -e " ${GREEN}${NC} npm: authenticated as ${BOLD}$NPM_USER${NC} (existing session)"
89+
else
90+
echo -e " ${YELLOW}npm: not logged in - opening browser...${NC}"
91+
if npm login --auth-type=web; then
92+
NPM_USER=$(npm whoami)
93+
echo -e " ${GREEN}${NC} npm: authenticated as ${BOLD}$NPM_USER${NC}"
94+
else
95+
echo -e "${RED}Error: npm login failed${NC}"
96+
exit 1
97+
fi
98+
fi
99+
fi
100+
101+
# JSR authentication - always validate with dry-run
102+
JSR_DRY_RUN_CMD="pnpm jsr publish --dry-run --allow-slow-types --allow-dirty"
103+
if [[ -n "${JSR_TOKEN:-}" ]]; then
104+
JSR_DRY_RUN_CMD="$JSR_DRY_RUN_CMD --token $JSR_TOKEN"
105+
fi
106+
JSR_CHECK=$(cd pkgs/edge-worker && $JSR_DRY_RUN_CMD 2>&1 || true)
107+
if echo "$JSR_CHECK" | grep -qi "unauthorized\|not logged in\|authentication"; then
108+
if [[ -n "${JSR_TOKEN:-}" ]]; then
109+
echo -e " ${YELLOW}!${NC} jsr: JSR_TOKEN set but invalid/expired - opening browser..."
110+
else
111+
echo -e " ${YELLOW}jsr: not logged in - opening browser...${NC}"
112+
fi
113+
if deno login; then
114+
echo -e " ${GREEN}${NC} jsr: authenticated via browser"
115+
else
116+
echo -e "${RED}Error: JSR login failed${NC}"
117+
exit 1
118+
fi
119+
else
120+
if [[ -n "${JSR_TOKEN:-}" ]]; then
121+
echo -e " ${GREEN}${NC} jsr: authenticated (via token)"
122+
else
123+
echo -e " ${GREEN}${NC} jsr: authenticated (existing session)"
124+
fi
125+
fi
126+
127+
echo ""
128+
72129
# ------------------------------------------------------------------
73130
# Resolve snapshot tag if not provided
74131
# ------------------------------------------------------------------
@@ -249,7 +306,7 @@ echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━
249306
echo -e "${BOLD}Building packages...${NC}"
250307
echo ""
251308

252-
if pnpm nx run-many -t build ; then
309+
if pnpm nx run-many -t build --exclude=demo,website ; then
253310
echo -e "${GREEN}✓ Packages built successfully${NC}"
254311
else
255312
echo -e "${RED}✗ Build failed${NC}"
@@ -269,11 +326,10 @@ NPM_SUCCESS=false
269326
JSR_SUCCESS=true # Default true (only set false if JSR package exists and fails)
270327
JSR_PUBLISHED_VERSION="" # Track JSR version if published
271328

272-
# Publish to npm
329+
# Publish to npm (token is read from NPM_TOKEN env var via .npmrc)
330+
# GITHUB_ACTIONS=true skips changeset's npm profile check which fails with automation tokens
273331
echo -e "${BOLD}Publishing to npm...${NC}"
274-
NPM_OUTPUT=$(pnpm exec changeset publish --tag snapshot 2>&1)
275-
echo "$NPM_OUTPUT"
276-
if echo "$NPM_OUTPUT" | grep -qi "published" ; then
332+
if GITHUB_ACTIONS=true pnpm exec changeset publish --tag snapshot ; then
277333
echo -e "${GREEN}✓ npm packages published${NC}"
278334
NPM_SUCCESS=true
279335
else
@@ -286,7 +342,12 @@ if [[ -f pkgs/edge-worker/jsr.json ]]; then
286342
JSR_PUBLISHED_VERSION=$(jq -r '.version' pkgs/edge-worker/jsr.json)
287343
echo ""
288344
echo -e "${BOLD}Publishing to JSR...${NC}"
289-
if ( cd pkgs/edge-worker && pnpm jsr publish --allow-slow-types --allow-dirty ) ; then
345+
JSR_PUBLISH_CMD="pnpm jsr publish --allow-slow-types --allow-dirty"
346+
# Pass token explicitly if set (pnpm jsr may not inherit JSR_TOKEN properly)
347+
if [[ -n "${JSR_TOKEN:-}" ]]; then
348+
JSR_PUBLISH_CMD="$JSR_PUBLISH_CMD --token $JSR_TOKEN"
349+
fi
350+
if ( cd pkgs/edge-worker && $JSR_PUBLISH_CMD ) ; then
290351
echo -e "${GREEN}✓ JSR package published${NC}"
291352
else
292353
echo -e "${RED}✗ JSR publish failed${NC}"
@@ -314,61 +375,101 @@ fi
314375
# ------------------------------------------------------------------
315376
# Display installation instructions
316377
# ------------------------------------------------------------------
378+
379+
# Extract versions for output
380+
PGFLOW_VERSION=""
381+
DSL_VERSION=""
382+
CORE_VERSION=""
383+
for PKG in "${NPM_PKGS[@]}"; do
384+
if [[ $PKG == "pgflow@"* ]]; then
385+
PGFLOW_VERSION=$(echo "$PKG" | rev | cut -d'@' -f1 | rev)
386+
elif [[ $PKG == "@pgflow/dsl@"* ]]; then
387+
DSL_VERSION=$(echo "$PKG" | rev | cut -d'@' -f1 | rev)
388+
elif [[ $PKG == "@pgflow/core@"* ]]; then
389+
CORE_VERSION=$(echo "$PKG" | rev | cut -d'@' -f1 | rev)
390+
fi
391+
done
392+
317393
echo ""
318394
echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
319395
echo -e "${GREEN}✅ Snapshot release complete!${NC}"
320396
echo ""
321-
echo -e "${BOLD}Install with npm:${NC}"
397+
398+
# Raw version for easy selection
399+
echo -e "${BOLD}Version:${NC} ${GREEN}${PGFLOW_VERSION}${NC}"
322400
echo ""
323401

324-
# NPM packages
325-
for PKG in "${NPM_PKGS[@]}"; do
326-
[[ $PKG == "@pgflow/edge-worker"* ]] && continue
327-
echo -e "${BLUE}npm install $PKG${NC}"
328-
done
402+
# npx command
403+
echo -e "${BOLD}Install pgflow CLI:${NC}"
404+
echo -e "${BLUE}npx -y pgflow@${PGFLOW_VERSION} install${NC}"
405+
echo ""
329406

330-
# JSR package (only show if successfully published)
407+
# Deno/Supabase imports
408+
echo -e "${BOLD}Deno/Supabase Edge Function imports:${NC}"
331409
if [[ -n "$JSR_PUBLISHED_VERSION" ]]; then
332-
echo ""
333-
echo -e "${BOLD}For Deno/Supabase Edge Functions:${NC}"
334-
echo -e "${BLUE}import { EdgeWorker } from \"jsr:@pgflow/edge-worker@$JSR_PUBLISHED_VERSION\"${NC}"
410+
echo -e "${BLUE}import { EdgeWorker } from \"jsr:@pgflow/edge-worker@$JSR_PUBLISHED_VERSION\";${NC}"
411+
fi
412+
if [[ -n "$DSL_VERSION" ]]; then
413+
echo -e "${BLUE}import { Flow } from \"npm:@pgflow/dsl@$DSL_VERSION\";${NC}"
414+
echo -e "${BLUE}import { Flow } from \"npm:@pgflow/dsl@$DSL_VERSION/supabase\";${NC}"
335415
fi
416+
echo ""
336417

337418
# Deno import map
338-
echo ""
339-
echo -e "${BOLD}Or add to deno.json imports:${NC}"
419+
echo -e "${BOLD}Add to deno.json imports:${NC}"
340420
echo ""
341421
echo -e "${BLUE}{"
342422
echo -e " \"imports\": {"
343423

344-
# Extract versions for deno.json
345-
DSL_VERSION=""
346-
CORE_VERSION=""
347-
for PKG in "${NPM_PKGS[@]}"; do
348-
if [[ $PKG == "@pgflow/dsl@"* ]]; then
349-
DSL_VERSION=$(echo "$PKG" | rev | cut -d'@' -f1 | rev)
350-
elif [[ $PKG == "@pgflow/core@"* ]]; then
351-
CORE_VERSION=$(echo "$PKG" | rev | cut -d'@' -f1 | rev)
352-
fi
353-
done
354-
355-
# Show edge-worker (JSR) - only if successfully published
356-
if [[ -n "$JSR_PUBLISHED_VERSION" ]]; then
357-
echo -e " \"@pgflow/edge-worker\": \"jsr:@pgflow/edge-worker@$JSR_PUBLISHED_VERSION\","
424+
# Show core (npm)
425+
if [[ -n "$CORE_VERSION" ]]; then
426+
echo -e " \"@pgflow/core\": \"npm:@pgflow/core@$CORE_VERSION\","
427+
echo -e " \"@pgflow/core/\": \"npm:@pgflow/core@$CORE_VERSION/\","
358428
fi
359429

360-
# Show dsl and dsl/supabase (npm)
430+
# Show dsl (npm)
361431
if [[ -n "$DSL_VERSION" ]]; then
362432
echo -e " \"@pgflow/dsl\": \"npm:@pgflow/dsl@$DSL_VERSION\","
363-
echo -e " \"@pgflow/dsl/supabase\": \"npm:@pgflow/dsl@$DSL_VERSION/supabase\","
433+
echo -e " \"@pgflow/dsl/\": \"npm:@pgflow/dsl@$DSL_VERSION/\","
434+
if [[ -n "$JSR_PUBLISHED_VERSION" ]]; then
435+
echo -e " \"@pgflow/dsl/supabase\": \"npm:@pgflow/dsl@$DSL_VERSION/supabase\","
436+
else
437+
echo -e " \"@pgflow/dsl/supabase\": \"npm:@pgflow/dsl@$DSL_VERSION/supabase\""
438+
fi
364439
fi
365440

366-
# Show core (npm) - no trailing comma on last entry
367-
if [[ -n "$CORE_VERSION" ]]; then
368-
echo -e " \"@pgflow/core\": \"npm:@pgflow/core@$CORE_VERSION\""
441+
# Show edge-worker (JSR) - only if successfully published
442+
if [[ -n "$JSR_PUBLISHED_VERSION" ]]; then
443+
echo -e " \"@pgflow/edge-worker\": \"jsr:@pgflow/edge-worker@$JSR_PUBLISHED_VERSION\","
444+
echo -e " \"@pgflow/edge-worker/\": \"jsr:@pgflow/edge-worker@$JSR_PUBLISHED_VERSION/\","
445+
echo -e " \"@pgflow/edge-worker/_internal\": \"jsr:@pgflow/edge-worker@$JSR_PUBLISHED_VERSION/_internal\""
369446
fi
370447

371448
echo -e " }"
372449
echo -e "}${NC}"
373450

451+
echo ""
452+
453+
# ------------------------------------------------------------------
454+
# Optional: Spawn test Supabase project
455+
# ------------------------------------------------------------------
456+
if [[ "$SKIP_CONFIRMATION" != "true" ]]; then
457+
echo -e "${YELLOW}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
458+
echo -e "${BOLD}Spawn test Supabase project with this version?${NC}"
459+
echo -e " Will run: ${BLUE}mksupa new '$TAG' --pgflow='$PGFLOW_VERSION'${NC}"
460+
echo ""
461+
read -p "Create test project? (y/N) " -n 1 -r
462+
echo ""
463+
464+
if [[ $REPLY =~ ^[Yy]$ ]]; then
465+
echo ""
466+
echo -e "${BOLD}Spawning test Supabase project...${NC}"
467+
if command -v fish >/dev/null 2>&1; then
468+
fish -c "mksupa new '$TAG' --pgflow='$PGFLOW_VERSION'"
469+
else
470+
echo -e "${RED}Error: fish shell not found${NC}"
471+
fi
472+
fi
473+
fi
474+
374475
echo ""

0 commit comments

Comments
 (0)