|
| 1 | +# 🟣 Heroku Deployment Guide |
| 2 | + |
| 3 | +This guide provides a comprehensive, step-by-step walkthrough for deploying **Master-Bot** and its **Next.js Web Dashboard** to [Heroku](https://www.heroku.com/). |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 📑 Table of Contents |
| 8 | + |
| 9 | +1. [Architecture Overview](#-architecture-overview) |
| 10 | +2. [Prerequisites](#-prerequisites) |
| 11 | +3. [Method A: Git Buildpack Deployment](#-method-a-git-buildpack-deployment) |
| 12 | +4. [Method B: Docker Container Deployment (heroku.yml)](#-method-b-docker-container-deployment-herokuxml) |
| 13 | +5. [Database & Redis Add-ons](#-database--redis-add-ons) |
| 14 | +6. [Environment Variables & Config Vars](#-environment-variables--config-vars) |
| 15 | +7. [Scaling Dynos](#-scaling-dynos) |
| 16 | +8. [Database Synchronization](#-database-synchronization) |
| 17 | +9. [Lavalink & Audio Hosting on Heroku](#-lavalink--audio-hosting-on-heroku) |
| 18 | +10. [Monitoring & Logs](#-monitoring--logs) |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## 🏗️ Architecture Overview |
| 23 | + |
| 24 | +On Heroku, Master-Bot runs across dedicated process types: |
| 25 | + |
| 26 | +```text |
| 27 | +┌─────────────────────────────────────────────────────────────┐ |
| 28 | +│ Heroku App │ |
| 29 | +├──────────────────────────────┬──────────────────────────────┤ |
| 30 | +│ web Dyno │ worker Dyno │ |
| 31 | +│ - Next.js 15 Web Dashboard │ - Sapphire & Discord.js Bot │ |
| 32 | +│ - Receives HTTP/HTTPS │ - Connects to Discord WS │ |
| 33 | +├──────────────────────────────┴──────────────────────────────┤ |
| 34 | +│ Heroku Add-ons │ |
| 35 | +│ - Heroku Postgres (DATABASE_URL) │ |
| 36 | +│ - Heroku Data for Redis / Redis Cloud (REDIS_URL) │ |
| 37 | +└─────────────────────────────────────────────────────────────┘ |
| 38 | + ▲ |
| 39 | + │ Lavalink WebSocket (Port 2333) |
| 40 | + ▼ |
| 41 | +┌─────────────────────────────────────────────────────────────┐ |
| 42 | +│ Remote Lavalink v4 Node (Dedicated VPS / External Host) │ |
| 43 | +└─────────────────────────────────────────────────────────────┘ |
| 44 | +``` |
| 45 | + |
| 46 | +* **`web` Dyno**: Hosts the Next.js 15 web dashboard (`apps/dashboard`), bound to Heroku's dynamic `$PORT`. |
| 47 | +* **`worker` Dyno**: Runs the Discord bot client (`apps/bot`) as a background worker. |
| 48 | +* **`Heroku Postgres`**: Provides managed PostgreSQL storage for Prisma ORM. |
| 49 | +* **`Heroku Data for Redis`**: Provides fast caching and queue management. |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## 🛠️ Prerequisites |
| 54 | + |
| 55 | +1. A [Heroku Account](https://signup.heroku.com/). |
| 56 | +2. [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli) installed on your machine: |
| 57 | + - **Windows**: `winget install Heroku.CLI` |
| 58 | + - **macOS**: `brew tap heroku/brew && brew install heroku` |
| 59 | + - **Linux**: `curl https://cli-assets.heroku.com/install.sh | sh` |
| 60 | +3. Verified login: |
| 61 | + ```bash |
| 62 | + heroku login |
| 63 | + ``` |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +## 📦 Method A: Git Buildpack Deployment |
| 68 | + |
| 69 | +### 1. Create a New Heroku Application |
| 70 | + |
| 71 | +```bash |
| 72 | +heroku create master-bot-app |
| 73 | +``` |
| 74 | + |
| 75 | +### 2. Configure Buildpacks |
| 76 | + |
| 77 | +Master-Bot uses `pnpm` and `Node.js 20+`. Configure the official Node.js buildpack: |
| 78 | + |
| 79 | +```bash |
| 80 | +# Add Node.js buildpack |
| 81 | +heroku buildpacks:add heroku/nodejs -a master-bot-app |
| 82 | + |
| 83 | +# Ensure devDependencies are installed during the build phase |
| 84 | +heroku config:set NPM_CONFIG_PRODUCTION=false -a master-bot-app |
| 85 | +``` |
| 86 | + |
| 87 | +### 3. Configure Add-ons (PostgreSQL & Redis) |
| 88 | + |
| 89 | +Attach managed database and Redis services: |
| 90 | + |
| 91 | +```bash |
| 92 | +# Provision PostgreSQL (Essential Tier) |
| 93 | +heroku addons:create heroku-postgresql:essential-0 -a master-bot-app |
| 94 | + |
| 95 | +# Provision Redis (Mini Tier or Redis Cloud) |
| 96 | +heroku addons:create heroku-redis:mini -a master-bot-app |
| 97 | +``` |
| 98 | + |
| 99 | +> [!NOTE] |
| 100 | +> Heroku automatically populates `DATABASE_URL` and `REDIS_URL` in your application config vars when add-ons are attached. |
| 101 | +
|
| 102 | +### 4. Create `Procfile` |
| 103 | + |
| 104 | +Ensure a `Procfile` exists at the root of your repository with the following process definitions: |
| 105 | + |
| 106 | +```text |
| 107 | +web: pnpm --filter @master-bot/dashboard start |
| 108 | +worker: pnpm --filter @master-bot/bot start |
| 109 | +``` |
| 110 | + |
| 111 | +### 5. Set Config Vars |
| 112 | + |
| 113 | +Set all required Discord and dashboard environment variables: |
| 114 | + |
| 115 | +```bash |
| 116 | +heroku config:set \ |
| 117 | + NODE_ENV=production \ |
| 118 | + DISCORD_TOKEN="your_bot_token" \ |
| 119 | + DISCORD_CLIENT_ID="your_client_id" \ |
| 120 | + DISCORD_CLIENT_SECRET="your_client_secret" \ |
| 121 | + NEXTAUTH_SECRET="generate_random_32_char_secret" \ |
| 122 | + NEXTAUTH_URL="https://master-bot-app.herokuapp.com" \ |
| 123 | + LAVA_ENABLED=true \ |
| 124 | + LAVA_EXTERNAL=true \ |
| 125 | + LAVA_HOST="your-external-lavalink-node.com" \ |
| 126 | + LAVA_PORT=2333 \ |
| 127 | + LAVA_PASS="your_lavalink_password" \ |
| 128 | + -a master-bot-app |
| 129 | +``` |
| 130 | + |
| 131 | +### 6. Deploy Code to Heroku |
| 132 | + |
| 133 | +```bash |
| 134 | +git push heroku main |
| 135 | +``` |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## 🐳 Method B: Docker Container Deployment (`heroku.yml`) |
| 140 | + |
| 141 | +For exact environment parity without buildpack caching issues, you can deploy using Heroku's container runtime. |
| 142 | + |
| 143 | +### 1. Set App Stack to Container |
| 144 | + |
| 145 | +```bash |
| 146 | +heroku stack:set container -a master-bot-app |
| 147 | +``` |
| 148 | + |
| 149 | +### 2. Configure `heroku.yml` |
| 150 | + |
| 151 | +Create `heroku.yml` in the root workspace directory: |
| 152 | + |
| 153 | +```yaml |
| 154 | +setup: |
| 155 | + addons: |
| 156 | + - plan: heroku-postgresql:essential-0 |
| 157 | + as: DATABASE |
| 158 | + - plan: heroku-redis:mini |
| 159 | + as: REDIS |
| 160 | +build: |
| 161 | + docker: |
| 162 | + web: |
| 163 | + dockerfile: Dockerfile |
| 164 | + target: dashboard |
| 165 | + worker: |
| 166 | + dockerfile: Dockerfile |
| 167 | + target: bot |
| 168 | +release: |
| 169 | + command: |
| 170 | + - pnpm --filter @master-bot/db prisma db push |
| 171 | +``` |
| 172 | +
|
| 173 | +### 3. Deploy via Git |
| 174 | +
|
| 175 | +```bash |
| 176 | +git push heroku main |
| 177 | +``` |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## ⚙️ Environment Variables & Config Vars Reference |
| 182 | + |
| 183 | +| Variable | Description | Required | Example | |
| 184 | +| :--- | :--- | :--- | :--- | |
| 185 | +| `DISCORD_TOKEN` | Discord Bot authentication token | Yes | `MTA...` | |
| 186 | +| `DISCORD_CLIENT_ID` | Discord Application ID | Yes | `123456789012345678` | |
| 187 | +| `DISCORD_CLIENT_SECRET` | Discord OAuth2 Client Secret | Yes | `abc123xyz...` | |
| 188 | +| `NEXTAUTH_SECRET` | NextAuth cryptographic session secret | Yes | `openssl rand -base64 32` | |
| 189 | +| `NEXTAUTH_URL` | Canonical URL of the Heroku web dashboard | Yes | `https://my-app.herokuapp.com` | |
| 190 | +| `DATABASE_URL` | Primary PostgreSQL connection string | Yes | Managed by Heroku Postgres | |
| 191 | +| `REDIS_URL` | Redis connection URL | Yes | Managed by Heroku Redis | |
| 192 | +| `LAVA_ENABLED` | Enables audio playback subsystem | Optional | `true` | |
| 193 | +| `LAVA_EXTERNAL` | Declares external Lavalink host | Optional | `true` | |
| 194 | +| `LAVA_HOST` | External Lavalink hostname / IP | If Lava on | `lava.example.com` | |
| 195 | +| `LAVA_PORT` | Lavalink WebSocket port | If Lava on | `2333` | |
| 196 | +| `LAVA_PASS` | Lavalink authentication password | If Lava on | `youshallnotpass` | |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +## 📈 Scaling Dynos |
| 201 | + |
| 202 | +After deploying, scale up the `web` and `worker` dynos: |
| 203 | + |
| 204 | +```bash |
| 205 | +# Enable 1 web dyno (Dashboard) and 1 worker dyno (Discord Bot) |
| 206 | +heroku ps:scale web=1 worker=1 -a master-bot-app |
| 207 | +``` |
| 208 | + |
| 209 | +To verify running dynos: |
| 210 | + |
| 211 | +```bash |
| 212 | +heroku ps -a master-bot-app |
| 213 | +``` |
| 214 | + |
| 215 | +--- |
| 216 | + |
| 217 | +## 🗄️ Database Synchronization |
| 218 | + |
| 219 | +To push your Prisma schema changes directly to Heroku Postgres: |
| 220 | + |
| 221 | +```bash |
| 222 | +heroku run pnpm --filter @master-bot/db prisma db push -a master-bot-app |
| 223 | +``` |
| 224 | + |
| 225 | +--- |
| 226 | + |
| 227 | +## 🎵 Lavalink & Audio Hosting Considerations |
| 228 | + |
| 229 | +> [!IMPORTANT] |
| 230 | +> **Recommended Audio Architecture:** |
| 231 | +> Heroku dynos restart at least once every 24 hours (dyno cycling) and do not support raw UDP voice traffic routing on standard web ports. For optimal, uninterrupted 24/7 music playback: |
| 232 | +> 1. Set `LAVA_EXTERNAL=true` on Heroku. |
| 233 | +> 2. Host `Lavalink.jar` on a cheap standalone VPS (e.g., Hetzner, DigitalOcean, Oracle Cloud) or use a managed Lavalink provider. |
| 234 | +> 3. Point `LAVA_HOST`, `LAVA_PORT`, and `LAVA_PASS` on Heroku to your external Lavalink instance. |
| 235 | +
|
| 236 | +--- |
| 237 | + |
| 238 | +## 📜 Monitoring & Logs |
| 239 | + |
| 240 | +Stream live logs from all dynos in real time: |
| 241 | + |
| 242 | +```bash |
| 243 | +# Stream combined logs |
| 244 | +heroku logs --tail -a master-bot-app |
| 245 | + |
| 246 | +# Filter logs for the Discord bot worker only |
| 247 | +heroku logs --tail --ps worker -a master-bot-app |
| 248 | + |
| 249 | +# Filter logs for the Next.js Dashboard web server only |
| 250 | +heroku logs --tail --ps web -a master-bot-app |
| 251 | +``` |
| 252 | + |
| 253 | +--- |
| 254 | + |
| 255 | +## 🔄 Restarting & Troubleshooting |
| 256 | + |
| 257 | +* **Restart App**: `heroku restart -a master-bot-app` |
| 258 | +* **Run Interactive Shell**: `heroku run bash -a master-bot-app` |
| 259 | +* **Check Dyno Status**: `heroku ps -a master-bot-app` |
0 commit comments