The deploy proves the answering process is this commit
Browse files at this commit5fed7f72ea49a66619b7e0f23731f0486ea9b452Parent: 79ce4663 changed files +40 -12
- .github/workflows/main.yml +27 -11
- crates/iz-web/build.rs +9 -0
- crates/iz-web/src/main.rs +4 -1
.github/workflows/main.ymlmodified+27 -11
| @@ -30,6 +30,11 @@ env: | |||
| 30 | # moves the path with it — a baked-in absolute path once kept pointing at | 30 | # moves the path with it — a baked-in absolute path once kept pointing at |
| 31 | # the previous user's home and failed the deploy on permissions. | 31 | # the previous user's home and failed the deploy on permissions. |
| 32 | DEPLOY_SUBDIR: iz | 32 | DEPLOY_SUBDIR: iz |
| 33 | + # Baked into the binary by build.rs and asserted against after the | ||
| 34 | + # deploy restart: a /healthz answered by a stale process from an old | ||
| 35 | + # deploy now fails and rolls back instead of reporting green (observed | ||
| 36 | + # on the izlek→iz rename). | ||
| 37 | + IZ_BUILD_SHA: ${{ github.sha }} | ||
| 33 | 38 | ||
| 34 | jobs: | 39 | jobs: |
| 35 | deploy: | 40 | deploy: |
| @@ -143,10 +148,11 @@ jobs: | |||
| 143 | 148 | ||
| 144 | # bash -s for the same reason as above: the login shell is not | 149 | # bash -s for the same reason as above: the login shell is not |
| 145 | # bash and must never see this script. | 150 | # bash and must never see this script. |
| 146 | - ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE'" <<'REMOTE' | 151 | + ssh "$ssh_target" "bash -se -- '$DEPLOY_SUBDIR' '$SERVICE' '$GITHUB_SHA'" <<'REMOTE' |
| 147 | set -euo pipefail | 152 | set -euo pipefail |
| 148 | deploy_path=$HOME/$1 | 153 | deploy_path=$HOME/$1 |
| 149 | service=$2 | 154 | service=$2 |
| 155 | + expected="ok $3" | ||
| 150 | export XDG_RUNTIME_DIR=/run/user/$(id -u) | 156 | export XDG_RUNTIME_DIR=/run/user/$(id -u) |
| 151 | # Read before anything moves: a config the health check cannot | 157 | # Read before anything moves: a config the health check cannot |
| 152 | # read is a failure to stop on, not one to roll back from. | 158 | # read is a failure to stop on, not one to roll back from. |
| @@ -162,20 +168,30 @@ jobs: | |||
| 162 | fi | 168 | fi |
| 163 | mv -- "$deploy_path/current.new" "$deploy_path/current" | 169 | mv -- "$deploy_path/current.new" "$deploy_path/current" |
| 164 | systemctl --user restart "$service" | 170 | systemctl --user restart "$service" |
| 165 | - # The unit is Type=simple, so a restart returns before the process | 171 | + # The unit is Type=simple, so a restart returns before the new |
| 166 | - # has bound its port. Wait for the health route rather than a clock. | 172 | + # process has bound its port — and before an old one has let go |
| 173 | + # of it. Wait for the health route rather than a clock, and | ||
| 174 | + # check the answer names this commit: a stale process can still | ||
| 175 | + # answer, and did once (the izlek→iz rename deployed green over | ||
| 176 | + # a pre-rename server). | ||
| 177 | + rollback() { | ||
| 178 | + rm -rf -- "$deploy_path/current" | ||
| 179 | + mv -- "$deploy_path/current.previous" "$deploy_path/current" | ||
| 180 | + systemctl --user restart "$service" | ||
| 181 | + } | ||
| 167 | for _ in $(seq 1 30); do | 182 | for _ in $(seq 1 30); do |
| 168 | - if curl -fsS "http://$listen/healthz" >/dev/null 2>&1; then | 183 | + served=$(curl -fsS "http://$listen/healthz" 2>/dev/null) || { sleep 1; continue; } |
| 169 | - echo 'iz is up' | 184 | + if [ "$served" != "$expected" ]; then |
| 170 | - rm -rf -- "$deploy_path/current.previous" | 185 | + echo "/healthz answered '$served', wanted '$expected'; rolling back" >&2 |
| 171 | - exit 0 | 186 | + rollback |
| 187 | + exit 1 | ||
| 172 | fi | 188 | fi |
| 173 | - sleep 1 | 189 | + echo 'iz is up' |
| 190 | + rm -rf -- "$deploy_path/current.previous" | ||
| 191 | + exit 0 | ||
| 174 | done | 192 | done |
| 175 | echo 'iz did not answer /healthz; rolling back' >&2 | 193 | echo 'iz did not answer /healthz; rolling back' >&2 |
| 176 | - rm -rf -- "$deploy_path/current" | 194 | + rollback |
| 177 | - mv -- "$deploy_path/current.previous" "$deploy_path/current" | ||
| 178 | - systemctl --user restart "$service" | ||
| 179 | exit 1 | 195 | exit 1 |
| 180 | REMOTE | 196 | REMOTE |
| 181 | 197 | ||
crates/iz-web/build.rsmodified+9 -0
| @@ -32,4 +32,13 @@ fn main() { | |||
| 32 | hex.push_str(&format!("{byte:02x}")); | 32 | hex.push_str(&format!("{byte:02x}")); |
| 33 | } | 33 | } |
| 34 | println!("cargo:rustc-env=IZ_STYLE_FINGERPRINT=sha256:{hex}"); | 34 | println!("cargo:rustc-env=IZ_STYLE_FINGERPRINT=sha256:{hex}"); |
| 35 | + | ||
| 36 | + // The commit this binary was built from, served by `/healthz` and | ||
| 37 | + // asserted against after the deploy restart, so a stale process | ||
| 38 | + // holding the port cannot pass for this deploy. "dev" locally, where | ||
| 39 | + // nothing asserts it. The rerun-if-env-changed line is load-bearing: | ||
| 40 | + // without it a rust-cache hit would ship last deploy's sha. | ||
| 41 | + let sha = std::env::var("IZ_BUILD_SHA").unwrap_or_else(|_| "dev".into()); | ||
| 42 | + println!("cargo:rustc-env=IZ_BUILD_SHA={sha}"); | ||
| 43 | + println!("cargo:rerun-if-env-changed=IZ_BUILD_SHA"); | ||
| 35 | } | 44 | } |
crates/iz-web/src/main.rsmodified+4 -1
| @@ -8,7 +8,10 @@ use topcoat::router::{BodyLimit, Router, RouterBuilderDiscoverExt, route}; | |||
| 8 | 8 | ||
| 9 | #[route(GET "/healthz")] | 9 | #[route(GET "/healthz")] |
| 10 | async fn healthz() -> Result<&'static str> { | 10 | async fn healthz() -> Result<&'static str> { |
| 11 | - Ok("ok") | 11 | + // The deploy asserts this against the commit it pushed, so a stale |
| 12 | + // process still holding the port fails the deploy instead of | ||
| 13 | + // answering a green health check. | ||
| 14 | + Ok(concat!("ok ", env!("IZ_BUILD_SHA"))) | ||
| 12 | } | 15 | } |
| 13 | 16 | ||
| 14 | #[tokio::main] | 17 | #[tokio::main] |