# Deployment guide — Hetzner Cloud ``` GitHub Actions → DockerHub → terraform apply → live HTTPS app ``` All secrets and config are passed through Terraform. There is no manual SSH step to set up the server — `terraform apply` provisions the machine, writes every config file, and starts the containers automatically. --- ## Prerequisites | Tool | Install | |------|---------| | [Terraform](https://developer.hashicorp.com/terraform/install) ≥ 1.5 | `brew install terraform` | | [Docker](https://docs.docker.com/get-docker/) | desktop or CLI | | A [Hetzner Cloud](https://www.hetzner.com/cloud/) account | free to sign up | | A [Docker Hub](https://hub.docker.com/) account | free | | A domain name with DNS you control | any registrar | --- ## 1. Docker Hub — create a repository and access token 1. Log into [hub.docker.com](https://hub.docker.com/) 2. **Create Repository** → name it `at-tracker` 3. **Account Settings → Security → New Access Token** - Description: `github-actions`, permissions: **Read, Write, Delete** - Save the token — shown only once --- ## 2. GitHub — add CI secrets **Settings → Secrets and variables → Actions → New repository secret:** | Secret | Value | |--------|-------| | `DOCKERHUB_USERNAME` | your Docker Hub username | | `DOCKERHUB_TOKEN` | the access token from step 1 | Push to `main` (or trigger **Actions → Build & push Docker image → Run workflow**) and wait for it to go green. The image must exist on Docker Hub before you run `terraform apply`. --- ## 3. Hetzner Cloud — create an API token 1. [console.hetzner.cloud](https://console.hetzner.cloud) → your project 2. **Security → API Tokens → Generate API Token** - Permission: **Read & Write** 3. Copy the token immediately --- ## 4. Terraform — configure and apply ```bash cd infra cp terraform.tfvars.example terraform.tfvars ``` Edit `terraform.tfvars` — every value is required: ```hcl hcloud_token = "your_hetzner_api_token" docker_image = "yourdockerhubuser/at-tracker:latest" domain = "tracker.yourdomain.com" postgres_password = "a_long_random_password" ``` Then: ```bash terraform init terraform plan # review what will be created terraform apply ``` Terraform creates the SSH key, firewall, and server. The server's cloud-init script runs on first boot (~3 min) and: - Installs Docker - Writes `/opt/at-tracker/.env`, `docker-compose.yml`, and `Caddyfile` - Runs `docker compose pull && docker compose up -d` The server IP is printed in the outputs: ``` server_ip = "1.2.3.4" ssh_command = "ssh root@1.2.3.4" ``` --- ## 5. DNS — point your domain at the server Create an **A record** at your DNS provider: | Type | Host | Value | |------|------|-------| | A | `tracker` (or `@`) | `1.2.3.4` | Caddy will not obtain a TLS certificate until DNS resolves. You can check propagation with: ```bash dig tracker.yourdomain.com +short ``` --- ## 6. Verify Once DNS resolves, Caddy obtains the TLS cert automatically (usually within 30 seconds of the first request). Visit `https://tracker.yourdomain.com` — you should see the collection tracker table. To watch the startup sequence on the server: ```bash ssh root@1.2.3.4 # Check cloud-init finished cloud-init status # Watch live logs cd /opt/at-tracker && docker compose logs -f ``` You should see: - `caddy` — `certificate obtained successfully` - `app` — `Connected to Jetstream` - `app` — `Flushed N names` every few seconds --- ## Redeploying after a code change ```bash # 1. Push to main — GitHub Actions builds and pushes the new image automatically. # 2. Run the deploy script from your local machine (in the repo root). # This syncs docker-compose.yml and Caddyfile, then pulls and restarts the app. # Postgres and Caddy keep running — no downtime, no cert re-issue. DEPLOY_HOST=root@1.2.3.4 ./deploy.sh # Or pass the host directly: ./deploy.sh root@1.2.3.4 ``` The script always syncs `docker-compose.yml` and `Caddyfile` from the repo to the server before restarting, so config file changes are picked up automatically — no manual SSH editing required. If you only want to update the app image without touching config: ```bash ssh root@1.2.3.4 cd /opt/at-tracker docker compose pull app docker compose up -d --no-deps app ``` --- ## Testing locally before deploying You can run the full stack on your machine without the server: ```bash # Build the image locally docker build -t at-tracker:local . # Create a local .env cp .env.example .env.local # Edit .env.local: # DOCKER_IMAGE=at-tracker:local # DOMAIN=localhost ← Caddy won't do TLS for localhost; see note below # POSTGRES_PASSWORD=localdev # Start (without Caddy — hit the app directly on port 3000) DATABASE_URL=postgres://attracker:localdev@localhost:5432/attracker \ docker compose -f docker-compose.yml \ --env-file .env.local \ up postgres app ``` The app is then available at `http://localhost:3000`. > Caddy is omitted in local testing because it requires a real domain and > publicly routable port 80/443 to obtain a TLS certificate. For local HTTPS > you can use [mkcert](https://github.com/FiloSottile/mkcert) and a custom > Caddyfile, but for development `http://localhost:3000` is sufficient. --- ## Maintenance commands ```bash # Live logs docker compose logs -f # Restart everything docker compose restart # Connect to Postgres docker compose exec postgres psql -U attracker -d attracker # Check collection stats docker compose exec postgres psql -U attracker -d attracker \ -c "SELECT COUNT(*) AS types, SUM(count) AS total_records FROM collection_stats;" # Top 10 most-seen collections docker compose exec postgres psql -U attracker -d attracker \ -c "SELECT name, count FROM collection_stats ORDER BY count DESC LIMIT 10;" ``` --- ## Cost estimate | Resource | Spec | Price | |----------|------|-------| | Hetzner CX22 | 2 vCPU / 4 GB RAM / 40 GB SSD | ~€4.35/mo | | IPv4 address | 1 static public IP | ~€0.50/mo | | Bandwidth | 20 TB included | €0 | | Docker Hub | public image | $0 | | **Total** | | **~€4.85/mo** | --- ## File reference ``` infra/ ├── versions.tf Terraform provider pinning ├── variables.tf All input variables with descriptions ├── main.tf SSH key, firewall, server; templatefile() renders cloud-init ├── outputs.tf server_ip, ipv6, ssh_command ├── cloud-init.yaml.tpl Template: writes .env + compose files, installs Docker, starts app └── terraform.tfvars.example Copy → terraform.tfvars (gitignored), fill in secrets Caddyfile Reverse proxy config; reads $DOMAIN from .env docker-compose.yml caddy + app (DockerHub image) + postgres .env.example Reference only — actual .env is written by Terraform .github/workflows/ └── docker-publish.yml Builds and pushes image to Docker Hub on push to main ```