BS-001 Initialize BortStack foundation and project workflow

This commit is contained in:
rob
2026-11-10 14:02:50 +00:00
parent 1ec90a082f
commit 52265961cb
119 changed files with 10298 additions and 64 deletions
+118
View File
@@ -0,0 +1,118 @@
# Production Deployment
## Architecture
The supported production pattern is a Hetzner-hosted Docker Compose project:
1. LinuxServer SWAG runs as the existing shared TLS reverse proxy on an external
Docker network (default `swagnet`).
2. The template's nginx container joins that network, serves the built React SPA,
and proxies `/api/` to the private API container.
3. Gunicorn serves Flask in the non-root API container.
4. MySQL persists to a named volume and is reachable only on the internal network.
5. Resend provides outbound transactional account/contact email from the generated project's `@roborto.net` product identity when explicitly enabled.
6. Cloudflare Email Routing receives mail sent to that product address and forwards it to the captain's private mail server; this inbound path is provisioned outside the repository.
7. GitHub Actions validates pull requests; pushes to `production` publish images
tagged with the tested commit SHA and deploy that exact commit and image set.
## Server prerequisites
- Docker Engine with the Compose plugin.
- A running SWAG stack and external Docker network matching `SWAG_NETWORK`.
- A clean clone on branch `production` at the path stored in the GitHub variable
`PRODUCTION_DEPLOY_PATH`.
- A mode-`600` production environment file outside the repository at the path in
`PRODUCTION_ENV_PATH`.
- A deployment user able to run Docker without an interactive prompt and pull
the repository's GHCR packages.
- A verified SSH host key and dedicated deployment key.
Use `.env.example` only as a variable-name reference. Generate unique secrets and
never commit the production file.
## Product email commissioning
The generator derives a product mailbox from the project slug. For example, a project named `Example Product` receives `example-product@roborto.net` as both the example Resend sender address and contact destination. The visible sender name remains independently configurable in `EMAIL_FROM`.
Before setting `EMAIL_DELIVERY_ENABLED=true` in production:
1. Confirm the generated address and reserve it for the product.
2. Verify the required `roborto.net` sending identity and DNS records with Resend using the captain's existing domain-management process.
3. Create the matching Cloudflare Email Routing rule and point it to the intended mailbox on the captain's private mail system.
4. Confirm inbound delivery from an unrelated external account; a successful Resend API call does not prove inbound routing works.
5. Send verification and password-reset messages to test users and confirm links use the production `APP_URL`.
6. Submit the public contact form, confirm delivery to the product mailbox, and verify that replying targets the visitor address supplied through `Reply-To`.
7. Record the sender identity, routing owner, destination owner, and test date without storing API keys, mailbox credentials, or DNS secrets in the repository.
Resend handles application-originated outbound mail; Cloudflare Email Routing handles inbound mail. The application does not host an SMTP receiver, read the destination mailbox, or provision either provider. Keep `RESEND_API_KEY` and all private-mail-server credentials outside Git.
## GitHub configuration
Configure these Actions secrets:
- `PRODUCTION_SSH_HOST`
- `PRODUCTION_SSH_USER`
- `PRODUCTION_SSH_PRIVATE_KEY`
- `PRODUCTION_SSH_KNOWN_HOSTS`
Configure these repository or `production` environment variables:
- `PRODUCTION_DEPLOY_PATH`, for example `/home/deploy/example-app`
- `PRODUCTION_ENV_PATH`, for example `/opt/example-app/.env`
- `PRODUCTION_PUBLIC_URL`, for example `https://app.example.com`
The workflow refuses a dirty remote checkout or a branch other than `production`,
fast-forwards to the tested commit, runs migrations from the immutable API image,
and checks the local frontend plus the public health endpoint. Protect the
`production` environment/branch where the GitHub plan permits it.
## SWAG
Adapt `ops/swag/app.subdomain.conf.sample` to the desired SWAG hostname convention.
SWAG should proxy to the Compose `nginx` service on port 80 over the shared network.
Do not expose MySQL. Loopback-bound ports 8081 and 5001 are provided only for
server-local diagnostics.
## Manual first deployment
1. Copy `.env.example` to the external production path and replace every example.
2. Create the configured MySQL database volume and external SWAG network through
the normal Compose/SWAG lifecycle.
3. Run `docker compose --env-file /path/to/.env config --quiet`.
4. Start MySQL, run `flask --app api.py db upgrade` from the API image, then start
the API and nginx services.
5. Confirm `/api/health/live` and `/api/health/ready` locally and through the public URL.
## Health and logs
- `/api/health` and `/api/health/live` prove that Flask can serve a request; they do not query external dependencies.
- `/api/health/ready` executes a minimal database query and returns `503` without database details when persistence is unavailable. Compose and deployment verification use this readiness endpoint.
- Application events use one-line JSON on standard error with timestamp, severity, logger, and generic message. Account tokens, request bodies, contact contents, email addresses, credentials, and provider/database exception details must not be added to logs.
- Container logs remain the default collection boundary. Add a host-level retention/forwarding policy before production; this template does not select an external observability vendor.
Use liveness for process restarts and readiness for routing/deployment gates. A healthy probe does not prove registration, email delivery, authentication, or every migration is correct.
## Deployment failure and rollback
The workflow stops on failed configuration, pull, migration, service update, or health verification. Because database migrations may be forward-only, application rollback and schema rollback are separate decisions.
Before each production deployment:
1. Confirm a recent recoverable database backup and record the current commit and immutable image tags.
2. Review the pending migration upgrade and downgrade operations against disposable data.
3. Confirm the previous API/nginx image tags remain pullable.
For an application-only rollback, set `API_IMAGE` and `NGINX_IMAGE` to the previously verified commit tags in the external environment/deployment invocation, restore the corresponding clean Git commit, run `docker compose config --quiet`, and update the API/nginx services. Do not run a database downgrade automatically.
If the previous application is incompatible with the migrated schema, keep the new application stopped or in maintenance mode while following a migration-specific recovery plan. Restore into disposable infrastructure first; never overwrite the production volume merely because a deployment health check failed.
## Security and proxy boundaries
- SWAG is the sole public TLS boundary. MySQL is internal-only; diagnostic API/nginx ports bind to loopback.
- Nginx preserves the trusted upstream `X-Forwarded-Proto` value from SWAG and replaces client forwarding headers at the application boundary. Flask trusts exactly one nginx proxy hop.
- Production startup rejects wildcard CORS, short secret keys, SQLAlchemy query logging, and insecure `SameSite=None` cookie configuration.
- Nginx applies frame, MIME-sniffing, referrer, and browser-permission headers. Add a project-specific Content Security Policy only after inventorying required scripts, styles, fonts, frames, and external origins.
- Review host firewalling, Docker daemon access, SSH key scope, GHCR package access, log retention, disk alerts, resource limits, and security updates before launch.
Production provisioning, Resend identity verification, and DNS/Cloudflare changes remain deliberate operator
actions; this repository does not create or mutate external infrastructure.