DocsDeploying: the path from a change to production

Deploying: the path from a change to production

A repository on a desk server deploys through desk. The landing runs the checks, builds the images, promotes them, and rolls the Deployments. A person does not push an image and does not run kubectl. This page gives the order, the config that each step reads, and the command that proves the step worked. docs/runs.md gives the detail of each run.

1. The order

  1. Make the change in a work folder of desk checkout. The folder has no .git.
  2. desk change --message "<type>(<scope>): <summary>". The server keeps the change.
  3. desk land --wait. It answers landed <change>, then log entry <n>: every check, image and deploy passed, and it exits 0 only when all of them passed.
  4. Read the result with desk runs landing <seq> --wait, or desk runs list.
  5. A store application needs one more step: desk publish <name> --track <track> --wait.

A landing of main moves the base of every work folder. Run desk sync in a work folder that you keep for a long time. docs/latency.md gives the measured time of each step of this list, from the landing to the pods.

2. What the landing starts

Step Run kind What it does Config
Checks command runs the commands of validation on a runner, one after another by needs validation[]
Images image builds each image that a changed path matches, and pushes its tags images[]
Promotion image gives an image the tag latest once the root check passed (#145) images[].tags
Deploy deploy sets each Deployment of a deploys target to the pushed digest deploys[]

A check runs on a runner of the lane runs. An image builds on the lane of the image. A deploy runs on a runner of the lane deploy, which runs no code of the repository. That runner holds the token of the cluster. See docs/runs.md for the lanes.

One deployer pool serves every repository (#179). A runner of the lane deploy with no DESK_REPOS reads GET /v1/runner/repos?lane=deploy every minute. The answer lists each repository whose config has a deploys target and where the identity of the runner has the right run, and the runner claims the deploy runs of any of them. Add a deploys target, and the pool rolls it after the next landing. Remove the targets, and the pool leaves the repository. A runner with DESK_REPOS or DESK_REPO claims only the deploy runs of those repositories, so a deployer Deployment per repository keeps its work.

At its start the server reads the config of main of every repository and sets the flag of the lane to the one that a landing sets (#182). A repository whose config has a deploys target is therefore in the pool before its next landing, and the server log names each repository whose flag it changed. A second start changes nothing. The run of a deploy names the runner that took it, in claimedBy and claimedHost (#182).

A landing restarts a pod only when the image of that pod changed. A promotion reads the digest that each tag holds now, and it moves the tag only when the digest of the build differs (src/server/promote-run.ts, #179). A tag that already holds the digest stays as it is, and the promotion does not tell Keel of it. A landing of an unchanged tree therefore moves no tag, and no Deployment rolls.

The promotion of a landing that a newer landing replaced (#192)

The head of main is built. A landing that a newer landing replaced before its checks ended still promotes the images that it built: its checks passed, so its promotion is due. The server advances the promotion of that landing when its last check or build ends, and also when the promotion of another landing ends.

A promotion never moves a tag back to older code. Before a promotion writes a tag, it reads the sequence numbers of the landings that promoted the image. If a newer landing promoted the image, the older landing records superseded and writes no tag, so the tag keeps the digest of the newer landing. If the promotion of the newer landing is queued or runs, the older landing waits.

desk runs landing <seq> shows the state of each promotion:

Line Meaning
promotion app: promoted this landing moved the tags last
promotion app: superseded by 1234 a newer landing promoted the image first, so this landing wrote no tag
promotion app: skipped (no file of its paths changed) no file of the paths of the image changed since the last image
promotion app: blocked: <why> a check, the build or the promotion failed

The input of an image (#189)

desk builds one image for one component. The image of a component holds the files that its entry point imports, and no other file of dist. The image desk-server holds dist/server/main.js and the files that it reaches. Each runner image holds dist/server/runner-main.js and the files that it reaches. A server file that no runner imports changes the digest of desk-server only.

The image desk-server holds the runner too, for now. The Deployments desk-deployer-* start node dist/server/runner-main.js from the tag desk-server:main, and the chart that owns them moves them to a runner image in a later issue (#189). Until that chart lands, a runner file gives a new digest to desk-server and to the three runner images.

The layer of an image holds no version string and no build time. scripts/image-dist.mjs writes the files of one component into the layer. scripts/image-app.sh writes a minimal package.json ({"name":"desk","private":true,"type":"module"}) and the production node_modules, and it copies no other file of the repository. The version of package.json is therefore not an input of any image. A version bump, a doc change, a UI change and a skill change leave the digest of the four images of the application as they are.

The Dockerfile installs the tools before it copies the code: Node and the buck2 binary in deps and buck2, then git, buildctl and the Rust toolchain, then the dist of the component. A code change therefore reuses the tool layers. The stage runtime is the server and stays last, so a build with no target still gives the server; it copies the files of the runner as well, for the reason above.

images[].paths decides which landing builds which image. Give each image the paths that its build reads: the dist and src files of its component, the scripts of the build, and the raw files that scripts/embed.mjs reads for it (server/**, web/**, templates/**, packs/** for the server, packs/** for a runner). test/image-split.test.mjs checks that the list is current, and that a doc or UI change reaches no image of the application.

3. Two ways that a Deployment follows an image

The Deployment runs What rolls it What the repository needs
the tag :latest, imagePullPolicy: Always Keel polls the registry and rolls the Deployment no entry in deploys; the promotion of latest is enough
the digest <image>@sha256:… desk, through a deploys target an entry in deploys; one deployer pool of the lane deploy serves it

Keel is the shorter path, and it starts when the pod of Keel polls. The digest path gives a canary and a rollback, and it records which change runs in each Deployment. Use the digest path when a bad image costs more than the delay of a canary.

4. The config of a repository that deploys

"images": [{
  "name": "api",
  "dockerfile": "api/Dockerfile",
  "context": "api",
  "needs": ["api"],
  "paths": ["api/**"],
  "tags": ["latest", "main"],
  "changeArg": "APP_REVISION",
  "secrets": { "NPM_TOKEN": "npmtoken" },
  "timeoutMinutes": 30
}],
"deploys": [{
  "name": "api",
  "namespace": "api",
  "deployments": { "names": ["api"] },
  "containers": { "api": "api" },
  "timeoutMinutes": 15
}]
  • paths decides when the image builds. A landing that matches no path builds nothing.
  • tags holds the tags that a promotion sets. latest rolls a Deployment that follows the tag.
  • changeArg names the build variable that holds the landed change, for a binary or a page that stamps its version.
  • containers maps a container of the Deployment to a name of images. A deploy sets every container that deployments selects from that map.
  • deploys[].name is also the id of the deploy run, together with the digests. The same digests never deploy twice, and one deploy of a target waits or runs at a time.

A target waits while a build or a promotion of one of its images is queued, runs or waits for a check. So one landing rolls each Deployment one time.

5. A canary, before the patch

deploys[].canary starts one pod of the new image on the node of the running pod, with the volumes and the environment of the Deployment, and with command as its command. A pod that does not succeed in timeoutMinutes (default 5) changes no Deployment, and its last 40 log lines go to the run log. A migration check belongs here: munbot runs munbot db check-migrations on a copy of the tenant database before its image rolls.

A Deployment whose new pod is in CrashLoopBackOff fails at once. A Deployment that fails or does not end in its time limit gets its old images back in one patch. Read docs/runs.md for the four steps of src/server/deploy-run.ts.

A live check, after the deploy

deploys[].verify (command, timeoutMinutes from 1 to 60 with a default of 10, secrets) checks the running deployment (munhq/axon#601). When the deploy of a target passes, the server queues command as a run on the tree of the landing, with DESK_DEPLOYED_CHANGE set to the change that the deploy rolled out and with the secrets that secrets names. The run verify <target> is a step of the landing: the report of the landing lists it, and the deploy line of a loop is met only when that run passed. Use it for the checks that need the real services of the environment, such as a call through the API, a row that reaches the database, or the build commit of the pods.

An image that a cluster rolls out by its tag, with no deploy target, takes the same images[].verify: the server queues it when the promotion of the image passed. Its command waits for the rollout itself, for example until the service reports the new build commit.

6. Secrets

A check or an image of a landing gets the secrets that it names, only on main, after a landing. The runner writes the value into DESK_BUILD_SECRET_<ID> (a file with _FILE), and it replaces the value with [secret] in the run log. A secret that the runner does not hold ends the run with an error that names it. Add the secret to the runner before the first landing that reads it.

7. A store application is not a deploy

An Android application reaches a phone through Google Play, and a new bundle is the only path. There is no over-the-air update. The steps are:

  1. Raise the version code in the manifest of the app (expo.android.versionCode for Expo). A versionCode that Play holds already fails the publish at step 3.
  2. Land the change. The publish takes the head of main, not the work folder.
  3. Run desk publish <name> --track internal --wait. For production, use --track production --rollout 0.1.
  4. Read the log of the run. A passed upload ends with Play: the bundle <versionCode> is on the track <track>, completed (edit <id>). desk release list --limit 5 shows the release. desk runs view does not carry the versionCode of a publish.

docs/runs.md#publish-a-bundle-for-google-play-161 gives the entry, the seven steps of the runner, and the signer check.

8. What proves a deploy

desk runs landing <seq> --wait          # 0 only when every step passed
desk runs view <id> --json              # the result of one run
curl -s -o /dev/null -w "%{http_code}\n" https://app.example.com/api/health   # 200
kubectl -n api get deploy api -o jsonpath='{.spec.template.spec.containers[0].image}'
desk release list --limit 5             # the releases of store publishes

The last command shows the digest or the tag that the Deployment runs now. Compare it with the digest in the log of the image run.

9. Faults met, and their cause

Symptom Cause
the landing passed, and the pod runs the old image the Deployment follows a tag and Keel did not poll, or it follows a digest and the repository has no deploys target
a promotion waits for hours the root check has not passed, so the tag latest does not move (#145)
400 Invalid JSON payload received. Unexpected token. PK… on a Play upload the URL had no /upload prefix, so Google read the zip as JSON (#168)
the bytes are not the blob …: they have another id on an upload the part was named by a plain sha256 instead of the store blob id, objectId("blob", bytes) (#167)
a run ends with a missing secret named the runner does not hold DESK_BUILD_SECRET_<ID>; add it, then land again
a deploy fails and the old image returns the new pod did not become available in timeoutMinutes; read the last 40 log lines of the canary
the pods roll twice, 1 to 2 minutes apart, on the same digest Keel keeps keel.sh/update-time in the pod template, so the poll patches again after the promotion patched (munhq/k3s-gpu#311)
the landing ends in 90 s and the pods are ready in 4 minutes the image build is not on the path of desk, but it is the path of a deploys target; read the build log of the landing (docs/latency.md)