> ## Documentation Index
> Fetch the complete documentation index at: https://docs.servbase.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy from CI

> Deploy a Servbase app from GitHub Actions or any CI, wait for the build, and fail the pipeline when it fails.

Apps connected to GitHub already deploy on every push to their branch. Use the API when you want to deploy from CI after your own tests pass, or for apps that are not connected to GitHub.

> [!TIP]
>
> If your app is connected to GitHub and you deploy from CI, turn off **Automatic deploys** in the app's settings so each push does not deploy twice.

## 1. Create a deploy key

In the dashboard, open **API** and create a key with the scopes `apps:read` and `apps:deploy`, limited to the one application. Save it as a CI secret named `SERVBASE_API_KEY`, and the app's ID as `SERVBASE_APP_ID`.

## 2. Add the deploy script

The script starts a deployment, waits until it finishes, prints the build log, and exits with an error if the build failed.

```bash scripts/servbase-deploy.sh theme={null}
#!/usr/bin/env bash
set -euo pipefail
API="https://api.servbase.net"
AUTH="Authorization: Bearer ${SERVBASE_API_KEY:?missing}"
APP="${SERVBASE_APP_ID:?missing}"

start=$(curl -fsS -X POST "$API/app/apps/$APP/deploy" -H "$AUTH")
id=$(echo "$start" | jq -r '.deployment_id')
echo "Deployment $id started"

for _ in $(seq 1 180); do            # up to 30 minutes
  status=$(curl -fsS "$API/app/apps/$APP/deployments/$id" -H "$AUTH" | jq -r '.deployment.status')
  case "$status" in
    success) echo "Deployment $id succeeded"; exit 0 ;;
    failed)
      echo "Deployment $id failed. Build log:"
      curl -fsS "$API/app/apps/$APP/deployments/$id/log" -H "$AUTH" | jq -r '.log' | tail -n 80
      exit 1 ;;
  esac
  sleep 10
done
echo "Timed out waiting for deployment $id" >&2
exit 1
```

## 3. Run it in your pipeline

> [tab] GitHub Actions
>
> ```yaml .github/workflows/deploy.yml theme={null}
> name: Deploy
> on:
>   push:
>     branches: [main]
>
> jobs:
>   deploy:
>     runs-on: ubuntu-latest
>     steps:
>       - uses: actions/checkout@v4
>       # Run your tests here first.
>       - name: Deploy to Servbase
>         env:
>           SERVBASE_API_KEY: ${{ secrets.SERVBASE_API_KEY }}
>           SERVBASE_APP_ID: ${{ secrets.SERVBASE_APP_ID }}
>         run: bash scripts/servbase-deploy.sh
> ```

> [tab] GitLab CI
>
> ```yaml .gitlab-ci.yml theme={null}
> deploy:
>   stage: deploy
>   image: alpine:3.20
>   only: [main]
>   before_script:
>     - apk add --no-cache bash curl jq
>   script:
>     - bash scripts/servbase-deploy.sh
> ```
>
> Add `SERVBASE_API_KEY` and `SERVBASE_APP_ID` as masked CI/CD variables.

> [tab] Node.js
>
> ```javascript deploy.mjs theme={null}
> const API = "https://api.servbase.net";
> const headers = { Authorization: `Bearer ${process.env.SERVBASE_API_KEY}` };
> const app = process.env.SERVBASE_APP_ID;
>
> const start = await fetch(`${API}/app/apps/${app}/deploy`, { method: "POST", headers });
> if (!start.ok) throw new Error(`Deploy refused: ${(await start.json()).detail}`);
> const { deployment_id: id } = await start.json();
>
> for (let i = 0; i < 180; i++) {
>   await new Promise((r) => setTimeout(r, 10_000));
>   const { deployment } = await (await fetch(`${API}/app/apps/${app}/deployments/${id}`, { headers })).json();
>   if (deployment.status === "success") process.exit(0);
>   if (deployment.status === "failed") {
>     console.error(deployment.deploy_log.split("\n").slice(-80).join("\n"));
>     process.exit(1);
>   }
> }
> throw new Error(`Timed out waiting for deployment ${id}`);
> ```

## Common responses

| Response | Meaning |
| - | - |
| `409` "A deployment is already in progress" | Wait for the running deployment, or let the script retry later. |
| `409` "Redeploy is not allowed for static HTML and PHP apps" | Static HTML and PHP apps not connected to GitHub are edited live in the file manager. Connect the app to GitHub to deploy it from CI. |
| `404` | The key is not limited to this app, or the app ID is wrong. |
| `403` | The key lacks `apps:deploy` or `apps:read`. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.