URL: /docs/github-app

---
title: GitHub App
description: Connect Turntable to GitHub with a least-privilege App, an encrypted secret store and verified webhooks.
---

Turntable talks to GitHub as a GitHub App. This page covers creating the App, where its credentials live, and how webhooks are verified. It can report each job as a check run, off by default, see [Quickstart](/docs/quickstart#report-results-to-github-as-check-runs).

## 1. Create a master key

The App's private key, webhook secret and client secret are kept in a built-in encrypted store. Each secret is sealed with AES-256-GCM under its own data key, and the data key is wrapped by a master key from a 32-byte file.

```sh
turntable keys init --file /secure/path/master.key
export TURNTABLE_MASTER_KEY_FILE=/secure/path/master.key
```

The file is created with mode `0600` and is never printed. Back it up: without it the stored secrets cannot be decrypted. A wrong or damaged key makes decryption fail closed. Each record binds the org id and secret name into its encryption, so a stored value cannot be moved to another name or org. No operation returns a secret value.

## 2. Create the App

```sh
turntable github-app create --url https://ci.example.com [--org my-github-org] [--name Turntable]
```

This serves a local page on `127.0.0.1` and prints its address. Open it, press the button, and approve the App on GitHub (under your account, or under `--org`). GitHub redirects back to the loopback callback with a one-time code, which Turntable exchanges for the App's credentials. They go straight into the encrypted store and are not printed.

The manifest asks for the least access that works:

| Permission | Level | Why |
| --- | --- | --- |
| Checks | write | Reporting results (used later) |
| Contents | read | Fetching the commit to run |
| Metadata | read | Listing repositories |
| Pull requests | read | Reading pull request facts |

Subscribed events are `push`, `pull_request` and `check_run`. GitHub also sends `installation` and `installation_repositories` to every App without a subscription.

The webhook URL is `<url>/api/v1/webhooks/github`. The `--turntable-org` flag chooses which Turntable org owns the App and its installations (default `local`).

The App is created private, so only the account that owns it can install it. To let other accounts install it (the [claim flow](/docs/hosted#one-instance-several-orgs-claiming-an-installation)), open the App's settings on GitHub, go to **Advanced**, and choose **Make public**. The instance-hosted manifest flow does this for you when `TURNTABLE_CLAIM_INSTALLATIONS=1`.

Install the App on the repositories you want from the link the command prints.

## Installations, orgs and projects

When GitHub reports an installation, Turntable does not trust the payload. It re-fetches the installation and its repositories from the GitHub API with the App's own credentials, checks that the installation belongs to this App, and then maps it to the owning org and each repository to a project. A payload that names a different installation, account or repository than the API reports changes nothing. A repository already bound by another installation or org is left alone and reported. Removing a repository from an installation, or uninstalling the App, unbinds it.

### Re-granting a repo

When an installation drops a repo (or is deleted), the sync unbinds it and records on the project which forge repo id it lost and when (`last_forge_repo_id`, `unbound_at`). The project, its settings and its runs stay. If the same forge repo id is granted again, the sync re-attaches it to that project instead of creating a new one, clears the two columns and writes a `reattach_repo_binding` audit entry. Three rules hold: the project must be in the same org as the installation, the forge repo id must match (never the name alone, so a renamed or recreated repo gets a new project), and the project must have no binding now. With several candidates, the most recently unbound wins. The decision reads only these columns, never the audit log. Projects unbound before this existed have no columns set and are not re-attached.

### Moving a polled repo to the App

A repo that was ingested by polling before the App was installed is bound with no installation, so no check runs can be posted for it. When the App is installed on it, the sync adopts that binding: it sets the installation (and the GitHub repo id, when the binding had none) and the binding keeps its project, so run history stays together. The adoption is audited as `adopt_repo_binding`. Only a binding in the same org with no installation is adopted. A binding in another org, from another installation, or whose stored GitHub repo id differs from the installation's (a renamed or recreated repo) is a conflict and is left alone. After adoption the binding is an installation binding like any other: removing the repo from the installation unbinds it, and a poll entry that still names it binds it again on the next poll cycle, into the same project: the project, its settings and its run history are kept, and the binding has no installation until the App is installed on it again.

An installation that was synced before this adoption existed has its polled repos skipped, with the binding left without an installation. Two things pick those up:

- **Boot sweep.** After the server is listening, it re-runs the sync for each active installation whose org still has a binding with no installation. It does not delay startup or `/healthz`, it makes no GitHub call when no org has such a binding, and running it again changes nothing. Failures are logged and audited as `adopt_sweep`.
- **`resync_installation`.** An org-level token with `release_operator` or higher re-syncs one installation id its org owns (`POST /api/v1/github/installations/{installation_id}/resync`, also an MCP tool) and gets back what was bound, adopted, unbound or left alone. Another org's installation id is `not_found`. It is limited to 6 calls a minute per org and then refused with `rate_limited`. Every call is audited.

## Webhook verification

The endpoint, `POST /api/v1/webhooks/github`, requires `TURNTABLE_MASTER_KEY_FILE`.

- The body is capped at 25 MiB (GitHub's maximum), by `Content-Length` and by the streamed size.
- The `X-Hub-Signature-256` HMAC-SHA256 is checked over the raw body with a constant-time compare, before anything is parsed. A bad signature gets `401` and stores nothing. Repeated failures from one client are rate limited (`429`), keyed on the real client address (see `TURNTABLE_TRUSTED_PROXIES` in [Hosted deployment](/docs/hosted)).
- The delivery GUID is stored before the `202`. A delivery with a GUID seen before is acknowledged and not run again. Deliveries are kept for 30 days.
- Events with no handler are stored as ignored and acknowledged. `push` and `pull_request` are ignored too, except that they wake the poller for the repo they name (see [Hosted deployment](/docs/hosted)). A delivery interrupted before it finished is retried on the next start.
- Handlers are idempotent.

## Rotating the webhook secret

```sh
turntable github-app rotate-webhook-secret
```

A new secret is generated and set on the App through the GitHub API. The old one stays valid for 24 hours so in-flight deliveries still verify. If GitHub refuses the change, nothing changes locally.

## Installation tokens

Installation access tokens come from one function. It signs a short-lived App JWT, asks GitHub for a token scoped to the repositories and permissions of the call, and caches it until about five minutes before it expires. Tokens are never logged or returned by an operation.

## Other commands

`turntable github-app status` lists the App and its installations (no secrets).
