# Deploy from Git

Source: https://openpanel.sh/docs/deploy/git

Build a service from a repository, a branch and a folder in one of your Git accounts, on your own server, at the branch's newest commit or the one you name.

A service can come from a Git repository. Each deploy clones it on one of your servers, builds an image there, and then starts it. Nothing is built on OpenPanel's own servers.

## What a Git service needs [#what-a-git-service-needs]

| Setting | What it takes |
|---|---|
| **Git account** | One of your organization's [Git accounts](/docs/git), by its id. `openpanel git` lists them. |
| **Repository** | `owner/name`, such as `acme/shop`; GitLab groups can be deeper, such as `acme/web/shop`. |
| **Branch** | The branch to build. `main` when you give none. |
| **Folder** | For a monorepo: the service's folder in the repository, such as `apps/web`. The repository's root when you give none. A folder cannot lead out of the repository. |
| **Build** | Railpack, which works out how to build the folder, or a Dockerfile you name. See [How builds work](/docs/builds). |

For an account added as one repository over SSH or HTTPS ([Any Git server](/docs/git/any-git-server)), the build clones that repository. The repository setting is still required: give its `owner/name` as the Git host shows it.

## Create a service from a repository [#create-a-service-from-a-repository]

<Tabs items={["Console","CLI","API"]}>
<Tab value="Console">
Open the project. Click **New service** and choose **A Git repository**. Choose the **Git account**, the **Repository** and the **Branch**, and for a monorepo the **Folder**. Under **Build and start**, keep **Auto** or choose **Dockerfile**. Tick a server under **Servers it may run on**, and click **Create service**.
</Tab>

<Tab value="CLI">
```sh
openpanel git

openpanel services create web --project shop \
  --repo acme/shop --git-account "$GIT_ACCOUNT_ID" --branch main --path apps/web \
  --public --port http:3000 --server my-server
```

To build with a Dockerfile instead of Railpack, add `--dockerfile Dockerfile`; the path is inside the service's folder. See [Dockerfile builds](/docs/builds/dockerfile).
</Tab>

<Tab value="API">
```sh
curl -X POST "https://openpanel.sh/api/v1/projects/$PROJECT_ID/services" \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "web",
    "source": { "kind": "git", "accountId": "'"$GIT_ACCOUNT_ID"'", "repo": "acme/shop", "branch": "main", "path": "apps/web" },
    "exposure": "public",
    "ports": [{ "protocol": "http", "port": 3000 }],
    "serverIds": ["'"$SERVER_ID"'"]
  }'
```

Leave `build` out to build with Railpack, or send `"build": { "kind": "dockerfile", "dockerfile": "Dockerfile" }`.
</Tab>
</Tabs>

## Deploy a branch or a commit [#deploy-a-branch-or-a-commit]

A deploy builds the branch's newest commit. To build an exact commit, give its hash, 7 to 64 hexadecimal characters:

<Tabs items={["Console","CLI","API"]}>
<Tab value="Console">
Open the project and click the service on the canvas. To build another branch, change the **Branch** in the **Source** card of its **Settings** tab, click **Save**, then **Deploy** on its **Overview** tab. The console always builds the branch's newest commit; to build an exact commit, use the CLI or the API.
</Tab>

<Tab value="CLI">
```sh
openpanel deploy web --project shop --commit 3f9c2a1 --wait
```
</Tab>

<Tab value="API">
```sh
curl -X POST "https://openpanel.sh/api/v1/services/$SERVICE_ID/deployments" \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "commit": "3f9c2a1" }'
```
</Tab>
</Tabs>

The deployment moves through `queued` (waiting for the server to start the build), `building`, `deploying` and `running`. Once the build ends, the deployment's `commit` field still shows what you asked for; the build's own output names the full commit it built. To deploy on every push instead, turn on [push to deploy](/docs/deploy/push-to-deploy).

## Where the build runs [#where-the-build-runs]

The build runs on the first server the deployment is placed on, by OpenPanel's agent there, one build at a time per server. The agent:

1. clones the branch's newest commit, or the commit you named, without the rest of the history where the Git host allows it;
2. builds the service's folder with its Dockerfile or with Railpack;
3. keeps the image on that server under a name of its own for this deployment, and reports that the build succeeded;
4. starts the new containers once the deployment is `deploying`.

See [How builds work](/docs/builds) for the details, the time limit and the cache.

<Callout type="info" title="A Git service on several servers">
A Git service placed on more than one server is built once and pushed to your organization's own registry, and each of its servers pulls the image from there. The registry travels over the private network, so every server of the service must be in it; OpenPanel refuses the deploy while one is not, for example a server added with One command. See [Replicas](/docs/load-balancing/replicas).
</Callout>

## Private repositories [#private-repositories]

A private repository needs a Git account with a token, a password or a deploy key. The account's credentials leave OpenPanel sealed for its gateway, which hands them to the agent for this one build:

- a token or password goes in an HTTP header for the repository's own host, never in the address or on a command line;
- an SSH key goes to a file only that build reads, and is deleted with the build's folder when it ends.

## What can go wrong [#what-can-go-wrong]

| Message | What it means |
|---|---|
| "That Git account was not found." | The account id is wrong, the account was removed, or it is another member's own account. |
| "Choose the repository as owner/name." | The repository is not in the `owner/name` form. |
| "A commit is its hexadecimal hash." | `--commit` or `commit` was not 7 to 64 hexadecimal characters. A branch name does not go there. |
| "Choose at least one server for this service before deploying." | The service has no server it may run on, or all of them are disconnected. |
| "The build failed: …" | The clone or the build stopped. See [A build fails](/docs/troubleshooting/build-fails). |

<Cards>
<Card href="/docs/builds" title="How builds work" description="Railpack or a Dockerfile, the time limit and the cache." />

<Card href="/docs/deploy/push-to-deploy" title="Push to deploy" description="A webhook per service that deploys each push." />

<Card href="/docs/git" title="Git accounts" description="Connect GitHub, GitLab, Gitea, Forgejo, Bitbucket or any Git server." />
</Cards>