Skip to content
Kaiov0.8.0

Deploy from CI

The CLI is a thin HTTP client: it talks to a Kaio server and nothing else. A pipeline therefore needs no checkout of Kaio and no toolchain, just the published image with its entrypoint overridden.

Terminal window
docker run --rm --network host -e KAIO_ADDR \
--entrypoint kaio-cli ghcr.io/rgaidot/kaio:latest health
.github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: self-hosted
env:
KAIO_ADDR: ${{ secrets.KAIO_ADDR }}
KAIO: >-
docker run --rm --network host -e KAIO_ADDR
-v ${{ github.workspace }}:/w -w /w
--entrypoint kaio-cli ghcr.io/rgaidot/kaio:latest
steps:
- uses: actions/checkout@v4
- name: Refuse to deploy to a server that is not answering
run: $KAIO health
- name: Deploy, and roll back if it does not come up healthy
run: $KAIO stack deploy blog compose.yml --env-file .env.ci --wait
.gitlab-ci.yml
deploy:
stage: deploy
image: ghcr.io/rgaidot/kaio:latest
variables:
KAIO_ADDR: $KAIO_ADDR
script:
- kaio-cli health
- kaio-cli stack deploy blog compose.yml --env-file .env.ci --wait
only:
- main

The image ships both binaries, so a job that runs in it calls kaio-cli directly. The entrypoint only needs overriding when the image is invoked with docker run.

health first. A server that is down fails the job before anything is written, instead of halfway through a deploy.

stack deploy is idempotent. It rewrites the compose file, recreates the containers and records a new version, so re-running the pipeline on an unchanged file is safe. A failed compose up restores the previous version on its own and the step fails, so a bad commit leaves the last working stack running.

--wait checks what you deployed. Without it, stack deploy returning 0 means compose up went through, not that the containers stayed up. With it, the server waits until every container of the stack runs and passes its healthcheck, or has exited with 0 for a one-shot service, and answers only then. A container that exits with an error, is killed for memory or fails its healthcheck rolls the stack back to the version it ran before, and the step fails with code unhealthy, naming each container at fault with its last log lines. A stack still not ready after --wait-timeout seconds (120 by default, 600 at most) is treated the same way. A container without a healthcheck counts as up once it has run for five seconds, so give the services that matter a healthcheck: it is what tells a crash at startup from a crash a minute later. A new stack has no earlier version to go back to and is left as deployed.

Two jobs never deploy over each other. While one call changes a stack, another on the same stack fails at once with code busy instead of racing it, so two pipelines, or a pipeline and an agent, cannot interleave a deploy and an update. --expected-version goes one step further: the job names the version it was built against, and when someone deployed since, it fails with code version_moved rather than overwrite that deploy.

The control plane forwards to any node in its registry, so the pipeline keeps talking to one address and names the node it wants:

Terminal window
kaio-cli --json node ls | jq -r '.[] | select(.status == "online") | .name'
- name: Deploy on every node that answers
run: |
for node in $($KAIO --json node ls | jq -r '.[] | select(.status=="online") | .name'); do
echo "deploying on $node"
$KAIO node "$node" run stack deploy blog compose.yml
done

A node removed from the registry drops out of the list and loses its route, so a decommissioned server cannot be deployed to by a pipeline that has not been updated.

Nothing schedules this for you. The loop is the whole of it, a node that was offline when the pipeline ran is skipped, and there is no rollback across hosts: each server deploys or restores its own previous version independently. If you need all-or-nothing across a cluster, that belongs in your pipeline, not in Kaio.

See Multiple servers for how servers join in the first place.

Terminal window
# Fail the build when a stack has a pending image update
kaio-cli --json stack ls | jq -e 'map(select(.update_available)) | length == 0'
# Fail on any critical CVE in a scanned image
kaio-cli --json image ls | jq -e '[.[].scan.summary.critical // 0] | add == 0'
# Report nodes that stopped answering
kaio-cli --json node ls | jq -r '.[] | select(.status != "online") | "\(.name) \(.last_error)"'

Kaio, built by Régis Gaidot