# Git deploys

> Deploy from GitHub, GitLab or any Git server with build commands, shared paths, zero-downtime releases, rollbacks and push to deploy.

A site can deploy its code from a Git repository: every deploy builds a new release next to the live one and switches to it only when it works.

## 1. Connect the repository

**GitHub, without deploy keys**: connect your account under **Settings → GitHub** (see [Integrations](/docs/integrations)) and choose the repositories Zefily may read. On the site's **Deploy** pane, pick the account and the repository from the list.

**Any Git server** (GitLab, Bitbucket, Gitea, your own): enter the repository address (`git@host:owner/repo.git` or `https://…`). Zefily creates a **deploy key** for the site; add it to the repository with read-only access.

## 2. Set up the deploy

On the site's **Deploy** pane, **Set up Git deploys**:

| Setting | Meaning |
| --- | --- |
| Preset | Fills sensible defaults for Laravel, PHP, Static, Node.js or Python |
| Repository, Branch | Where the code comes from |
| Public directory | The folder the web server serves inside the release (e.g. `public`) |
| Build commands | One per line, run as the site user in the new release before it goes live; `composer`, `php`, `node`, `npm` are the site's versions |
| Shared paths | Kept between releases, e.g. `.env` and `storage` |
| Releases kept | How many old releases stay for rollbacks |

A Laravel preset, for example:

```
composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction
php artisan migrate --force
php artisan optimize
```

### zefily.toml

Deploy settings can also live in the repository, next to the code:

```toml
[deploy]
build = ["composer install --no-dev", "php artisan migrate --force"]
shared = [".env", "storage"]
keep = 5
```

Keys present in `zefily.toml` replace the panel's settings for that deploy; unknown keys are an error, so a typo never silently changes what runs.

### Ready to deploy?

Before the first deploy, **Check access** under **Ready to deploy?** asks the server to read the repository with its deploy key (or the GitHub App) and to find the branch. It tells you whether the key still has to be added to the repository, or whether the branch name is wrong — before a deploy fails on it.

## How a release goes live

1. The code is fetched into a new release directory (`releases/<timestamp>`).
2. Shared files and folders are linked in.
3. Build commands run as the site user.
4. With **Back up the database before each release**, the site's linked database is dumped to `~/backups` on the server.
5. The **release command** (for example `php artisan migrate --force`) runs once in the new release. If it fails, the current release keeps running.
6. The site switches to the new release atomically; app processes and the site's workers restart.
7. A **health check** asks the site for its home page. If it does not answer (a 5xx error or no answer), the site switches back to the previous release automatically and the deploy log explains why.

The deploy log on the pane shows every step. **Deploy now** starts a deploy; **Roll back** returns to the previous release instantly.

After a deploy the pane lists the **commits since the last release** (up to 50) with a link to compare them on GitHub, GitLab or Bitbucket.

## Files the app wrote outside shared paths

A release is built from the repository, so files the running app wrote next to its build (uploads, a SQLite database, generated files) are not in a new release. Before the switch, the agent compares the live release with the file list it recorded when that release went live. When the app wrote files outside the shared paths, the deploy (or rollback) stops and names the paths with their file count, size and whether they hold an SQLite database. Choose for each path:

- **Keep**: the path becomes a shared path. Its live copy moves into `shared/` and stays across deploys, from now on.
- **Ignore this path**: its files may be lost at each deploy from now on. The choice stays on the site, so you are not asked again.
- **Deploy anyway** (or **Roll back anyway**): goes on without the files of the paths you did not choose. Before a release is removed, its such files are archived to `~/backups/<release>-runtime-data.tar`; a release with more than 2 GB of them stays on disk.

Not checked: build output and caches (`vendor`, `node_modules`, `.next`, `public/build`, `bootstrap/cache`, `storage/framework`, `tmp`), logs (`*.log`), Python bytecode and `.git`. Push deploys stop the same way, and the failed deploy notice gives the reason. The check runs twice: before the build (fast feedback) and right before the switch, after the release command (a refusal there says the release command already ran). After the switch the previous release is compared once more, and files the app wrote since the last check are archived to `~/backups/<release>-late-runtime-data.tar`; the deploy log and result name them. Kept paths are copied again right before the switch. Keeping a directory keeps all of it, so the repository's files in it are no longer updated by deploys. Agents older than this release do not check. Containers keep their data in volumes and have their own check.

## Release options

Under **Release options** on the Deploy pane:

- **Root directory (monorepo)**: the repository directory that becomes the release, for example `apps/web`. Build commands, shared paths and the web root are relative to it.
- **Release command**: runs once per deploy, before the release goes live (see above).
- **Back up the database before each release**: needs a database linked to the site. Backups are listed with the server's database exports.

## Release from a ZIP

A PHP, static, Node or Python site that does not deploy from Git can be released from a ZIP: choose **Upload ZIP** on the Deploy pane (up to 1 GB). The archive is checked on the server (no paths outside the release, no links), unpacked into a new release (a single top folder, as in GitHub's **Download ZIP**, is removed) and goes live with the same build commands, shared paths, release command, backup and health check as a Git deploy. **Roll back** returns to the previous release. A site that served its files directly moves to `current/` on its first ZIP release.

A Node.js or Python site has its **Build commands** and **Start command** fields in the ZIP block too. Until you save them they show the same presets as a Git deploy (`npm ci`, `npm run build --if-present`; `uv sync --frozen --no-dev`). The release starts the app's process before it switches, and the health check asks the app on its own port: a release whose app does not answer fails with that reason, and a first release has no earlier one to return to.

## Processes

Besides the web process, a site can run **workers** (a command kept running, with 1–16 instances, for example `node worker.js` or `php artisan queue:work --queue=mail`) and **schedules** (cron syntax, for example `0 6 * * *`). They run as the site user in the live release; workers restart on every deploy. They also appear in the server's [cron and daemons](/docs/cron-and-daemons) sections.

## Push to deploy

**Turn on push to deploy**, then **Show webhook** gives a URL and a secret. Add a webhook for push events in your Git host:

- **GitHub**: content type `application/json`, the secret as the webhook secret. Repositories connected through the GitHub App need no webhook: with push to deploy on, the app's push events start the deploy.
- **GitLab**: the secret as the secret token.
- **Other tools** (CI, scripts): send the secret as a Bearer token.

Every push to the branch then deploys. **New secret** rotates it; **Turn off** stops it. Deploys can also be started from CI with the [API and CLI](/docs/api-and-cli).

### Only when certain files change

In a monorepo, **Deploy only when these paths change** (under **Release options**) limits push to deploy to the pushes that touch your app. Write one path per line from the repository root: `apps/web/` covers a whole folder, `*` matches within a folder (`config/*.php`), `**` across folders (`**/*.blade.php`). A push that changes none of them is acknowledged and skipped. When the Git host does not list every commit of a push (more than 20 on GitHub), the push deploys to be safe. Deploys you start yourself, from the panel, the API or the CLI, always run.

## Laravel extras

Laravel sites get two switches on the Deploy pane: the **Scheduler (every minute)** and the number of **Queue workers**, managed as a cron entry and daemons. See [Laravel](/docs/laravel).

## Environment variables

The `.env` is edited on the same pane — see [Logs and environment variables](/docs/logs-and-env). Values many sites share (an SMTP password, an API key) can live in **Settings → Shared variables** and be referenced as `${{shared.NAME}}`.

---
Source: https://zefily.com/docs/deploy-from-github
