g1t/scripts/deploy.sh

67 lines1,812 bytesCodeBlame
1#!/usr/bin/env bash
2# Deploys g1t: every part, or the ones named, in the order they depend on
3# each other. Each part's D1 migrations are applied before its code goes
4# out, so new code never meets an old database.
5#
6# scripts/deploy.sh # everything
7# scripts/deploy.sh billing web # just these, still in order
8#
9# Uses your `wrangler login`. The repository's .env may hold a token for
10# other tools; it is ignored here unless you set CLOUDFLARE_DEPLOY_TOKEN.
11set -euo pipefail
12
13ROOT="$(cd "$(dirname "$0")/.." && pwd)"
14export CLOUDFLARE_API_TOKEN="${CLOUDFLARE_DEPLOY_TOKEN:-}"
15
16# Dependency order: what others bind to goes first.
17ORDER=(
18 services/events
19 services/identity
20 services/repos
21 services/work
22 services/search
23 services/projects
24 services/billing
25 services/integrations
26 services/webhooks
27 services/actions
28 services/models
29 services/deployments
30 services/runner
31 services/security
32 services/context
33 apps/api
34 services/pages
35 services/og
36 # The status page: binds only to billing, checks the rest over the internet.
37 apps/status
38 apps/web
39 apps/sudo
40 apps/docs
41)
42
43wanted() {
44 [ $# -eq 0 ] && return 0
45 local dir="$1"; shift
46 for name in "${PICK[@]}"; do
47 [ "$dir" = "$name" ] || [ "$(basename "$dir")" = "$name" ] && return 0
48 done
49 return 1
50}
51
52PICK=("$@")
53for dir in "${ORDER[@]}"; do
54 [ ${#PICK[@]} -eq 0 ] || wanted "$dir" || continue
55 echo "== $dir"
56 cd "$ROOT/$dir"
57 db=$(grep -o '"database_name": *"[^"]*"' wrangler.jsonc 2>/dev/null | head -1 | sed 's/.*"\([^"]*\)"$/\1/' || true)
58 if [ -n "$db" ] && [ -d migrations ]; then
59 npx wrangler d1 migrations apply "$db" --remote
60 fi
61 if grep -q '"deploy": "[^"]*build' package.json 2>/dev/null; then
62 npm run deploy
63 else
64 npx wrangler deploy
65 fi
66done
67echo "== Deployed."