From 3a7fd86d030744d1206523565fc3f9510d72556b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Wed, 2 Dec 2026 21:14:30 -0300 Subject: [PATCH] ddev: serve Mailpit on a worktree's variant hostnames init writes a project Traefik config that routes the variants' Mailpit ports to web's Mailpit. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 7 ++++++ ddev/commands/host/hostshift | 43 +++++++++++++++++++++++++++++++++++- ddev/install.yaml | 2 ++ test/addon-command.sh | 23 +++++++++++++++++++ test/integration-ddev.sh | 5 +++++ 5 files changed, 79 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f15f4a7..341ba75 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,13 @@ serves the worktree and `https://acme.ddev.site` goes on serving the parent. service's `VIRTUAL_HOST` into the mkcert SAN list too — verified on v1.25.2 with no hostshift config file on disk. Everything not named there keeps going to `web`, so the canonical site stays reachable alongside the variant. +- **Mailpit.** `https://wt-a--acme.ddev.site:8026` is the worktree's own + Mailpit, so a test that reads mail from its base URL works on a variant. The + variants route to the proxy on 80 and 443 only, so `init` writes + `.ddev/traefik/config/hostshift-mailpit.yaml`, which DDEV merges into the + project's router config and which sends the Mailpit ports to `web`. The proxy + is not involved. After updating the add-on, run `ddev hostshift init` once (a + worktree's pre-start hook does it on the next start). - **Multiple hostnames.** A multisite needs nothing extra. If the parent declares `additional_hostnames`, `init` derives a variant for each one. - **Keeping the two projects apart.** `HOSTSHIFT_WEB_HOSTS` narrows `web` back diff --git a/ddev/commands/host/hostshift b/ddev/commands/host/hostshift index 2e2322d..2af853a 100755 --- a/ddev/commands/host/hostshift +++ b/ddev/commands/host/hostshift @@ -2048,7 +2048,7 @@ hostshift: listening on"*) logs_has_banner=1 ;; stale="" for f in .ddev/traefik/config/*.yaml; do [ -f "$f" ] || continue - case "$(basename "$f" .yaml)" in "$name") ;; *) stale="${stale:+$stale }$f" ;; esac + case "$(basename "$f" .yaml)" in "$name"|hostshift-mailpit) ;; *) stale="${stale:+$stale }$f" ;; esac done if [ -n "$stale" ]; then echo " This project carries router config for a name it no longer has:" >&2 @@ -3097,6 +3097,47 @@ cp -p .ddev/.env "$tmp" mv "$tmp" .ddev/.env echo "hostshift: wrote .ddev/.env" >&2 + +# Mailpit on the variants. They are on the hostshift service's VIRTUAL_HOST, +# which exposes 80 and 443 only, so `:8026` matched no router and +# answered 404. DDEV merges every other .yaml in .ddev/traefik/config/ into the +# project's router config, so two routers send those ports to web's Mailpit, +# Host unchanged. +# +# The service is DDEV's own, not a URL of ours: where DDEV chooses not to route +# Mailpit (hardened images, Let's Encrypt) it does not define it, and these +# routers then go nowhere instead of publishing the mail. +# +# No #ddev-generated: DDEV deletes a signed file here that no add-on installed. +# The ports are DDEV's to say, so without them (run by hand) nothing is written. +mailpit=.ddev/traefik/config/hostshift-mailpit.yaml +http_port="${DDEV_MAILPIT_HTTP_PORT:-${DDEV_MAILPIT_PORT:-}}" +https_port="${DDEV_MAILPIT_HTTPS_PORT:-}" +if [ -z "$variants" ]; then + rm -f "$mailpit" +elif [ -n "${DDEV_SITENAME:-}" ] && [ -n "$http_port" ] && [ -n "$https_port" ]; then + rule="$(printf '%s' "$variants" | tr ', ' '\n\n' \ + | awk 'NF { printf "%sHost(`%s`)", (n++ ? " || " : ""), $0 }')" + mkdir -p "${mailpit%/*}" + cat > "$mailpit" <&2 # And pick it up. init always leaves the project stale — that is what it is for — diff --git a/ddev/install.yaml b/ddev/install.yaml index a3e35fa..768d5fb 100644 --- a/ddev/install.yaml +++ b/ddev/install.yaml @@ -296,6 +296,8 @@ removal_actions: # Paths relative to .ddev/, which is the cwd DDEV runs these actions in — # documented at post_install_actions above, and got wrong here, which made # this whole list dead on every removal. `..` is the project root. + # Ours alone, and it routes hostnames nothing serves once the add-on is gone. + rm -f traefik/config/hostshift-mailpit.yaml left="" [ -f docker-compose.hostshift-loopback.yaml ] && left="$left .ddev/docker-compose.hostshift-loopback.yaml — pins production hostnames to 127.0.0.1 in web" diff --git a/test/addon-command.sh b/test/addon-command.sh index e6d8d3c..5aefd22 100755 --- a/test/addon-command.sh +++ b/test/addon-command.sh @@ -2739,6 +2739,29 @@ contains "it works from a subdirectory" "HOSTSHIFT_VARIANTS=wt-a--acme.ddev.site echo +echo "== mailpit on the variants" + +mp=".ddev/traefik/config/hostshift-mailpit.yaml" +mailpit_init() { + (cd "$wt" && DDEV_SITENAME=acme-wt DDEV_MAILPIT_HTTP_PORT=8025 DDEV_MAILPIT_HTTPS_PORT=8026 \ + "$cmd" init --slug wt-a "$@" >/dev/null 2>&1 || true) +} +mailpit_init --dry-run +[ ! -e "$wt/$mp" ] && pass "--dry-run writes no router file" || fail "--dry-run writes no router file" "$mp exists" +mailpit_init +router="$(cat "$wt/$mp" 2>/dev/null || true)" +contains "the https port is routed" "- http-8026" "$router" +contains "the http port is routed" "- http-8025" "$router" +contains "every variant is matched" 'rule: Host(`wt-a--acme.ddev.site`) || Host(`wt-a--nat.acme.ddev.site`)' "$router" +contains "to DDEV's own mailpit service" "service: acme-wt-web-8025" "$router" +# DDEV deletes a signed file in this directory that no add-on installed. +case "$router" in *"#ddev-generated"*) fail "the router file is not signed as DDEV's" "$router" ;; + *) pass "the router file is not signed as DDEV's" ;; esac +rm -f "$wt/$mp" +(cd "$wt" && "$cmd" init --slug wt-a >/dev/null 2>&1 || true) +[ ! -e "$wt/$mp" ] && pass "without DDEV's ports nothing is written" || fail "without DDEV's ports nothing is written" "$mp exists" + +echo echo "== copy-db --dry-run writes nothing" # copy-db is the one destructive subcommand, and it exits before the file's only diff --git a/test/integration-ddev.sh b/test/integration-ddev.sh index 3beeb9e..0fd1017 100755 --- a/test/integration-ddev.sh +++ b/test/integration-ddev.sh @@ -210,6 +210,11 @@ contains "the worktree's own hostname still serves the worktree" "PROJECT=worktr # Mailpit rides web's VIRTUAL_HOST, which the add-on narrows. code="$(curl -sk -o /dev/null -w '%{http_code}' --max-time 20 https://${tag}-wt-a.ddev.site:8026/ 2>/dev/null || true)" [ "$code" = "200" ] && pass "mailpit still routes" || fail "mailpit still routes" "http $code" +# The variant goes to hostshift, not web, so its Mailpit ports are separate routes. +code="$(curl -sk -o /dev/null -w '%{http_code}' --max-time 20 https://wt-a--${tag}.ddev.site:8026/api/v1/info 2>/dev/null || true)" +[ "$code" = "200" ] && pass "mailpit routes on the variant" || fail "mailpit routes on the variant" "http $code" +code="$(curl -s -o /dev/null -w '%{http_code}' --max-time 20 http://wt-a--${tag}.ddev.site:8025/api/v1/info 2>/dev/null || true)" +[ "$code" = "200" ] && pass "mailpit routes on the variant over http" || fail "mailpit routes on the variant over http" "http $code" out="$(cd "$wt" && ddev hostshift check 2>&1)" && pass "check passes a live worktree" \ || fail "check passes a live worktree" "$out"