# Deployment and configuration Pick a platform, then use this page to set up file storage, updates and licensing. ## Platforms - [Cloudflare](deploy-cloudflare.md) - [Docker](deploy-docker.md) - [Vercel](deploy-vercel.md) ## Object storage: R2, Backblaze B2 or AWS S3 All files (raw mail, attachments, Drive, avatars, branding icons, JMAP uploads, backups) live in one bucket. The default is the `BUCKET` R2 binding in `wrangler.jsonc`. **Backblaze B2.** Set all four, as Worker secrets or in `.dev.vars` locally: | Variable | Example | Purpose | | --- | --- | --- | | `B2_KEY_ID` | `004abc...` | Application key ID | | `B2_APPLICATION_KEY` | `K004...` | Application key secret | | `B2_BUCKET` | `mailflare` | Bucket name | | `B2_ENDPOINT` | `s3.us-west-004.backblazeb2.com` | S3-compatible endpoint from the bucket page; sets the region | ```bash npx wrangler secret put B2_KEY_ID npx wrangler secret put B2_APPLICATION_KEY npx wrangler secret put B2_BUCKET npx wrangler secret put B2_ENDPOINT ``` **AWS S3.** Set `S3_BUCKET`, `S3_REGION`, `S3_ACCESS_KEY_ID` and `S3_SECRET_ACCESS_KEY`. The two keys fall back to `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`, so one IAM user can serve SES and storage. The user needs `s3:GetObject`, `s3:PutObject`, `s3:DeleteObject` and `s3:AbortMultipartUpload` on `arn:aws:s3:::/*`. Rules: - **Switching does not copy data.** Copy existing objects first, with the same keys (for example with `rclone`). Otherwise old mail, attachments and Drive files show as missing. - If B2 and S3 are both set, B2 wins. - A complete set replaces R2 everywhere. A partial set keeps R2 and is reported by `GET /api/v1/storage`. The R2 binding can stay. - Keep the bucket private, in the named region. Use a key limited to that bucket with read and write access. - Set a lifecycle rule that keeps only the last version and deletes unfinished large files after a few days. - Drive uploads buffer one 64 MB part at a time. B2 and S3 require at least 5 MB for every part but the last. Test storage with `POST /api/v1/storage` (admin key with the `storage` scope) or the `manage_storage` MCP tool with `action: "test"`. It writes, reads and deletes a small object. ## Updating Mailflare 1. Select **Update Mailflare** in the admin dashboard. 2. Wait for the GitHub Action, then for the Cloudflare deployment. The new version is live only after Cloudflare finishes. 3. Open or refresh **Admin settings**. If migrations are pending, select **Update database**. > **Warning:** An update replaces the whole tracked file tree of your installation branch. Your own code changes and extra committed files are removed. Repository variables, secrets and other GitHub or Cloudflare settings stay. The button runs `.github/workflows/deploy-update.yml` in your repository. It commits the latest published release (or, on the commit channel, the offered commit) and pushes it. Cloudflare's Git integration then deploys it. An older workflow without a `target` input runs once without it, which installs the new workflow. ### Set up the update button 1. Create a fine-grained GitHub token for your installation repository only: | Permission | Access | Used to | | --- | --- | --- | | Actions | Read and write | Start the update workflow | | Contents | Read and write | Commit and push the new source | | Workflows | Read and write | Replace files in `.github/workflows` | 2. Add these values: | Location | Name | Type | Value | | --- | --- | --- | --- | | Cloudflare Worker | `GITHUB_UPDATE_TOKEN` | Secret | The token | | Cloudflare Worker | `GITHUB_UPDATE_REPO` | Variable | Your repository, `owner/repository` | | Cloudflare Worker | `GITHUB_UPDATE_REF` | Variable, optional | Branch to update; default branch if unset | | GitHub → Actions | `MAILFLARE_UPDATE_TOKEN` | Secret | The same token | | GitHub → Actions | `UPDATE_SOURCE_REPOSITORY` | Variable, optional | Upstream; default `hieunc229/mailflare` | 3. Check that `.github/workflows/deploy-update.yml` exists on the branch. If it is missing or older, copy the [current workflow](https://github.com/hieunc229/mailflare/blob/main/.github/workflows/deploy-update.yml) once by hand. A workflow cannot install itself. ### Database migrations Deploying and migrating are separate steps. - If your Cloudflare deploy command contains `wrangler d1 migrations apply DB --remote`, remove it and use `npm run deploy`. - Each migration runs in one batch. On failure it is rolled back, the filename is shown, and you can retry. - **Apply migrations soon after a deploy.** Incoming mail that needs a missing table or column is retried every 5 minutes, up to 12 times (about an hour). Other failures back off for up to about four hours. After the last attempt, the Worker copies the email to `dead/` and keeps the original for any delivered message that still references it. If the copy fails, Cloudflare sends the job to `mailflare-inbound-dlq`. Use the job’s `rawR2Key` to find the original and requeue it after fixing the problem. Cloudflare orphan cleanup keeps the original for at least 29 days, covering both queues’ retention windows. Automatic updates apply migrations from the 5-minute cron. - The self-hosted server applies migrations at startup, so it does not have this problem. ### Writing migrations (contributors) - Add a new SQL file in `drizzle/migrations`. Never edit an applied one. See [migrations](databases.md#migrations) for the other databases. - Build and dev commands bundle the files; `npm run db:bundle` does it on demand. - New releases must work with the old schema until an admin migrates. Prefer additive changes, keep old columns for a while, and do not make sign-in or admin settings depend on a new column. Plan maintenance for incompatible changes. ## Branding license Apply all migrations, then activate a Pro or Team key under **Admin → Licenses**. Mailflare sends the key to Paymug and stores only a one-way hash and the activation state. ## Related - [Troubleshooting](troubleshooting.md) - [Providers](providers.md) - [Databases](databases.md)