Deployment health checks: catch a broken release before it gets traffic
Deploy a FastAPI app on Gregale, reject a revision that returns 503 on readiness, and verify a fixed candidate before it gets traffic.
A deployment health check asks whether the new revision is ready to serve requests before users reach it. On Gregale, set the check to an application endpoint such as GET /readyz. A build can succeed and still fail this check. If you stage a replacement at 0% traffic, the release that was already serving stays in place while you investigate.
Download the FastAPI starter and follow along with the Gregale CLI, jq, curl (7.82+) and openssl. Use a new disposable app with no production secrets or resources. The demo returns only synthetic data; the public test URL is intentional.
1. Give the app a readiness endpoint
The starter points Gregale at an app-owned endpoint and listens on the port supplied by the platform:
hosting:
start: uvicorn main:app --host 0.0.0.0 --port $PORT
health: /readyz
Return 200 when the app can serve and 503 while it cannot. Make this route lightweight and keep it outside login redirects. Return only a readiness signal, not dependency credentials or error details. In the demo, the file release.txt picks which revision the app returns and whether its readiness route fails.
2. Deploy a known good revision
Run these blocks in the same Bash session from the extracted starter directory. Export a Gregale API key with permission to create, deploy and delete a test app as GREGALE_TOKEN. Use a test workspace, not a shared production workspace. The create request must return HTTP 201 for a fresh app; a name conflict stops the drill.
set -euo pipefail
: "${GREGALE_TOKEN:?Export a Gregale API key for the test workspace}"
APP="ready-demo-$(openssl rand -hex 8)"
RECEIPTS="$(mktemp -d)"
g() { gregale --non-interactive --json "$@"; }
d() { g deploy --name "$APP" --path . --source=worktree --no-require-authn "$@"; }
printf 'v1\n' > release.txt
CREATE_STATUS="$(curl --silent --show-error --max-time 30 --config - \
--output "$RECEIPTS/app.json" --write-out '%{http_code}' \
--json "$(jq -nc --arg slug "$APP" '{slug: $slug, type: "app", require_authn: false}')" \
https://api.gregale.dev/v1/apps <<EOF
header = "Authorization: Bearer $GREGALE_TOKEN"
EOF
)"
if [[ "$CREATE_STATUS" != 201 ]]; then
printf 'App was not reserved (HTTP %s). Stop here.\n' "$CREATE_STATUS" >&2
exit 1
fi
APP_ID="$(jq -er --arg app "$APP" 'select(.slug == $app) | .id' "$RECEIPTS/app.json")"
same_app() { g app get "$APP" | jq -e --arg id "$APP_ID" '.id == $id' >/dev/null; }
serving_only() {
g release traffic status "$APP" | jq -e --arg id "$1" '
[.deployments[] | select(.traffic_percent > 0) | [.id, .traffic_percent]] == [[$id, 100]]'
}
same_app
d --wait --yes --rollout immediate > "$RECEIPTS/v1.json"
A="$(jq -er --arg app "$APP_ID" '
select(.app_id == $app and .status == "live" and .traffic_percent == 100) | .id
' "$RECEIPTS/v1.json")"
URL="$(jq -er '.app_url' "$RECEIPTS/v1.json")"
curl -fsS "$URL/version" | jq -e '.release == "v1"'
The create response supplies the new app ID. The later identity checks stop the demo if the slug no longer points to that app. Keep the deployment ID and the stable URL. The /version response is the proof that the application answered. A generic health URL or a build-success message alone cannot tell you which revision actually ran.
3. Reject a broken candidate
The deliberately broken revision builds normally, starts Uvicorn, and answers GET /readyz with HTTP 503. Stage it without shifting traffic.
same_app
printf 'broken\n' > release.txt
if d --yes --no-traffic --wait > "$RECEIPTS/broken.json"; then
echo "Unexpected success. Do not promote this revision." >&2
exit 1
fi
Expect the deploy to fail at startup, not during the build. Use the receipt from this invocation, not failure history. Stop if it does not prove that this candidate failed its readiness check.
FAILED="$(jq -er --arg app "$APP_ID" --arg original "$A" '
select(.app_id == $app and .id != $original
and .status == "failed" and .traffic_percent == 0
and .error_code == "app_startup_timeout"
and ((.error // "") | contains("at /readyz") and contains("last status 503")))
| .id
' "$RECEIPTS/broken.json")"
gregale deployment "$FAILED" --app "$APP"
serving_only "$A"
curl -fsS "$URL/version" | jq -e '.release == "v1"'
In the hosted demo, Gregale reported app_startup_timeout and recorded /readyz and last status 503 in the deployment error. Read the full error: the timeout code alone does not tell you whether the app answered at all.
4. Verify the fixed revision before promoting
same_app
printf 'v2\n' > release.txt
d --yes --no-traffic --wait > "$RECEIPTS/v2.json"
B="$(jq -er --arg app "$APP_ID" '
select(.app_id == $app and .status == "live" and .traffic_percent == 0) | .id
' "$RECEIPTS/v2.json")"
g release verify "$B" --app "$APP" --path /version |
jq -e --arg id "$B" '.verified and .deployment_id == $id
and .http_status == 200 and (.body | fromjson | .release) == "v2"'
serving_only "$A"
curl -fsS "$URL/version" | jq -e '.release == "v1"'
The exact-deployment check should return v2 while the stable URL still returns v1. For this disposable app only, move traffic directly to the verified revision. The guard refuses the cutover if the original revision no longer serves all traffic.
same_app
g release traffic promote --app "$APP" --release "$B" --if-serving "$A"
serving_only "$B"
curl -fsS "$URL/version" | jq -e '.release == "v2"'
same_app
gregale app delete "$APP"
On a real app, keep application-level checks and traffic verification separate. A readiness check is not a smoke test for your business logic, a runtime liveness monitor, or a rollback for database side effects.
See FastAPI deployment, deploy troubleshooting, and how to verify which version is running in production.
Questions
- What does a deployment health check do?
It checks whether a new revision is ready to serve before it receives traffic. It is separate from building the artifact or checking whether a running process is still alive.
- What should a readiness endpoint return?
Return HTTP 200 when the app is ready to serve and 503 when it is not. Keep the endpoint lightweight, avoid authentication redirects, and do not include secrets.
- How can I check a fixed release without sending users to it?
Stage it at 0% traffic, then run gregale release verify on the exact deployment ID and an application-owned route. Check the normal app URL separately before promoting.