diff --git a/README.md b/README.md index df5bc8d..bb01192 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ MaroonRides infrastructure: one Contabo VPS (`prod-server`) running NixOS and a single-node k3s cluster (`prod`) reconciled by Flux. - `tofu/`: Contabo instance + NixOS install, `maroonrides.app` DNS records, + the `web.maroonrides.app` Pages project and its GitHub deploy secrets, Cloudflare tokens, R2 backup bucket, Tailscale policy, Flux webhooks - `nixos/`: host configuration - `kubernetes/`: Flux tree. `apps/base` is the platform, `apps/prod` the apps @@ -16,12 +17,12 @@ State is `infra.tfstate` in the `maroonrides-tofu-state` R2 bucket. `tofu/.env` Item `tofu-credentials` in the `Maroon Rides Deployment` 1Password vault: -| Section | Fields | Permissions | -| ------------ | ---------------------------------------------------- | ---------------------------------------------------------------------------- | -| `cloudflare` | `api_token` | Account: API Tokens Edit, Workers R2 Storage Edit; zone: Zone Read, DNS Edit | -| `github` | `token` | `Maroon-Rides/infra`: Webhooks read/write | -| `contabo` | `client_id`, `client_secret`, `username`, `password` | Contabo API | -| `tailscale` | `client_id`, `client_secret` | OAuth client, all scopes | +| Section | Fields | Permissions | +| ------------ | ---------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | +| `cloudflare` | `api_token` | Account: API Tokens Edit, Workers R2 Storage Edit, Cloudflare Pages Edit; zone: Zone Read, DNS Edit | +| `github` | `token` | `Maroon-Rides/infra`: Webhooks read/write; `Maroon-Rides/MaroonRides`: Secrets and Variables read/write | +| `contabo` | `client_id`, `client_secret`, `username`, `password` | Contabo API | +| `tailscale` | `client_id`, `client_secret` | OAuth client, all scopes | ## Bring-up diff --git a/tofu/config/main.tf b/tofu/config/main.tf index ac8ea18..d18619c 100644 --- a/tofu/config/main.tf +++ b/tofu/config/main.tf @@ -34,3 +34,13 @@ module "flux_webhooks" { domain = local.domain vault_uuid = local.vault_uuid } + +module "pages_web" { + source = "../modules/pages-web" + + account_id = local.account_id + zone_id = local.zone_id + domain = local.domain + project_name = "maroonrides-web" + repository = "MaroonRides" +} diff --git a/tofu/modules/pages-web/main.tf b/tofu/modules/pages-web/main.tf new file mode 100644 index 0000000..db494ac --- /dev/null +++ b/tofu/modules/pages-web/main.tf @@ -0,0 +1,60 @@ +locals { + hostname = "web.${var.domain}" +} + +// Deployments are direct uploads from GitHub Actions, so the project has no git source. +resource "cloudflare_pages_project" "web" { + account_id = var.account_id + name = var.project_name + production_branch = "main" +} + +resource "cloudflare_pages_domain" "web" { + account_id = var.account_id + project_name = cloudflare_pages_project.web.name + name = local.hostname +} + +resource "cloudflare_dns_record" "web" { + zone_id = var.zone_id + name = local.hostname + type = "CNAME" + content = cloudflare_pages_project.web.subdomain + proxied = true + ttl = 1 +} + +resource "cloudflare_account_token" "pages_deploy" { + account_id = var.account_id + name = "maroonrides pages deploy" + + policies = [ + { + effect = "allow" + permission_groups = [ + { id = "8d28297797f24fb8a0c332fe0866ec89" }, # Pages Write + ] + resources = jsonencode({ + "com.cloudflare.api.account.${var.account_id}" = "*" + }) + } + ] +} + +resource "github_actions_secret" "cloudflare_api_token" { + repository = var.repository + secret_name = "CLOUDFLARE_API_TOKEN" + plaintext_value = cloudflare_account_token.pages_deploy.value +} + +resource "github_actions_variable" "cloudflare_account_id" { + repository = var.repository + variable_name = "CLOUDFLARE_ACCOUNT_ID" + value = var.account_id +} + +resource "github_actions_variable" "pages_project" { + repository = var.repository + variable_name = "CLOUDFLARE_PAGES_PROJECT" + value = cloudflare_pages_project.web.name +} diff --git a/tofu/modules/pages-web/variables.tf b/tofu/modules/pages-web/variables.tf new file mode 100644 index 0000000..70c9f78 --- /dev/null +++ b/tofu/modules/pages-web/variables.tf @@ -0,0 +1,19 @@ +variable "account_id" { + type = string +} + +variable "zone_id" { + type = string +} + +variable "domain" { + type = string +} + +variable "project_name" { + type = string +} + +variable "repository" { + type = string +} diff --git a/tofu/modules/pages-web/versions.tf b/tofu/modules/pages-web/versions.tf new file mode 100644 index 0000000..28553c1 --- /dev/null +++ b/tofu/modules/pages-web/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + } + github = { + source = "integrations/github" + } + } +}