Skip to content

Commit 072b0bf

Browse files
authored
Merge pull request #11 from crystalshards/claude/prepare-deployment-011CUK8EfZS8krvhwVUByjmi
Prepare application for Railway deployment
2 parents e3dbfeb + 444e7ae commit 072b0bf

File tree

7 files changed

+199
-6
lines changed

7 files changed

+199
-6
lines changed

Dockerfile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ RUN shards install --production
1313
COPY . .
1414
RUN shards build --release --production --static
1515

16-
# Import to scratch image
17-
FROM scratch
16+
# Runtime image
17+
FROM alpine:latest
1818

19-
ARG TARGET
20-
COPY --from=builder /build/bin/* /
21-
COPY --from=builder /build/bin/$TARGET /.main
22-
COPY --from=builder /etc/ssl/cert.pem /etc/ssl1.1/
19+
RUN apk add --no-cache openssl ca-certificates
20+
21+
ARG TARGET=web
22+
COPY --from=builder /build/bin/$TARGET /app
23+
COPY --from=builder /build/assets /assets
24+
COPY --from=builder /build/src/views /views
25+
COPY --from=builder /build/src/scss /scss
26+
27+
WORKDIR /
28+
CMD ["/app"]

Dockerfile.job_runner

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM 84codes/crystal:1.8.1-alpine as builder
2+
3+
# Setup
4+
WORKDIR /build
5+
RUN apk add --no-cache openssl ca-certificates yaml-static curl cmake musl clang clang-dev alpine-sdk dpkg
6+
RUN openssl version -d
7+
8+
# Deps
9+
COPY shard.lock shard.yml ./
10+
RUN shards install --production
11+
12+
# Build
13+
COPY . .
14+
RUN shards build --release --production --static
15+
16+
# Runtime image
17+
FROM alpine:latest
18+
19+
RUN apk add --no-cache openssl ca-certificates
20+
21+
COPY --from=builder /build/bin/job_runner /app
22+
COPY --from=builder /build/assets /assets
23+
COPY --from=builder /build/src/views /views
24+
COPY --from=builder /build/src/scss /scss
25+
26+
WORKDIR /
27+
CMD ["/app"]

RAILWAY_DEPLOYMENT.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Railway Deployment Guide
2+
3+
This guide will help you deploy CrystalShards.org to Railway.
4+
5+
## Prerequisites
6+
7+
- Railway account (https://railway.app)
8+
- Railway CLI installed (optional, but recommended)
9+
- GitHub token for indexing repositories
10+
11+
## Services Required
12+
13+
This application requires the following services:
14+
15+
1. **PostgreSQL Database** - Primary data store
16+
2. **Redis** - Caching and job queue
17+
3. **Web Service** - Main web application
18+
4. **Job Runner** - Background job processor
19+
20+
## Deployment Steps
21+
22+
### Option 1: Deploy via Railway Dashboard
23+
24+
1. **Create a new project on Railway**
25+
- Go to https://railway.app
26+
- Click "New Project"
27+
- Select "Deploy from GitHub repo"
28+
- Select this repository
29+
30+
2. **Add PostgreSQL Database**
31+
- Click "New Service"
32+
- Select "Database"
33+
- Choose "PostgreSQL"
34+
- Railway will automatically set DATABASE_URL
35+
36+
3. **Add Redis**
37+
- Click "New Service"
38+
- Select "Database"
39+
- Choose "Redis"
40+
- Railway will automatically set REDIS_URL
41+
42+
4. **Configure Web Service**
43+
- Select the main service (auto-created from repo)
44+
- Go to "Settings" → "Deploy"
45+
- Set Dockerfile path: `Dockerfile`
46+
- Go to "Variables" and add:
47+
- `PORT` = 3000
48+
- `GITHUB_TOKEN` = your GitHub personal access token
49+
- `DATABASE_URL` = (reference from PostgreSQL service)
50+
- `REDIS_URL` = (reference from Redis service)
51+
52+
5. **Add Job Runner Service**
53+
- Click "New Service"
54+
- Select "GitHub Repo"
55+
- Choose the same repository
56+
- Go to "Settings" → "Deploy"
57+
- Set Dockerfile path: `Dockerfile.job_runner`
58+
- Go to "Variables" and add the same environment variables as web service
59+
60+
6. **Run Database Migrations**
61+
- After first deployment, run migrations:
62+
- In Railway dashboard, go to web service
63+
- Open "Deploy Logs"
64+
- Or use Railway CLI: `railway run bin/migrate`
65+
66+
### Option 2: Deploy via Railway CLI
67+
68+
```bash
69+
# Install Railway CLI
70+
npm i -g @railway/cli
71+
72+
# Login to Railway
73+
railway login
74+
75+
# Create new project
76+
railway init
77+
78+
# Add PostgreSQL
79+
railway add --database postgresql
80+
81+
# Add Redis
82+
railway add --database redis
83+
84+
# Set environment variables
85+
railway variables set GITHUB_TOKEN=your_github_token_here
86+
railway variables set PORT=3000
87+
88+
# Deploy
89+
railway up
90+
91+
# Run migrations (after first deploy)
92+
railway run bin/migrate
93+
```
94+
95+
## Environment Variables
96+
97+
| Variable | Description | Required |
98+
|----------|-------------|----------|
99+
| DATABASE_URL | PostgreSQL connection string | Yes (auto-set by Railway) |
100+
| REDIS_URL | Redis connection string | Yes (auto-set by Railway) |
101+
| GITHUB_TOKEN | GitHub personal access token for API | Yes |
102+
| PORT | Port for web service to listen on | Yes (default: 3000) |
103+
104+
## Post-Deployment
105+
106+
1. **Run migrations**: Ensure database schema is created
107+
2. **Start indexing**: The job runner will automatically start indexing GitHub repositories
108+
3. **Monitor logs**: Check Railway dashboard for any errors
109+
110+
## Troubleshooting
111+
112+
- **Build fails**: Check that Crystal 1.8.1 is compatible with all dependencies
113+
- **Database connection issues**: Verify DATABASE_URL is set correctly
114+
- **Redis connection issues**: Verify REDIS_URL is set correctly
115+
- **No data appearing**: Check job_runner logs to ensure indexing is working
116+
117+
## Custom Domain (Optional)
118+
119+
1. Go to web service settings
120+
2. Click "Domains"
121+
3. Add your custom domain
122+
4. Update DNS records as instructed by Railway

railway.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[build]
2+
builder = "DOCKERFILE"
3+
dockerfilePath = "Dockerfile"
4+
5+
[deploy]
6+
numReplicas = 1
7+
restartPolicyType = "ON_FAILURE"
8+
restartPolicyMaxRetries = 10

shard.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ targets:
1111
main: src/web.cr
1212
job_runner:
1313
main: src/job_runner.cr
14+
migrate:
15+
main: src/migrate.cr
1416

1517
dependencies:
1618
orion:

src/migrate.cr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require "./app"
2+
3+
puts "Running database migrations..."
4+
Clear::Migration::Manager.instance.apply_all
5+
puts "Migrations completed successfully!"

src/web.cr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "./app"
2+
require "orion"
3+
require "./controllers/**"
4+
5+
# Routes
6+
Orion.get "/", HomeController, :home
7+
Orion.get "/stats", HomeController, :stats
8+
9+
Orion.get "/shards", ShardsController, :index
10+
Orion.get "/shards/search", ShardsController, :search
11+
Orion.get "/shards/:provider/:uri", ShardsController, :show
12+
13+
Orion.get "/authors", AuthorsController, :index
14+
Orion.get "/authors/:email_or_name", AuthorsController, :show
15+
16+
Orion.get "/tags", TagsController, :index
17+
Orion.get "/tags/:name", TagsController, :show
18+
19+
# Static files (if needed)
20+
# Orion.static "/assets", "./assets"
21+
22+
# Start server
23+
Orion.listen

0 commit comments

Comments
 (0)