# Deploy from GitHub

> Connect a project to a GitHub repository and every push deploys: the production branch to production, every other branch to a preview with its own URL and a pull request comment.

- Category: Deploy
- URL: https://www.forkrail.com/docs/deploy-from-github

## Connect a repository

1. **Give your install’s GitHub App access**

   Open the project’s **Settings → Git**. The first time, **Install on GitHub** takes you to GitHub, where you pick the account and either all repositories or only the ones you choose.

2. **Connect the repository**

   Back on Settings → Git, the repositories the app can read are listed, most recently pushed first. Click **Connect**.

3. **Check the production branch and root directory**

   The production branch is the repository’s default branch unless you change it. Set the root directory if the project lives in a subfolder of a monorepo.

Or start from the repository: **New project → Import**, name the project, pick the branch and root directory, paste any environment variables, and click **Deploy**. The production branch builds at once and you land on its build log.

**The same from a terminal (with the install’s AWS credentials)**

```bash
forkrail git repos                                # what the app can read
forkrail git connect shop acme/shop               # pushes to the default branch go to production
forkrail git connect shop acme/mono --branch release --root apps/shop
forkrail git connect shop acme/shop --create      # make the project from the repo and deploy it
forkrail git deploy shop                          # deploy the production branch's newest commit now
forkrail git disconnect shop
```

## What a push does

- GitHub sends a signed webhook to your dashboard. forkrail fetches that commit’s files and builds them in your builder exactly like `forkrail deploy`, with the project’s environment variables and build cache.
- The commit gets a status, `forkrail/<project>`: pending while it builds, then a link to the deployment or to the failed build’s log.
- GitHub is told about the deployment itself, which puts **View deployment** on the pull request.
- Two pushes to the production branch close together both build. If the older one finishes last, production stays on the newer one.
- Pushes from forks aren’t built.

## Branch URLs and pull request comments

Besides each deployment’s own URL, every branch gets an address that follows its newest ready preview: `<project>-git-<branch>.<your domain>`, with the branch lowercased and anything but letters and digits turned into dashes. `feature/Logs` becomes `shop-git-feature-logs.example.com`. A failed, canceled or skipped build leaves it where it was.

Every open pull request gets one comment per connected project, updated as the build goes: building, ready with the branch URL, failed with the step and a link to the log, canceled or skipped.

When a branch is deleted, usually after a merge, its previews are removed with their Lambda versions and its branch URL stops answering. Anything that has been production is kept as a rollback target.

## Monorepos

Connect one repository to several projects, each with its own root directory (`apps/web`, `services/api`). A push builds all of them. Dependencies install with the workspace’s package manager: the lockfile is found in the folders above the project, up to the repository’s top.

## Pushes that don’t deploy

Two `vercel.json` keys, read from the pushed commit, work as on Vercel:

**Turn off deploys for some branches**

```json
{
  "git": {
    "deploymentEnabled": { "dependabot/**": false, "wip-*": false }
  }
}
```

**Skip a build when a command exits 0**

```json
{
  "ignoreCommand": "[ \"$VERCEL_GIT_COMMIT_REF\" = docs ]"
}
```

- `git.deploymentEnabled: false` turns pushes off entirely; an object turns them off per branch pattern (`*` within a path segment, `**` across segments).
- `ignoreCommand` runs at the start of a push’s build. Exit 0 skips the build: it’s listed as canceled with the command’s output, and the commit status is a success so required checks don’t block a merge. A build has the commit’s files but not the git history, so `git diff HEAD^ HEAD` can’t be used yet.
- `github.autoJobCancelation` (on by default) cancels a branch’s older builds when a newer push arrives. `github.autoAlias: false` builds production deployments without putting them live.

## Deploy hooks

A deploy hook is a secret URL that builds a branch’s newest commit when called, for a CMS that publishes or a scheduled job. Create one under **Settings → Git → Deploy Hooks** or from the CLI. The URL is shown once.

```bash
forkrail deploy-hooks create shop "Sanity publish" --branch main   # prints the URL once
forkrail deploy-hooks ls shop
forkrail deploy-hooks rm shop 3f9a0c1b2d4e

curl -X POST "https://dashboard.example.com/api/deploy-hooks/shop/<secret>"
curl -X POST "https://dashboard.example.com/api/deploy-hooks/shop/<secret>?buildCache=false"
```

- A project can have 5 hooks, called 60 times an hour in all; beyond that the answer is `429` with `retry-after`.
- Calling a hook again for the same commit cancels the build it started before.
- Only the secret’s SHA-256 is stored. Anyone with the URL can deploy the branch, so treat it like a password.

## Limits

- A commit’s files may be up to 256 MB zipped.
- Git submodules and Git LFS files aren’t fetched.
- Builds get the commit’s files, never a GitHub token. The app’s private key is readable only by the control function and your AWS administrators.
