Zaroz
Git builds

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 wantUse
Zaroz builds on every push, but only deploys when your CI is greenWait for checks
Your CI runs the tests, then asks Zaroz to build that commit and waits for the resultThe API
A tool or a schedule starts a build and needs no tokenA 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.

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, with these permissions:

PermissionWhy
services.deployStart, cancel and retry builds, and deploy one
services.readWait 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:

TerminalCode
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.

FieldMeaning
refA branch name or full ref. Default: the branch the service follows.
shaA commit on that ref. Default: its head.
deploytrue 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_cacheBuild without the cache.
sizeA 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:

TerminalCode
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.

A script that fails the job

TerminalCode
#!/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

YAMLCode
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

YAMLCode
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.

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, a build that does not fit is refused with included_minutes_exhausted and the job fails. See Build minutes.

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:
TerminalCode
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, 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:

ProviderWhat you see
GitHubA check named Zaroz: your service
GitLab and ForgejoA 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.

Last modified on