From 5fed7f72ea49a66619b7e0f23731f0486ea9b452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 3 Sep 2026 13:28:18 +0300 Subject: [PATCH] The deploy proves the answering process is this commit healthz serves the sha build.rs bakes in, and the swap step asserts the answer names the commit it just pushed, rolling back otherwise. The rename deploy went green over a pre-rename server: a stale process held the port and answered healthz, and the stylesheet check passed because the commit had not touched the css. A rerun-if-env-changed line keeps a cache hit from shipping last deploy's sha. --- .github/workflows/main.yml | 38 +++++++++++++++++++++++++++----------- crates/iz-web/build.rs | 9 +++++++++ crates/iz-web/src/main.rs | 5 ++++- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9b780e6..15af889 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -30,6 +30,11 @@ env: # moves the path with it — a baked-in absolute path once kept pointing at # the previous user's home and failed the deploy on permissions. DEPLOY_SUBDIR: iz + # Baked into the binary by build.rs and asserted against after the + # deploy restart: a /healthz answered by a stale process from an old + # deploy now fails and rolls back instead of reporting green (observed + # on the izlek→iz rename). + IZ_BUILD_SHA: ${{ github.sha }} jobs: deploy: @@ -143,10 +148,11 @@ jobs: # bash -s for the same reason as above: the login shell is not # bash and must never see this script. - ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE'" <<'REMOTE' + ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE' '$GITHUB_SHA'" <<'REMOTE' set -euo pipefail deploy_path=$HOME/$1 service=$2 + expected="ok $3" export XDG_RUNTIME_DIR=/run/user/$(id -u) # Read before anything moves: a config the health check cannot # read is a failure to stop on, not one to roll back from. @@ -162,20 +168,30 @@ jobs: fi mv -- "$deploy_path/current.new" "$deploy_path/current" systemctl --user restart "$service" - # The unit is Type=simple, so a restart returns before the process - # has bound its port. Wait for the health route rather than a clock. + # The unit is Type=simple, so a restart returns before the new + # process has bound its port — and before an old one has let go + # of it. Wait for the health route rather than a clock, and + # check the answer names this commit: a stale process can still + # answer, and did once (the izlek→iz rename deployed green over + # a pre-rename server). + rollback() { + rm -rf -- "$deploy_path/current" + mv -- "$deploy_path/current.previous" "$deploy_path/current" + systemctl --user restart "$service" + } for _ in $(seq 1 30); do - if curl -fsS "http://$listen/healthz" >/dev/null 2>&1; then - echo 'iz is up' - rm -rf -- "$deploy_path/current.previous" - exit 0 + served=$(curl -fsS "http://$listen/healthz" 2>/dev/null) || { sleep 1; continue; } + if [ "$served" != "$expected" ]; then + echo "/healthz answered '$served', wanted '$expected'; rolling back" >&2 + rollback + exit 1 fi - sleep 1 + echo 'iz is up' + rm -rf -- "$deploy_path/current.previous" + exit 0 done echo 'iz did not answer /healthz; rolling back' >&2 - rm -rf -- "$deploy_path/current" - mv -- "$deploy_path/current.previous" "$deploy_path/current" - systemctl --user restart "$service" + rollback exit 1 REMOTE diff --git a/crates/iz-web/build.rs b/crates/iz-web/build.rs index f4d0af5..9b9234e 100644 --- a/crates/iz-web/build.rs +++ b/crates/iz-web/build.rs @@ -32,4 +32,13 @@ fn main() { hex.push_str(&format!("{byte:02x}")); } println!("cargo:rustc-env=IZ_STYLE_FINGERPRINT=sha256:{hex}"); + + // The commit this binary was built from, served by `/healthz` and + // asserted against after the deploy restart, so a stale process + // holding the port cannot pass for this deploy. "dev" locally, where + // nothing asserts it. The rerun-if-env-changed line is load-bearing: + // without it a rust-cache hit would ship last deploy's sha. + let sha = std::env::var("IZ_BUILD_SHA").unwrap_or_else(|_| "dev".into()); + println!("cargo:rustc-env=IZ_BUILD_SHA={sha}"); + println!("cargo:rerun-if-env-changed=IZ_BUILD_SHA"); } diff --git a/crates/iz-web/src/main.rs b/crates/iz-web/src/main.rs index e432664..c8a3d21 100644 --- a/crates/iz-web/src/main.rs +++ b/crates/iz-web/src/main.rs @@ -8,7 +8,10 @@ use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, route}; #[route(GET "/healthz")] async fn healthz() -> Result<&'static str> { - Ok("ok") + // The deploy asserts this against the commit it pushed, so a stale + // process still holding the port fails the deploy instead of + // answering a green health check. + Ok(concat!("ok ", env!("IZ_BUILD_SHA"))) } #[tokio::main]