# Deploy from CI

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

Deploy from GitHub Actions or GitLab CI with an API key, after your tests pass, with the CLI or with the API alone.

Your CI can deploy a service once its tests pass. It signs in with an API key in `OPENPANEL_TOKEN` and runs `openpanel deploy`, or calls the API directly. The [CLI in CI](/docs/cli/ci) page lists what the CLI reads and prints there.

## 1. Make an API key for CI [#1-make-an-api-key-for-ci]

Make the key while signed in as yourself (`openpanel login`), since a key cannot make other keys. Give it the **deploy** scope and only the project it deploys:

```sh
openpanel keys create --name "GitHub Actions" --scope deploy --project shop
```

The key is printed once. The key acts as you, never with more access than you have, and stops working if you leave the organization. See [Authentication](/docs/api/authentication).

## 2. Store it in your CI [#2-store-it-in-your-ci]

| CI | Where |
|---|---|
| **GitHub Actions** | Repository, Settings, Secrets and variables, Actions: a repository secret named `OPENPANEL_TOKEN`. |
| **GitLab CI** | Project, Settings, CI/CD, Variables: a variable named `OPENPANEL_TOKEN`, masked and protected. |

## 3. Deploy [#3-deploy]

### A service built from Git [#a-service-built-from-git]

Deploy the exact commit your CI tested with `--commit`, and wait for the result with `--wait`: the step fails when the deployment fails.

<Tabs items={["GitHub Actions","GitLab CI"]}>
<Tab value="GitHub Actions">
```yaml title=".github/workflows/deploy.yml"
name: Deploy
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 50
    steps:
      - uses: actions/setup-node@v7
        with:
          node-version: 24
      - run: npx --yes https://cdn.openpanel.sh/cli/0.2.0/openpanel-cli-0.2.0.tgz deploy web --project shop --commit "$GITHUB_SHA" --wait
        env:
          OPENPANEL_TOKEN: ${{ secrets.OPENPANEL_TOKEN }}
```

To deploy only after your tests, put this job after them with `needs:`.
</Tab>

<Tab value="GitLab CI">
```yaml title=".gitlab-ci.yml"
deploy:
  stage: deploy
  image: node:24
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
  script:
    - npx --yes https://cdn.openpanel.sh/cli/0.2.0/openpanel-cli-0.2.0.tgz deploy web --project shop --commit "$CI_COMMIT_SHA" --wait
```

GitLab passes `OPENPANEL_TOKEN` to the job from the project's CI/CD variables. The `deploy` stage runs after `build` and `test`.
</Tab>
</Tabs>

### An image you build in CI [#an-image-you-build-in-ci]

Build and push the image, point the service at the new tag, then deploy. The image must be public, since your servers pull without signing in to a registry: on GitHub, check the package's visibility in its settings.

```yaml title=".github/workflows/deploy.yml"
name: Deploy
on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    permissions:
      contents: read
      packages: write
    steps:
      - uses: actions/checkout@v7
      - run: |
          echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
          docker build -t "ghcr.io/acme/shop:$GITHUB_SHA" .
          docker push "ghcr.io/acme/shop:$GITHUB_SHA"
      - uses: actions/setup-node@v7
        with:
          node-version: 24
      - run: |
          npx --yes https://cdn.openpanel.sh/cli/0.2.0/openpanel-cli-0.2.0.tgz services update web --project shop --image "ghcr.io/acme/shop:$GITHUB_SHA"
          npx --yes https://cdn.openpanel.sh/cli/0.2.0/openpanel-cli-0.2.0.tgz deploy web --project shop --wait
        env:
          OPENPANEL_TOKEN: ${{ secrets.OPENPANEL_TOKEN }}
```

Tagging each image with its commit keeps every tag pointing at one build, so a [rollback](/docs/deploy/rollbacks) runs exactly what ran before.

## Without the CLI [#without-the-cli]

The same deploy with `curl` and `jq`, which GitHub's Ubuntu runners have. It starts the deployment, then reads it every five seconds until it runs or fails:

```sh
deployment=$(curl -sS --fail-with-body -X POST "https://openpanel.sh/api/v1/services/$SERVICE_ID/deployments" \
  -H "Authorization: Bearer $OPENPANEL_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"commit\": \"$GITHUB_SHA\"}") || { echo "$deployment"; exit 1; }
id=$(echo "$deployment" | jq -r .id)

while true; do
  status=$(curl -sS --fail-with-body "https://openpanel.sh/api/v1/deployments/$id" \
    -H "Authorization: Bearer $OPENPANEL_TOKEN" | jq -r .status)
  echo "$status"
  case "$status" in
    running|superseded|cancelled) exit 0 ;;
    failed) exit 1 ;;
  esac
  sleep 5
done
```

`$SERVICE_ID` is the service's id, from `openpanel services get web --project shop`. For an image service, leave out the `Content-Type` and `-d` lines. When OpenPanel refuses the deploy, the step prints its answer, which says why. See [Deploy a service](/docs/api/deployments/deploy-a-service) and [Get a deployment](/docs/api/deployments/get-a-deployment).

## How long a deploy can take [#how-long-a-deploy-can-take]

A build on your server can run for 30 minutes, and a rollout then has 15 minutes to become ready; past either, OpenPanel marks the deployment failed. A job that waits for a Git service may wait about 45 minutes in the worst case, which is why the first workflow sets `timeout-minutes: 50`. An image service skips the build, so its rollout's 15 minutes are the most it waits on OpenPanel.