| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago | 1 | # One Cloudflare Pages project + custom domain + DNS record per language, |
| 2 | # matching blog/hugo.toml's multihost [languages.*] baseURLs: each language |
| 3 | # is a fully separate site (public/<lang>/ from the Hugo build) deployed to |
| 4 | # its own subdomain, with English at the zone apex. |
| Derive Makefile and Terraform language sets from one file 9349201 Romain Gautier 7h ago | 5 | # |
| 6 | # The language codes themselves come from ../languages.txt, the same file |
| 7 | # the Makefile reads for its deploy fan-out, so the two never drift apart. |
| 8 | # blog/hugo.toml's [languages.*] blocks can't read that file too (Tofu has |
| 9 | # no TOML parser to return the favor) - keep that one in sync by hand. |
| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago | 10 | locals { |
| Derive Makefile and Terraform language sets from one file 9349201 Romain Gautier 7h ago | 11 | language_codes = split(" ", trimspace(file("${path.module}/../languages.txt"))) |
| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago | 12 | languages = { |
| Derive Makefile and Terraform language sets from one file 9349201 Romain Gautier 7h ago | 13 | for code in local.language_codes : code => { |
| 14 | hostname = code == "en" ? var.zone_name : "${code}.${var.zone_name}" |
| 15 | project_name = "mykiwi-blog-${code}" |
| 16 | } |
| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago | 17 | } |
| 18 | } |
| 19 | |
| 20 | data "cloudflare_zone" "this" { |
| 21 | name = var.zone_name |
| 22 | } |
| 23 | |
| 24 | # Direct-upload projects (no `source` block, i.e. no git integration) - |
| 25 | # deploys happen out-of-band, e.g. `wrangler pages deploy public/en |
| 26 | # --project-name=mykiwi-blog-en` from CI, since this repo lives on |
| 27 | # tangled.sh rather than GitHub. |
| 28 | resource "cloudflare_pages_project" "site" { |
| 29 | for_each = local.languages |
| 30 | |
| 31 | account_id = var.cloudflare_account_id |
| 32 | name = each.value.project_name |
| 33 | production_branch = "main" |
| 34 | } |
| 35 | |
| 36 | resource "cloudflare_pages_domain" "site" { |
| 37 | for_each = local.languages |
| 38 | |
| 39 | account_id = var.cloudflare_account_id |
| 40 | project_name = cloudflare_pages_project.site[each.key].name |
| 41 | domain = each.value.hostname |
| 42 | } |
| 43 | |
| 44 | resource "cloudflare_record" "site" { |
| 45 | for_each = local.languages |
| 46 | |
| 47 | zone_id = data.cloudflare_zone.this.id |
| 48 | name = each.key == "en" ? "@" : each.key |
| 49 | type = "CNAME" |
| 50 | content = cloudflare_pages_project.site[each.key].subdomain |
| 51 | proxied = true |
| 52 | } |
| 53 | |
| 54 | # www.mykiwi.blog -> mykiwi.blog (English apex), not its own language - just |
| 55 | # the one common typo/habit redirect, not a 5th site. |
| 56 | resource "cloudflare_record" "www" { |
| 57 | zone_id = data.cloudflare_zone.this.id |
| 58 | name = "www" |
| 59 | type = "CNAME" |
| 60 | content = var.zone_name |
| 61 | proxied = true |
| 62 | } |
| 63 | |
| Migrate www redirect from Page Rules to Single Redirects b9bda7a Romain Gautier 9h ago | 64 | # Page Rules would also do this but its Edit permission only shows up in |
| 65 | # the API token builder for account Super Administrators - Single Redirects |
| 66 | # (this resource) works with a normal scoped token (Zone:Dynamic Redirect:Edit). |
| 67 | resource "cloudflare_ruleset" "www_redirect" { |
| 68 | zone_id = data.cloudflare_zone.this.id |
| 69 | name = "www to apex redirect" |
| 70 | description = "www.${var.zone_name} -> ${var.zone_name}, path preserved" |
| 71 | kind = "zone" |
| 72 | phase = "http_request_dynamic_redirect" |
| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago | 73 | |
| Migrate www redirect from Page Rules to Single Redirects b9bda7a Romain Gautier 9h ago | 74 | rules { |
| 75 | ref = "www_redirect" |
| 76 | description = "Redirect www to apex" |
| 77 | expression = "(http.host eq \"www.${var.zone_name}\")" |
| 78 | action = "redirect" |
| 79 | |
| 80 | action_parameters { |
| 81 | from_value { |
| 82 | status_code = 301 |
| 83 | target_url { |
| 84 | expression = "concat(\"https://${var.zone_name}\", http.request.uri.path)" |
| 85 | } |
| 86 | preserve_query_string = true |
| 87 | } |
| Add Cloudflare provisioning and deploy tooling e4c5d34 Romain Gautier 13h ago | 88 | } |
| 89 | } |
| 90 | } |