# Build from your CI

You do not need CI to build on Zaroz: a push starts a build. CI is for when you want your **own tests to decide** whether a commit goes live, or when something other than a push should start a build. There are three ways to combine them.

| You want | Use |
|---|---|
| Zaroz builds on every push, but only deploys when your CI is green | [Wait for checks](#let-your-ci-be-the-gate-wait-for-checks) |
| Your CI runs the tests, then asks Zaroz to build that commit and waits for the result | [The API](#start-a-build-from-ci-with-the-api) |
| A tool or a schedule starts a build and needs no token | [A deploy hook](#start-a-build-with-a-deploy-hook) |

If your CI builds the image itself and only tells Zaroz to run it, that is the other way: see [Deploy from CI with an API token](/guides/ci/overview).

## Let your CI be the gate: wait for checks

In the service's **Source** tab, under **When it builds and deploys**, turn on **Wait for checks**.

- A push builds **right away**, so the image is ready.
- The build **deploys only after the other checks on that commit pass**: your tests in GitHub Actions, GitLab CI or Forgejo Actions.
- If a check fails, the build stays built and does not deploy. You can still choose **Deploy this build** by hand.

The build page shows **Built. Waiting for this commit's other checks before deploying.** while it waits. Zaroz waits for a limited time, and then the build stays built, not deployed.

This works for GitHub, GitLab, Forgejo and Gitea connections. A plain git address has no checks to read, so builds there deploy without waiting.

## Start a build from CI with the API

Run your tests first and ask Zaroz to build the commit that passed. The job fails if the build fails.

### Create a token

Make a project token as described in [Create a token that can only deploy](/guides/ci/overview#create-a-token-that-can-only-deploy), with these permissions:

| Permission | Why |
|---|---|
| `services.deploy` | Start, cancel and retry builds, and deploy one |
| `services.read` | Wait for the build and read its result |

Add the token as a secret of your CI. The service instance id is the UUID in the service's URL in the console.

### The two calls

Start the build for a commit. `deploy: true` deploys it when it succeeds, whatever the service's auto-deploy setting says:

```bash
curl -sS -X POST "$ZAROZ_API/service-instances/$SERVICE/builds" \
  -H "Authorization: Bearer $ZAROZ_TOKEN" -H "Content-Type: application/json" \
  -d "{\"sha\": \"$GITHUB_SHA\", \"deploy\": true}"
# {"id": "...", "number": 42, "status": "queued", ...}
```

All fields are optional. A bare `POST` builds the head of the branch the service follows.

| Field | Meaning |
|---|---|
| `ref` | A branch name or full ref. Default: the branch the service follows. |
| `sha` | A commit on that ref. Default: its head. |
| `deploy` | `true` deploys on success. `false` leaves the build for **Deploy this build**. Unset: the service's auto-deploy setting decides, and a ref outside its rule does not deploy. |
| `no_cache` | Build without the cache. |
| `size` | A builder size, `small`, `medium` or `large`. It changes the minutes used. |

Then wait for it. The call returns when the build ends or after up to 10 minutes, and says which:

```bash
curl -sS "$ZAROZ_API/service-instances/$SERVICE/builds/$BUILD_ID/wait?timeout=600" \
  -H "Authorization: Bearer $ZAROZ_TOKEN"
# {"build": {"status": "succeeded", "terminal": true, ...}, "timed_out": false}
```

If `timed_out` is `true`, the build is still running. Call again. When `terminal` is `true`, `build.status` is `succeeded`, `failed`, `canceled`, `superseded`, `expired` or `skipped`. A failed build has `failure_code` and `failure_detail`: the codes are the ones in [Troubleshooting](/git-builds/troubleshooting).

### A script that fails the job

```bash
#!/usr/bin/env sh
# Build a commit on Zaroz and fail if the build fails.
#   ZAROZ_TOKEN=zaroz_... ZAROZ_SERVICE=<uuid> SHA=<commit> ./zaroz-build.sh
# Needs: curl, jq.
set -eu
: "${ZAROZ_TOKEN:?}" "${ZAROZ_SERVICE:?}" "${SHA:?}"
API="${ZAROZ_API:-https://kernel.zaroz.cloud/api/v2}"
BASE="$API/service-instances/$ZAROZ_SERVICE"

# The token goes through curl's config on stdin so it never shows in `ps`.
zcurl() { printf 'header = "Authorization: Bearer %s"\n' "$ZAROZ_TOKEN" | curl -fsS -K - "$@"; }

BUILD_ID=$(zcurl -X POST "$BASE/builds" -H "Content-Type: application/json" \
  -d "{\"sha\": \"$SHA\", \"deploy\": true}" | jq -r .id)
echo "Zaroz build $BUILD_ID started"

while :; do
  RESULT=$(zcurl "$BASE/builds/$BUILD_ID/wait?timeout=300")
  [ "$(echo "$RESULT" | jq -r .build.terminal)" = "true" ] && break
done

STATUS=$(echo "$RESULT" | jq -r .build.status)
echo "Build finished: $STATUS"
[ "$STATUS" = "succeeded" ] || {
  echo "$RESULT" | jq -r '.build | "\(.failure_code // "-"): \(.failure_detail // "")"' >&2
  exit 1
}
```

The job's exit code is the build's result. A build that succeeds and has `deploy: true` is deployed by Zaroz itself, on the server, so closing the job loses nothing. `build.deployment_id` is the deployment it started, once it has.

### In your CI

**GitHub Actions**

```yaml
  zaroz-build:
    needs: test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4          # for the script
      - run: sh deploy/zaroz-build.sh
        env:
          ZAROZ_TOKEN: ${{ secrets.ZAROZ_TOKEN }}
          ZAROZ_SERVICE: ${{ vars.ZAROZ_SERVICE }}
          SHA: ${{ github.sha }}
```

**GitLab CI**

```yaml
zaroz-build:
  stage: deploy
  needs: [test]
  script: sh deploy/zaroz-build.sh
  variables:
    SHA: $CI_COMMIT_SHA
```

Set `ZAROZ_TOKEN` and `ZAROZ_SERVICE` as masked CI/CD variables.

**Forgejo and Gitea Actions** use the same syntax as GitHub Actions, with `${{ github.sha }}` for the commit.

**Anything else**: run the script with the three variables set.

:::note
If CI should be the only thing that starts builds, set **When it builds** in **Source** so that it builds only when you start it. Otherwise a push builds once on its own and once from CI. A build of a commit that is already building returns that build, so the duplicate costs nothing.
:::

### If the build can cost money

A build that may go past your included minutes needs the cost to be confirmed when a person starts it. `GET /service-instances/{id}/builds/estimate` returns the most it can cost, and `confirmed_max_cost_cents` in the start request confirms it. Without it, such a start is refused. If your organization has not turned on [extra minutes](/git-builds/billing#extra-minutes), a build that does not fit is refused with [`included_minutes_exhausted`](/git-builds/troubleshooting#included_minutes_exhausted) and the job fails. See [Build minutes](/git-builds/billing).

## Start a build with a deploy hook

A **deploy hook** is a private URL. Calling it starts a build of the service. It needs no token, so it suits a tool that cannot hold one.

1. In **Source**, open **Deploy hooks**, give the hook a name and choose **Create hook**.
2. Copy the URL. **It is shown only once**, and anyone who has it can start a build, so treat it like a password.
3. Call it:

```bash
curl -sS -X POST "$DEPLOY_HOOK_URL"
```

An empty `POST` builds the head of the branch the service follows. Add `?ref=<branch>` or `?sha=<commit>` to build another one. A build for a commit or ref that is outside the service's rule is built but **waits for you to choose Deploy this build**.

- A service can have **3 hooks**, each with a name, its last use, and a **Revoke** button that stops the URL at once.
- A hook can start builds for this one service and nothing else.
- Each hook accepts up to **30 calls a minute**.

On a [plain git address](/git-builds/connecting), Zaroz finds new commits by checking every few minutes. A deploy hook starts the build at once.

## What Zaroz reports on your commits

Every build reports its progress on the commit in your git provider, so your pull requests show it:

| Provider | What you see |
|---|---|
| GitHub | A check named **Zaroz: your service** |
| GitLab and Forgejo | A status named **zaroz/your-service** |

It goes from pending to success or failure with the build, and links to the build page. Use it as a required check if you want a pull request to prove it builds before merging.

## Related

- [Deploy from CI with an API token](/guides/ci/overview): when your CI builds the image.
- [Build settings](/git-builds/build-settings): the rules for what builds and deploys.
- [Limits](/git-builds/limits): concurrent builds and the queue.
