# Deploy checks

Record a deploy and Tailcove answers one question about 15 minutes later: did errors get worse after it? The answer
lands on the deploy's report page, in the deploys API, in the channels your alerts already use, and as a commit status
on GitHub. It's the check you'd otherwise do by hand after every release, staring at the error chart.

## Record a deploy

Send a deploy marker from CI when the release goes out. With the [CLI](https://tailcove.dev/docs/cli):

```bash
tailcove deploy --source api --version "$GITHUB_SHA"
```

Or with the [deploys API](https://tailcove.dev/docs/api/deploys):

```bash
curl -X POST https://tailcove.dev/api/v1/sources/$TAILCOVE_SOURCE_ID/deploys \
  -H "Authorization: Bearer $TAILCOVE_API_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"version\": \"$GITHUB_REF_NAME\", \"commit_sha\": \"$GITHUB_SHA\", \"environment\": \"production\"}"
```

Every deploy recorded this way gets a check. Rollbacks are checked too, so you can see whether going back helped.

## What it compares

The check waits until 15 minutes have passed since the deploy (and one more minute, so the last lines have arrived),
then compares those 15 minutes with the 60 minutes before the deploy. Errors are lines at `error`, `critical`, `alert`
or `emergency` level, counted per minute of each window.

A deploy **regressed** when any of these is true:

- The error rate after it is above 1.5 times the rate before **and** at least 1 more error a minute than before, with
  5 errors or more in the 15 minutes. Both conditions together stop a quiet service from "regressing" on one stray
  error, and stop a busy one from passing with a real jump.
- An error [pattern](https://tailcove.dev/docs/patterns) with 3 or more error lines after the deploy had no line at all, at any level, in
  the 7 days before it. That's a kind of error your code didn't write last week.
- An [issue](https://tailcove.dev/docs/issues) was first seen after the deploy.

Otherwise it's **healthy**. A worked example: 24 errors in the hour before is 0.4 a minute, so the limit is
max(0.4 × 1.5, 0.4 + 1) = 1.4 a minute. 32 errors in the 15 minutes after is 2.1 a minute, past the limit: regressed.
21 errors (exactly 1.4 a minute) would still be healthy.

Two more outcomes:

- **No data**: no line at all arrived in the 15 minutes after the deploy, or the source sent nothing in the 7 days
  before it. A brand new source has nothing to compare with, and we'd rather say so than call every error "new".
- **Not checked**: the source has deploy checks off, the deploy was too old to check (more than 7 days), or the logs
  couldn't be read after several tries. Deploys recorded before this feature existed show as not checked too.

## Previews and pull requests

A deploy check after the merge tells you what broke in production. Most teams would rather know before the merge, from
the preview or review app their pull request already builds. A preview needs different rules, though: it's a fresh
environment with little traffic, so "the hour before" is empty or meaningless.

A deploy is a **preview** when it names a pull request (`pull_request`, or `--pull-request` in the CLI), or when its
environment is `preview`, `review` or `pr`, alone or followed by `-`, `_` or `/` (`pr-482`, `preview_fix-login`,
`review/fix-login`; any case). Everything else gets the release check above.

```bash
tailcove deploy --source web-preview --env preview \
  --pull-request 482 --branch fix-login --sha "$HEAD_SHA" --wait
```

The preview check waits the same 15 minutes (and one more), then:

- An error [pattern](https://tailcove.dev/docs/patterns) with 3 or more error lines in those 15 minutes **regresses** the preview when the
  comparison source had no line of it at all in the 7 days before the deploy.
- An [issue](https://tailcove.dev/docs/issues) of the preview's source that was first seen in those 15 minutes regresses it too.
- There's no error-rate rule. A preview with 400 errors of a kind production logs every day is still healthy: those
  errors aren't new.

It has **no data** when the preview sent nothing in the 15 minutes, or when the comparison source sent nothing in the 7
days before, since then every pattern would look new.

### Which source it's compared with

By default a preview is compared with its own source. That works when every pull request deploys to the same preview
source and some of them ran in the last week. If your previews log to a source of their own and production logs to
another, set **Compare previews with** under **Deploy checks** on the preview source's **Settings** tab and pick the
production (or staging) source. Only sources of the same team are offered, and a member restricted to some client
groups only sees the sources in those groups. If the chosen source is deleted later, previews are compared with their
own source again. Through the API it's `preview_compare_source_id` on the source (a source id like `src_…`).

### On the pull request

The commit status of a preview is called **Tailcove preview check**, separate from **Tailcove deploy check**, so a pull
request can show both. It needs the same things as the release status: a GitHub repository on the source and the full
40-character commit. Send the pull request's head commit, not the commit your CI happened to check out: on a
`pull_request` event in GitHub Actions, `GITHUB_SHA` is a merge commit GitHub makes for the run, and a status on it
never shows on the pull request. Use `${{ github.event.pull_request.head.sha }}`.

The report page says the deploy was a preview, links the pull request (when the source has a GitHub repository), shows
the branch and which source it was compared with, and lists the new patterns and issues. Messages to your channels say
"Preview v1.5.0 for PR #482" and add a line with the pull request, branch and comparison source. The step-by-step setup
for GitHub Actions is in the guide [Catch new errors in pull requests](https://tailcove.dev/guides/catch-new-errors-in-pull-requests).

## The report

Every place that shows a deploy now carries its verdict as a small badge (Regressed, Healthy, Checking, No data, Not
checked) and opens the report when you click it: the dashboard's **Changes and incidents** card, the **Recent deploys**
list on a source's Overview, the **Deployed just before** chips on an incident, and the **Release** row of a log line.

The report shows errors a minute before and after, each new error pattern with its first line and a **View lines** link
that opens the logs of that pattern in the window after the deploy, each new issue, the commit (linked to GitHub when
the source has a repository), what GitHub said about the commit status, and which channels were told. While the check
is waiting, the page says when it will run.

## Who hears about it

On the source's **Settings** tab, **Deploy checks** has three choices:

- **Tell us when a deploy makes things worse** (the default): a regression is sent to the source's default alert
  channels.
- **Tell us about every check**: regressions as above, plus a short "looks healthy" note for the others.
- **Off**: deploys are still recorded and drawn on the charts, but not checked.

The default alert channels are the ones set under **Notify by default** on the source's **Alerts** tab (see
[Alerts](https://tailcove.dev/docs/alerts)). A regression goes to all of them: email and SMS to the members who turned those on in their
profile, Slack, Discord, Microsoft Teams, Telegram, PagerDuty, Opsgenie and your webhook. A healthy check only goes to
Slack, Discord, Teams, Telegram and the webhook: nobody gets paged, texted or emailed about a deploy that went fine.

A regression in Slack looks like this:

```
🚨 Deploy v1.4.2 to production: 3 new error patterns and errors up from 0.4 to 2.1 a minute.
Source: api · 3f9a1c0 Retry SMS on failure
Open the deploy check
```

A webhook receives `"event": "deploy.checked"` with the deploy, the source and the same `check` object the API returns,
signed with `X-Tailcove-Signature` like every other Tailcove webhook.

## The commit status on GitHub

When the source has a GitHub repository (the **GitHub repository** button on its **Issues** tab) and the deploy names
its full commit (all 40 characters of `commit_sha`), the check is also set as a commit status named **Tailcove deploy
check**: `failure` for a regression, `success` for a healthy check or one with no data. Its **Details** link opens the
report. It's set with the GitHub connection of the member who chose the repository.

The Tailcove GitHub App asks only for the Issues and Metadata permissions, so GitHub refuses the commit status until the
app is also given **Commit statuses** (read and write). The report page tells you when that happened. A connection made
through the older OAuth app already has the access it needs. A short commit SHA (`3f9a1c0`) gets no commit status:
GitHub can't find a commit by it.

## Fail a pipeline when a deploy regresses

`--wait` makes the CLI wait for the check and exit with `1` when it regressed, and `0` when it's healthy, has no data or
was skipped. Put it after the step that ships:

```bash
tailcove deploy --source api --version "$GITHUB_SHA" --wait
```

In GitHub Actions:

```yaml
- name: Record the deploy and wait for its check
  env:
    TAILCOVE_API_TOKEN: ${{ secrets.TAILCOVE_API_TOKEN }}
  run: tailcove deploy --source api --version "$GITHUB_SHA" --env production --wait --timeout 20m
```

The step takes about 16 minutes, the time the check waits for errors to show up. Without the CLI, poll
[`GET /api/v1/sources/{id}/deploys/{deploy}`](https://tailcove.dev/docs/api/deploys) every minute until `check.status` is no longer
`pending`.
