# Deploying and updating

> First install: see [INSTALL.md](../INSTALL.md). This page covers automatic updates from GitHub.

## How updates reach your server

```
Claude ──(pull request)──► GitHub: FarmerButcher/meat-brand ──(you merge)──► main
                                                                              │
                                                      GitHub Actions: tests ──┤
                                                                              ▼
                                         SSH into your VPS ──► scripts/deploy.sh
                                         (backup → build → migrate → restart → health check)
```

* Claude never has access to your server. Claude has access to the **GitHub repo** only.
* Claude works on a branch and opens a **pull request**. You read it and press **Merge**.
* Merging to `main` runs the tests. If they pass, GitHub logs into your server with a
  deploy key **you** created and stored in GitHub, and runs `scripts/deploy.sh`.
* Every deploy takes a database backup first, and stops if the new version doesn't come up healthy.

You can keep a `staging` branch that deploys to a second, cheaper VPS. Merge there first and check it.

## 1. First install (once)

**Server:** Ubuntu 24.04, 4 GB RAM minimum (8 GB recommended: builds run on the box).

**DNS** (at your domain registrar), all pointing at the server's IP:

| Type | Name | Value |
|---|---|---|
| A | `@` (themeatbrand.com) | server IP |
| A | `www` | server IP |
| A | `app` | server IP |
| A | `*` (wildcard, for client preview sites) | server IP |
| A | `sites` (the EDGE_TARGET clients CNAME to) | server IP |

**Server access to the private repo (read-only deploy key):**

```bash
ssh root@your-server
ssh-keygen -t ed25519 -f ~/.ssh/github_deploy -N ""
cat ~/.ssh/github_deploy.pub
```
GitHub → repo → Settings → Deploy keys → Add → paste → **leave "Allow write access" off**.

```bash
cat >> ~/.ssh/config <<'EOF'
Host github.com
  IdentityFile ~/.ssh/github_deploy
EOF
git clone git@github.com:FarmerButcher/meat-brand.git /opt/meat-brand
cd /opt/meat-brand
sudo bash scripts/setup-server.sh     # first run creates .env and stops
nano .env                              # set PLATFORM_DOMAIN, EDGE_TARGET, ACME_EMAIL, BACKUP_S3_*
sudo bash scripts/setup-server.sh     # second run builds, migrates and starts
```

(Prefer uploading the zip instead? Unzip it to `/opt/meat-brand` and run the same two
commands. But automatic updates need the `git clone` route, so switch to it before step 2.)

Create your platform admin account (prompts for a password):

```bash
cd /opt/meat-brand
docker compose --profile tools run --rm -it migrate node /repo/apps/api/dist/cli/create-admin.js you@example.com
```

Sign in at `https://app.<domain>/login`, set up two-factor, then open `/admin`.

## 2. Turn on automatic deploys (once)

On your own computer:

```bash
ssh-keygen -t ed25519 -f gha_deploy -N ""        # key GitHub uses to log into the server
ssh-copy-id -i gha_deploy.pub root@your-server
ssh-keyscan your-server                          # copy the output
```

GitHub → repo → Settings → Environments → **New environment** `production` → add secrets:

| Secret | Value |
|---|---|
| `SSH_HOST` | server IP or hostname |
| `SSH_USER` | `root` (or a deploy user in the `docker` group) |
| `SSH_PRIVATE_KEY` | contents of `gha_deploy` (the private one) |
| `SSH_KNOWN_HOSTS` | output of `ssh-keyscan` |
| `APP_DIR` | `/opt/meat-brand` |

Settings → Secrets and variables → Actions → **Variables** → `DEPLOY_ENABLED` = `true`.
Optionally protect `production` with "Required reviewers" so every deploy needs your click.

Delete `gha_deploy` from your computer once it's stored in GitHub.

## 3. Manual deploy / rollback

```bash
cd /opt/meat-brand
git fetch && git checkout --detach origin/main && bash scripts/deploy.sh   # deploy latest
git checkout --detach <older-commit> && bash scripts/deploy.sh             # roll back code
```

Migrations are forward-only and must stay compatible with the previous code version
(add columns/tables first, remove them in a later release), so rolling back code is safe.
Rolling back the database means restoring a backup (docs/OPERATIONS.md).
