-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·220 lines (175 loc) · 6.44 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·220 lines (175 loc) · 6.44 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
#!/usr/bin/env bash
set -euo pipefail
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
success() { printf "${GREEN}✓${NC} %s\n" "$1"; }
error() { printf "${RED}✗ %s${NC}\n" "$1" >&2; }
warn() { printf "${YELLOW}⚠${NC} %s\n" "$1"; }
# Resolve project root (parent of scripts/)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"
echo ""
echo "=== mcp-docs — Railway Deploy ==="
echo ""
# ── Prerequisites ──────────────────────────────────────────────
check_cmd() {
local cmd="$1"
local label="${2:-$cmd}"
if ! command -v "$cmd" &>/dev/null; then
error "$label is required but not installed"
exit 1
fi
}
check_cmd railway "railway CLI"
success "railway CLI found"
# Verify railway is logged in
if ! railway whoami &>/dev/null; then
error "Not logged in to Railway. Run: railway login"
exit 1
fi
success "railway authenticated"
# Check .env exists
if [ ! -f .env ]; then
error ".env not found. Run: scripts/setup.sh"
exit 1
fi
success ".env found"
# Verify required vars in .env
env_val() {
local key="$1"
grep "^${key}=" .env 2>/dev/null | cut -d= -f2- || true
}
OPENAI_KEY="$(env_val OPENAI_API_KEY)"
WEBHOOK_SECRET="$(env_val GITHUB_WEBHOOK_SECRET)"
if [ -z "$OPENAI_KEY" ] || [ "$OPENAI_KEY" = "sk-..." ]; then
error "OPENAI_API_KEY not set in .env"
exit 1
fi
success "OPENAI_API_KEY present"
if [ -z "$WEBHOOK_SECRET" ] || [ "$WEBHOOK_SECRET" = "whsec_..." ]; then
error "GITHUB_WEBHOOK_SECRET not set in .env"
exit 1
fi
success "GITHUB_WEBHOOK_SECRET present"
echo ""
# ── Railway project ────────────────────────────────────────────
echo "Checking Railway project..."
if railway status &>/dev/null 2>&1; then
success "Railway project already linked"
else
warn "No Railway project linked, initializing..."
railway init
success "Railway project initialized"
fi
echo ""
# ── PostgreSQL ─────────────────────────────────────────────────
echo "Checking PostgreSQL..."
# Check if a Postgres plugin/service is attached by looking for DATABASE_URL in railway vars
if railway variables 2>/dev/null | grep -q "DATABASE_URL"; then
success "PostgreSQL already attached (DATABASE_URL found)"
else
warn "No DATABASE_URL found. Adding PostgreSQL..."
railway add --plugin postgresql
success "PostgreSQL plugin added"
echo " Waiting for PostgreSQL to provision..."
sleep 10
fi
# Enable pgvector extension
echo "Enabling pgvector extension..."
if railway run psql "\$DATABASE_URL" -c "CREATE EXTENSION IF NOT EXISTS vector;" 2>/dev/null; then
success "pgvector extension enabled"
else
warn "Could not enable pgvector — DATABASE_URL may not be ready yet"
warn " You can run manually after deploy:"
warn " railway run psql \"\$DATABASE_URL\" -c \"CREATE EXTENSION IF NOT EXISTS vector;\""
fi
echo ""
# ── Environment variables ──────────────────────────────────────
echo "Setting environment variables..."
railway variables set OPENAI_API_KEY="$OPENAI_KEY"
success "OPENAI_API_KEY set"
railway variables set GITHUB_WEBHOOK_SECRET="$WEBHOOK_SECRET"
success "GITHUB_WEBHOOK_SECRET set"
railway variables set NODE_ENV=production
success "NODE_ENV set"
GITHUB_TOKEN="$(env_val GITHUB_TOKEN)"
if [ -n "$GITHUB_TOKEN" ]; then
railway variables set GITHUB_TOKEN="$GITHUB_TOKEN"
success "GITHUB_TOKEN set"
else
warn "GITHUB_TOKEN not set in .env, skipping"
fi
echo ""
# ── Deploy ─────────────────────────────────────────────────────
echo "Deploying to Railway..."
railway up --detach
success "Deployment triggered"
echo ""
# ── Wait for deployment ────────────────────────────────────────
echo "Waiting for deployment to become live..."
# Get the Railway-assigned domain
RAILWAY_URL=""
RETRIES=60
while [ $RETRIES -gt 0 ]; do
# Try to extract the public domain from railway status or domain command
RAILWAY_URL="$(railway domain 2>/dev/null || true)"
if [ -n "$RAILWAY_URL" ]; then
break
fi
RETRIES=$((RETRIES - 1))
sleep 5
done
if [ -z "$RAILWAY_URL" ]; then
warn "Could not determine Railway URL automatically"
warn "Check your Railway dashboard for the deployment URL"
RAILWAY_URL="<your-railway-url>"
else
# Ensure URL has https prefix
if [[ "$RAILWAY_URL" != https://* ]]; then
RAILWAY_URL="https://${RAILWAY_URL}"
fi
success "Railway URL: $RAILWAY_URL"
# Poll the health endpoint
echo "Polling health endpoint..."
RETRIES=60
HEALTHY=0
while [ $RETRIES -gt 0 ]; do
HTTP_CODE="$(curl -s -o /dev/null -w "%{http_code}" "${RAILWAY_URL}/health" 2>/dev/null || echo "000")"
if [ "$HTTP_CODE" = "200" ]; then
HEALTHY=1
break
fi
RETRIES=$((RETRIES - 1))
sleep 5
done
if [ "$HEALTHY" -eq 1 ]; then
success "Deployment is live and healthy"
else
warn "Health endpoint not responding yet — deployment may still be starting"
warn "Check: curl ${RAILWAY_URL}/health"
fi
fi
echo ""
# ── Initial seed ───────────────────────────────────────────────
echo "Running initial index seed..."
if railway run npx tsx scripts/seed-index.ts; then
success "Index seeded successfully"
else
warn "Seed failed — you can retry with:"
warn " railway run npx tsx scripts/seed-index.ts"
fi
echo ""
# ── Done ───────────────────────────────────────────────────────
echo "=== Deployment complete! ==="
echo ""
echo "Railway URL: $RAILWAY_URL"
echo ""
echo "Next steps:"
echo " 1. Set a custom domain: railway domain"
echo " 2. Configure webhooks: scripts/setup-webhooks.sh"
echo " 3. Verify MCP endpoint: curl ${RAILWAY_URL}/health"
echo ""