# Deploy from GitHub

Connect a repository, and every push builds on the platform and deploys it. The default branch deploys to production, and every other branch to its preview.

> [!NOTE]
> **Preview** — This works today, but the surface may still change.

Connect a GitHub repository to an app, and every push to it is built on the
platform and deployed. The default branch deploys to production, and every
other branch to its [preview URL](/hosting/previews.md). Each build reports
back as a check on its commit, and a pull request gets its preview link in
the conversation.

During the preview, the Pilvia GitHub App is installed by arrangement. On
the hosted platform, write to
[support@pilvia.com](mailto:support@pilvia.com); on a self-hosted
installation, ask whoever runs it.

## Connecting a repository

The examples on this page read the control plane's address and your token
from the environment:

```sh
export PILVIA_APP_SERVER=https://deploy.pilvia.dev   # the hosted platform
export PILVIA_DEPLOY_TOKEN=pilvia_…
```

With the App installed, link the app to the repository:

```sh
curl -X PUT "$PILVIA_APP_SERVER/v1/apps/my-site/source" \
  -H "Authorization: Bearer $PILVIA_DEPLOY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"repo": "acme/website"}'
```

| Field | Means |
| --- | --- |
| `repo` | The repository, as `owner/name`. |
| `root` | The directory that holds `pilvia.app.json`, for a monorepo. Default: the top of the repository. |
| `branch` | The branch that deploys production. Default: the repository's default branch. |

Linking needs a [token](/hosting/tokens.md) that covers the app and is not
pinned to a target. The platform looks up the App's installation on the
repository first, so linking a repository the App cannot read is refused.
`GET` on the same path reads the link back.

In a monorepo, link each app to its own directory:

```sh
-d '{"repo": "acme/web", "root": "sites/docs"}'
```

## What a push builds

| The push | Builds |
| --- | --- |
| To the production branch | Production |
| To any other branch | That branch's preview, `<branch>--<app>` |
| Of a tag, or deleting a branch | Nothing |
| Changing nothing under `root` | Nothing |

The first push of a new branch always builds, because there is nothing yet to
compare it with.

When two pushes to the same branch overlap, the newer one wins. The older
build is stopped, its check is marked cancelled, and its deploy credential is
revoked before it can land over the newer release.

## The build

A build runs in a sandbox, in the directory the link names:

1. The platform fetches the commit's source. The build itself never holds a
   GitHub credential.
2. `bun install`, with bun 1.4.4. With a `bun.lock` (or `bun.lockb`) in the
   directory, it runs with `--frozen-lockfile`. Without one, bun resolves
   from `package.json` and migrates any other lockfile it can read.
3. `bun run build`, when `package.json` has a `build` script.
4. `pilvia app deploy`, the same [content-addressed deploy](/hosting/deploys.md)
   as from a laptop, so only changed files move.

A directory without a `package.json` skips steps 2 and 3 and deploys what is
there. It still needs its `pilvia.app.json`.

What the sandbox allows:

- **Network**: through the platform's proxy, on port 443 only:
  - the npm registry (`registry.npmjs.org`), for every build;
  - the hosts your app names in `pilvia.app.json` → `egress.allow`, read
    from the commit being built, so a site that fetches its CMS while it
    prerenders can build. They mean what they mean at run time
    (`*.example.com` is the names below `example.com`, not `example.com`
    itself). Plain `http://` entries, other ports and `"egress": "all"` do
    not reach a build.

  A dependency from anywhere else, such as a git URL or another registry,
  fails to install. The build's record (`GET /v1/apps/{name}/builds/{id}`)
  lists the hosts it was given as `egress`.

  Some build steps only warn when they cannot reach the network. Astro's
  fonts API with `fontProviders.google()` is one: refused, it builds the
  site without its fonts. Such a site names `fonts.google.com`,
  `fonts.googleapis.com` and `fonts.gstatic.com` in `egress.allow`.
- **Resources**: 2 CPUs and 4 GiB of memory, bursting to 4 CPUs and 8 GiB,
  and 10 GiB of scratch disk.
- **Time**: 15 minutes of building. A build that has not finished within an
  hour of its push, waiting included, is dropped.
- **Credentials**: one deploy token for this one build, which can deploy only
  to the build's target and is revoked when the build ends. The app's
  [secrets](/reference/cli.md) belong to the running app and are not given to
  the build, and there are no build-time variables yet. `CI` is set.

Every build installs its dependencies from scratch.

## On GitHub

- **A check per build**, named `Pilvia: <app>`, on the commit it built. It is
  in progress while the build runs, then ends as *Deployed*, *Failed*,
  *Timed out*, or *Built, but nothing was deployed*. Its details carry the
  build's own URL, the preview or production URL, and the end of the build
  log.
- **A comment per app** on a pull request from a branch of the same
  repository. It is written when the pull request opens, with the preview
  link, and updated as that branch's builds finish. It is one comment per
  app, edited in place, never a new one per push.
- **Pull requests from forks are not built.**

## Provenance

A release built from GitHub records the commit it was built from and the
branch that commit came from. Both are taken from the build itself, never
from anything inside the repository, so a release cannot claim a commit it
was not built from.

## Not yet

- Re-running a build from GitHub's check page. Push again instead.
- Locking production to GitHub. See
  [Git-push deploy](/hosting/git-push-deploy.md#locking-production-to-git).
- Pull requests from forks.
- Removing a branch's preview when the branch is deleted on GitHub.
  [Remove it by hand](/hosting/previews.md#removing-a-branch-preview) instead.

## The REST surface

| Request | Who | Does |
| --- | --- | --- |
| `PUT /v1/apps/{name}/source` | a token for the app, not pinned | link: `{repo, root, branch}` |
| `GET /v1/apps/{name}/source` | a token for the app | the link |
| `GET /v1/apps/{name}/builds` | a token for the app | its builds, newest first |
| `GET /v1/apps/{name}/builds/{id}` | a token for the app | one build, with the end of its log |
| `POST /v1/apps/{name}/builds` | operator | start a build of a commit by hand: `{source: {github: {repo, commit}}, branch}` |

A build's `phase` is `queued`, `running`, `succeeded` or `failed`. A build
started by hand records a branch only when GitHub confirms the commit is on
it.
