HTTP

Gate a deploy on an HTTP health check by polling an endpoint until it is healthy.

Operations

Operation Description
Http.WaitForHealthy
Poll a URL until it returns the expected status code (default 200) or the timeout elapses. Runs in-process on the build host, so it needs no curl in the image. Use it to gate a deploy on a health check, for example before swapping a slot into production. Overloads take a string, an IEnumerable<string> (every URL must pass), a Func<string> or a Func<IEnumerable<string>> (computed at execution time). Extra options: ConsecutiveSuccesses(n) needs n passing checks in a row (a failure resets the count), MinGapSeconds(s) waits at least s seconds between counted passes, and ExpectJson(path, expected) requires a dotted-path value in the JSON body. ExpectJson applies to every URL of the call. Response expectations (on HttpHealthCheckOptions and, for Get, on HttpGetOptions): ExpectContentType(prefix) (case-insensitive prefix of the Content-Type), ExpectBodyContains(text) (ordinal) and ExpectBodyMatches(regex) (.NET regex syntax, 2 second timeout). On a mismatch Http.Get fails the step (Value still holds the body) and WaitForHealthy treats it as a failed check whose message shows the content type and the first 200 characters of the body.
Http.Get
Sends a GET request (from the build host) and keeps the response. Returns a StepOutput: Value is the body and StatusCode the status code. Fails on a non-2xx status unless allowAnyStatus is true. Signatures: Get(string url, bool allowAnyStatus = false), Get(Func<string> url, bool allowAnyStatus = false), and the option overloads Get(string url, Action<HttpGetOptions> configure) and Get(Func<string> url, Action<HttpGetOptions> configure). HttpGetOptions has AllowAnyStatus() and the response expectations. Response expectations (on HttpHealthCheckOptions and, for Get, on HttpGetOptions): ExpectContentType(prefix) (case-insensitive prefix of the Content-Type), ExpectBodyContains(text) (ordinal) and ExpectBodyMatches(regex) (.NET regex syntax, 2 second timeout). On a mismatch Http.Get fails the step (Value still holds the body) and WaitForHealthy treats it as a failed check whose message shows the content type and the first 200 characters of the body.
Http.PublicIpV4
Looks up this machine's public IPv4 address (as seen by public services). Returns a StepOutput whose Value is the address.

Operation Details

Http.WaitForHealthy source

Poll a URL until it returns the expected status code (default 200) or the timeout elapses. Runs in-process on the build host, so it needs no curl in the image. Use it to gate a deploy on a health check, for example before swapping a slot into production. Overloads take a string, an IEnumerable<string> (every URL must pass), a Func<string> or a Func<IEnumerable<string>> (computed at execution time). Extra options: ConsecutiveSuccesses(n) needs n passing checks in a row (a failure resets the count), MinGapSeconds(s) waits at least s seconds between counted passes, and ExpectJson(path, expected) requires a dotted-path value in the JSON body. ExpectJson applies to every URL of the call. Response expectations (on HttpHealthCheckOptions and, for Get, on HttpGetOptions): ExpectContentType(prefix) (case-insensitive prefix of the Content-Type), ExpectBodyContains(text) (ordinal) and ExpectBodyMatches(regex) (.NET regex syntax, 2 second timeout). On a mismatch Http.Get fails the step (Value still holds the body) and WaitForHealthy treats it as a failed check whose message shows the content type and the first 200 characters of the body.

Http.WaitForHealthy(() => $"https://{host.Value}/healthz", o => o
  .ConsecutiveSuccesses(3)
  .MinGapSeconds(10)
  .ExpectJson("version", "1.4.0"));
Http.WaitForHealthy(new[] { "https://a.example.com/healthz", "https://a.example.com/healthz/db" });
Http.WaitForHealthy("https://app.example.com/healthz");
Http.WaitForHealthy("https://app.example.com/healthz/db", o => o
  .WithTimeoutSeconds(300)
  .WithIntervalSeconds(5));

Http.Get source

Sends a GET request (from the build host) and keeps the response. Returns a StepOutput: Value is the body and StatusCode the status code. Fails on a non-2xx status unless allowAnyStatus is true. Signatures: Get(string url, bool allowAnyStatus = false), Get(Func<string> url, bool allowAnyStatus = false), and the option overloads Get(string url, Action<HttpGetOptions> configure) and Get(Func<string> url, Action<HttpGetOptions> configure). HttpGetOptions has AllowAnyStatus() and the response expectations. Response expectations (on HttpHealthCheckOptions and, for Get, on HttpGetOptions): ExpectContentType(prefix) (case-insensitive prefix of the Content-Type), ExpectBodyContains(text) (ordinal) and ExpectBodyMatches(regex) (.NET regex syntax, 2 second timeout). On a mismatch Http.Get fails the step (Value still holds the body) and WaitForHealthy treats it as a failed check whose message shows the content type and the first 200 characters of the body.

var health = Http.Get("https://eu.example.com/healthz");
Ando.Report("eu.healthz", health);
var probe = Http.Get(() => $"https://{host.Value}/healthz", allowAnyStatus: true);
Ando.Require(() => probe.StatusCode < 500, "Server error");
Http.Get("https://x/", o => o.ExpectContentType("text/html").ExpectBodyContains("<title>"));
Http.Get("https://x/version", o => o.AllowAnyStatus().ExpectBodyMatches(@"^\d+\.\d+\.\d+$"));

Http.PublicIpV4 source

Looks up this machine's public IPv4 address (as seen by public services). Returns a StepOutput whose Value is the address.

var ip = Http.PublicIpV4();
Ando.Report("buildIp", ip);

Example

Wait for a freshly deployed app to report healthy before swapping a slot into production. Http.WaitForHealthy runs in-process on the build host, so the build image does not need curl.

// Deploy to the staging slot
AppService.DeployFolder("my-web-app", Root / "publish", "my-rg", o => o
  .WithDeploymentSlot("staging"));

// Wait for the staging slot to come up before promoting it
Http.WaitForHealthy("https://my-web-app-staging.azurewebsites.net/healthz", o => o
  .WithTimeoutSeconds(300)
  .WithIntervalSeconds(5));

// Promote staging to production
AppService.SwapSlots("my-web-app", "staging", "my-rg");

Overloads

Http.WaitForHealthy accepts the URL in four forms:

OverloadUse
WaitForHealthy(string url, configure?)One URL.
WaitForHealthy(IEnumerable<string> urls, configure?)Several URLs. A check passes only when every URL passes.
WaitForHealthy(Func<string> url, configure?)A URL computed at execution time, for example from a step output.
WaitForHealthy(Func<IEnumerable<string>> urls, configure?)Several URLs computed at execution time.
var host = AppService.GetHostName("my-web-app", "my-rg", "staging");

Http.WaitForHealthy(
  () => new[] { $"https://{host.Value}/healthz", $"https://{host.Value}/healthz/db" },
  o => o
    .ConsecutiveSuccesses(3)
    .MinGapSeconds(11)
    .WithTimeoutSeconds(600)
    .ExpectJson("version", "1.4.0"));

Options Reference

Http.WaitForHealthy Options

OptionDescription
WithExpectedStatus(int)HTTP status code that marks the endpoint healthy. Default 200.
WithTimeoutSeconds(int)Total time to keep polling before the step fails. Default 300.
WithIntervalSeconds(int)Delay between polls. Default 5.
WithRequestTimeoutSeconds(int)Per-request timeout for each poll. Default 10.
ConsecutiveSuccesses(int)Number of passing checks in a row required. A failure resets the count. With several URLs, one check means every URL passed. Default 1.
MinGapSeconds(int)Minimum seconds between two counted passing checks. ConsecutiveSuccesses(3).MinGapSeconds(11) means three passes spread over at least 22 seconds. Default 0.
ExpectContentType(string prefix)Requires the Content-Type to start with prefix (case-insensitive), for example text/html.
ExpectBodyContains(string text)Requires the body to contain text (ordinal comparison).
ExpectBodyMatches(string regex)Requires the body to match the .NET regular expression (2 second timeout).
ExpectJson(string path, string expected)Requires the response body to be JSON with expected at the dotted path (such as version or build.commit). Numbers and booleans compare by their JSON text. Applies to every URL of the call; use a separate call for URLs that return something else. Can be repeated.

Http.Get

Http.Get(url, allowAnyStatus = false) and Http.Get(Func<string> url, allowAnyStatus = false) send a GET request from the build host and return a StepOutput. Value is the response body and StatusCode the status code. The step fails on a non-2xx status unless allowAnyStatus is true. Reading the output before the step ran throws.

var health = Http.Get("https://eu.example.com/healthz");
Ando.Report("eu.healthz", health);

Response expectations

Http.Get has overloads that take a configuration lambda, Get(string url, Action<HttpGetOptions> configure) and Get(Func<string> url, Action<HttpGetOptions> configure). HttpGetOptions has AllowAnyStatus() and the same three expectations as WaitForHealthy: ExpectContentType(prefix) (case-insensitive prefix), ExpectBodyContains(text) (ordinal) and ExpectBodyMatches(regex) (.NET syntax, 2 second timeout).

Http.Get("https://x/", o => o.ExpectContentType("text/html").ExpectBodyContains("<title>"));

When an expectation is not met, Http.Get fails the step (Value still holds the body) and WaitForHealthy counts it as a failed check, with a message that shows the content type and the first 200 characters of the body. This catches a host that answers 200 with an error page or a default placeholder.

Http.PublicIpV4

Http.PublicIpV4() looks up this machine’s public IPv4 address, as seen by public services, and returns a StepOutput whose Value is the address.

var ip = Http.PublicIpV4();
Ando.Report("buildIp", ip);