|
| 1 | +# Railway Deployment Guide |
| 2 | + |
| 3 | +## Automatic Deployment Setup |
| 4 | + |
| 5 | +StackMemory includes automated deployment to Railway via GitHub Actions. |
| 6 | + |
| 7 | +### Quick Setup |
| 8 | + |
| 9 | +1. **Run the setup script:** |
| 10 | + ```bash |
| 11 | + npm run railway:setup |
| 12 | + ``` |
| 13 | + This will: |
| 14 | + - Install Railway CLI |
| 15 | + - Link your Railway project |
| 16 | + - Generate required tokens |
| 17 | + - Guide you through GitHub secrets setup |
| 18 | + |
| 19 | +2. **Add GitHub Secrets:** |
| 20 | + Go to your repository Settings → Secrets → Actions and add: |
| 21 | + - `RAILWAY_TOKEN` - Your Railway API token |
| 22 | + - `RAILWAY_PROJECT_ID` - Your Railway project ID |
| 23 | + |
| 24 | +3. **Deploy:** |
| 25 | + Deployments happen automatically when you: |
| 26 | + - Push to `main` or `master` branch → Production deployment |
| 27 | + - Open a pull request → Preview deployment |
| 28 | + |
| 29 | +### Manual Deployment |
| 30 | + |
| 31 | +```bash |
| 32 | +# Deploy current branch |
| 33 | +npm run railway:deploy |
| 34 | + |
| 35 | +# View logs |
| 36 | +npm run railway:logs |
| 37 | +``` |
| 38 | + |
| 39 | +## GitHub Actions Workflows |
| 40 | + |
| 41 | +### Production Deployment (.github/workflows/railway-deploy.yml) |
| 42 | +- Triggers on push to main/master |
| 43 | +- Runs tests before deploying |
| 44 | +- Posts deployment status as commit comment |
| 45 | +- Deploys to production environment |
| 46 | + |
| 47 | +### Preview Deployments (.github/workflows/railway-preview.yml) |
| 48 | +- Triggers on pull requests |
| 49 | +- Creates isolated preview environment |
| 50 | +- Posts preview URL as PR comment |
| 51 | +- Auto-cleans up when PR is closed |
| 52 | + |
| 53 | +## Railway Configuration |
| 54 | + |
| 55 | +### railway.json |
| 56 | +Configures build and deploy settings: |
| 57 | +- Build command: `npm run build` |
| 58 | +- Start command: `npm start` |
| 59 | +- Health check endpoint: `/health` |
| 60 | +- Auto-restart on failure (max 3 retries) |
| 61 | + |
| 62 | +### Environment Variables |
| 63 | +Set these in Railway dashboard: |
| 64 | + |
| 65 | +#### Required |
| 66 | +- `REDIS_URL` - Redis connection (use Railway Redis addon) |
| 67 | +- `LINEAR_API_KEY` - Linear API key for task sync |
| 68 | +- `CHROMADB_API_KEY` - ChromaDB for vector storage |
| 69 | + |
| 70 | +#### Optional |
| 71 | +- `DATABASE_URL` - PostgreSQL connection (use Railway PostgreSQL addon) |
| 72 | +- `GCS_BUCKET_NAME` - Google Cloud Storage for cold tier |
| 73 | +- `SENTRY_DSN` - Error tracking |
| 74 | +- `PORT` - Server port (Railway sets automatically) |
| 75 | + |
| 76 | +### Railway Addons |
| 77 | + |
| 78 | +1. **Redis** (Required for hot tier storage) |
| 79 | + - Add via Railway dashboard |
| 80 | + - Auto-populates `REDIS_URL` |
| 81 | + |
| 82 | +2. **PostgreSQL** (Optional) |
| 83 | + - For persistent metadata |
| 84 | + - Auto-populates `DATABASE_URL` |
| 85 | + |
| 86 | +## Storage Tiers on Railway |
| 87 | + |
| 88 | +StackMemory uses a 3-tier storage system optimized for Railway: |
| 89 | + |
| 90 | +1. **Hot Tier (Redis)** |
| 91 | + - Recent traces (<24 hours) |
| 92 | + - High-score traces |
| 93 | + - Railway Redis addon |
| 94 | + - LRU eviction, 100MB limit |
| 95 | + |
| 96 | +2. **Warm Tier (Railway Volumes)** |
| 97 | + - Mid-age traces (1-30 days) |
| 98 | + - Railway persistent volumes |
| 99 | + - Compressed storage |
| 100 | + |
| 101 | +3. **Cold Tier (GCS)** |
| 102 | + - Old traces (>30 days) |
| 103 | + - Google Cloud Storage Coldline |
| 104 | + - $0.004/GB/month |
| 105 | + |
| 106 | +## Monitoring |
| 107 | + |
| 108 | +### Health Check |
| 109 | +Railway monitors `/health` endpoint: |
| 110 | +```javascript |
| 111 | +// Automatically included in deployment |
| 112 | +app.get('/health', (req, res) => { |
| 113 | + res.json({ |
| 114 | + status: 'healthy', |
| 115 | + redis: redisClient.isReady, |
| 116 | + storage: storageSystem.status |
| 117 | + }); |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +### Logs |
| 122 | +```bash |
| 123 | +# View live logs |
| 124 | +npm run railway:logs |
| 125 | + |
| 126 | +# Via Railway CLI |
| 127 | +railway logs --tail |
| 128 | + |
| 129 | +# Filter by service |
| 130 | +railway logs --service stackmemory |
| 131 | +``` |
| 132 | + |
| 133 | +## Troubleshooting |
| 134 | + |
| 135 | +### Deployment Fails |
| 136 | +1. Check GitHub Actions logs |
| 137 | +2. Verify secrets are set correctly |
| 138 | +3. Ensure tests pass locally: `npm test` |
| 139 | +4. Check Railway dashboard for errors |
| 140 | + |
| 141 | +### Redis Connection Issues |
| 142 | +1. Verify Redis addon is provisioned |
| 143 | +2. Check `REDIS_URL` in environment variables |
| 144 | +3. Test locally with Railway proxy: |
| 145 | + ```bash |
| 146 | + railway run npm start |
| 147 | + ``` |
| 148 | + |
| 149 | +### Preview Deployments Not Working |
| 150 | +1. Ensure `RAILWAY_TOKEN` has project access |
| 151 | +2. Check PR has no merge conflicts |
| 152 | +3. Verify GitHub Actions are enabled |
| 153 | + |
| 154 | +## Cost Optimization |
| 155 | + |
| 156 | +### Railway Free Tier |
| 157 | +- 500 hours/month execution time |
| 158 | +- 100GB bandwidth |
| 159 | +- Suitable for development |
| 160 | + |
| 161 | +### Production Recommendations |
| 162 | +- Use Railway Pro for production |
| 163 | +- Enable auto-scaling for traffic spikes |
| 164 | +- Monitor resource usage in dashboard |
| 165 | +- Use Redis maxmemory policies |
| 166 | + |
| 167 | +## Security |
| 168 | + |
| 169 | +### Secrets Management |
| 170 | +- Never commit `.env` files |
| 171 | +- Use Railway's environment variables |
| 172 | +- Rotate tokens regularly |
| 173 | +- Use read-only tokens where possible |
| 174 | + |
| 175 | +### Network Security |
| 176 | +- Railway provides HTTPS by default |
| 177 | +- Private networking between services |
| 178 | +- IP allowlisting available on Pro plan |
| 179 | + |
| 180 | +## Support |
| 181 | + |
| 182 | +- Railway Discord: https://discord.gg/railway |
| 183 | +- Railway Docs: https://docs.railway.app |
| 184 | +- StackMemory Issues: https://github.com/stackmemoryai/stackmemory/issues |
0 commit comments