Zaroz
Deploy from CI/CD

Deploy from CI with an API token

Every guide in this section does the same four things, in any CI system:

  1. build an image and push it to a registry, with one immutable tag per commit;
  2. tell Zaroz to run that image (PUT /service-instances/{id}/source);
  3. start a deployment and wait for it (POST /service-instances/{id}/deployments);
  4. fail the job if the rollout failed, and put the previous image back.

Steps 2 to 4 are one small script, zaroz-deploy.sh. The GitHub, GitLab and Forgejo guides only differ in how they build, where the secrets live and how they stop two deploys running at once.

CI systemGuide
GitHub ActionsGitHub Actions
GitLab CIGitLab CI
Forgejo Actions and Gitea ActionsForgejo and Gitea Actions
anything else (Jenkins, Drone, a cron on your server)Generic shell

These guides are for a CI that builds the image itself. If you would rather let Zaroz build from your repository, see Build on Zaroz from your CI below.

Create a token that can only deploy

CI should never hold a token that can read your invoices or delete your account. Give it a project token: an organization API token limited to the project that holds your Workload.

services.deploy is the deploy permission. It allows exactly two writes on a service built from an image: PUT /service-instances/{id}/source and POST /service-instances/{id}/deployments. On a service built from git it also starts, cancels and retries builds. It does not read anything, change environment variables, backups, files or the firewall, start, stop or restart the service (services.power), open a terminal, or touch billing or members. Add services.read so the job can read the source and wait for the deployment to finish.

The token is scoped to one project, so it can deploy every service in that project and nothing outside it. If a pipeline must only ever touch one service, keep that service in a project of its own.

In the console, open your organization, go to Tokens and select Create token:

  1. Name it after the pipeline, for example ci-deploy-my-api.
  2. Scope: One project, and pick the project.
  3. Permissions: services.deploy and services.read.
  4. Expiry: 90 days is the default; choose what fits your release cadence (1 to 365 days).
  5. Copy the secret into your CI's secret store. It is shown once.

For a pipeline that must keep working when its creator leaves the organization, create it as organization-owned. A membership_tied token (the default) stops with its creator's membership.

You can do the same with the API from a signed-in session:

curl

TerminalCode
# the header goes through curl's config on stdin so it never shows in `ps` zcurl() { printf 'header = "Authorization: Bearer %s"\n' "$ZAROZ_TOKEN" | curl -sS -K - "$@"; } zcurl -X POST "$ZAROZ_API/organizations/<organization uuid>/api-tokens" -H "Content-Type: application/json" -d '{ "name": "ci-deploy-my-api", "lifetime": "organization_owned", "project_id": "<project uuid>", "expires_at": "2027-02-01T00:00:00Z", "capabilities": ["services.deploy", "services.read"] }' # {"token": {...}, "secret": "zaroz_pat_..."} the secret is shown this once

TypeScript

TypeScriptCode
const res = await fetch(`${API}/organizations/${ORG}/api-tokens`, { method: "POST", headers: { Authorization: `Bearer ${SESSION_JWT}`, "Content-Type": "application/json" }, body: JSON.stringify({ name: "ci-deploy-my-api", lifetime: "organization_owned", project_id: PROJECT, expires_at: "2027-02-01T00:00:00Z", capabilities: ["services.deploy", "services.read"], }), }); console.log((await res.json()).secret);

Python

Code
r = requests.post( f"{API}/organizations/{ORG}/api-tokens", headers={"Authorization": "Bearer " + SESSION_JWT}, json={ "name": "ci-deploy-my-api", "lifetime": "organization_owned", "project_id": PROJECT, "expires_at": "2027-02-01T00:00:00Z", "capabilities": ["services.deploy", "services.read"], }, timeout=20, ) print(r.json()["secret"])
  • The service instance id (<service instance uuid> in the scripts) is the UUID in the Workload's URL in the console. The token does not need it; the job does, to say what to deploy.
  • The secret (zaroz_pat_...) is shown once. Store it straight into your CI's secret store. Never put it in the repository, in .env files that are committed, or in the image.
  • Keep the secret out of command-line arguments, which any user on a shared runner can read with ps. Pass it as an environment variable from the CI secret store, as the workflows do; the script sends it to curl through standard input.
  • Mint one token per project and per environment (staging, production), so that revoking one does not stop the others.

Before it expires: Replace

A CI token expires, and the job that fails the morning it does is the one nobody was watching. Zaroz emails the organization's Owners and Admins, and the member who created the token, 30, 15, 7 and 1 days before. When the mail arrives:

  1. In Tokens, open the token's menu and select Replace. The form is filled with the same name, project, lifetime and permissions.
  2. Create it, copy the new secret and update the variable in your CI's secret store.
  3. Run the pipeline once. When it succeeds, select Revoke the old token.

Both tokens work until you revoke the old one, so there is no window where deploys fail. An expired token answers 401.

What the token does at each step

StepRequestPermission
Read what runs nowGET /service-instances/{id}/sourceservices.read
Choose the imagePUT /service-instances/{id}/sourceservices.deploy
DeployPOST /service-instances/{id}/deploymentsservices.deploy
Wait and checkGET /service-instances/{id}/deploymentsservices.read

PUT /source does not deploy: it only saves what the next deployment will pull. POST /deployments takes no body and deploys what is saved, so there is no way to deploy something that was not saved first.

Rollout, success and rollback

A deployment has a status (how the operation ended) and an outcome (succeeded or failed, with a failure_reason such as cluster_rejected or subscription_not_active). succeeded means the rollout was applied; it does not say the new pods are healthy. So the script also reads workloads[].status from the same list and requires every workload to be running.

Rollback is a deploy of the previous image. The script reads running_image before changing anything and, if the new rollout fails, saves that image again and deploys it. Set ROLLBACK=0 to turn this off. A deploy is refused with 409 while a restore, a resize or another deployment holds the service, and the response says which operation and since when.

Zaroz does not watch a registry for a moved tag (watch_tag is always false), so a pipeline that pushes latest and deploys nothing will never update. Use a unique tag per commit, as the guides do.

Private images

If the image is private, add a pull-only registry token and pass it as REGISTRY_USERNAME and REGISTRY_PASSWORD. Zaroz stores it, never returns it, and uses it to pull from inside the cluster. It must not be the token your CI uses to push.

Build on Zaroz from your CI

If your service builds from a git repository (Git builds), you do not need to build or push an image. Your CI runs its tests, then asks Zaroz to build the commit that passed and waits for the result:

TerminalCode
curl -sS -X POST "$ZAROZ_API/service-instances/$SERVICE/builds" \ -H "Authorization: Bearer $ZAROZ_TOKEN" -H "Content-Type: application/json" \ -d "{\"sha\": \"$COMMIT_SHA\", \"deploy\": true}" curl -sS "$ZAROZ_API/service-instances/$SERVICE/builds/$BUILD_ID/wait?timeout=600" \ -H "Authorization: Bearer $ZAROZ_TOKEN"

The same token works, with services.deploy (start the build) and services.read (wait for it). Zaroz deploys the build itself when it succeeds, so the job only has to report the result. You can also let Zaroz build on every push and deploy only after your CI passes, with Wait for checks. Both ways, a ready-made script and the GitHub, GitLab and Forgejo snippets are in Build from your CI.

Example repositories

Each example has a ready deploy workflow for all three CI systems and runs end to end against a project of your own.

RepositoryWhat it shows
example-scaled-apian HTTP API on 1 to 8 autoscaled units behind one endpoint
example-queue-workerworkers that scale with a queue
example-game-networkhub, lobbies, duels and plots (Minecraft), phase 2
example-job-runnerone unit per job, started by key, result by event

They live in the zaroz-oss organization at https://git.zaroz.cloud/zaroz-oss/.

Last modified on