csph apply, idempotently

Reading · 6 min · Module 8, lesson 3 of 648 min left in this module

Module 8 · Deploy as codeLesson 3 of 6

Goal: Apply a manifest more than once and read what each run created, updated or left unchanged.

Key idea

csph apply is idempotent: applying the same file twice gives the same result as applying it once. Nothing is created twice and nothing that already matches is touched, so you can run it on every change.

Check the file first

csph apply --file computesphere.yaml --dry-run
✓ manifest is valid — 2 resource(s): [web emails]

A dry run checks the file's shape (required fields, known types, references to real services) and creates nothing. It doesn't compare the file with what's running.

Apply it

csph apply --file computesphere.yaml

For the shop manifest from the last lesson, the first run prints:

Applied 4 resource(s) — status: success
  ✓ web (web-service) [staging] — created
  ✓ emails (background-worker) [staging] — created
  ✓ web (web-service) [prod] — created
  ✓ emails (background-worker) [prod] — created

Two services in two environments makes four results. A manifest without a project block needs a destination: add --project <project-id> --environment <environment-id>, or set defaults with csph projects use and csph environments use (lesson 5.1.1).

Apply it again

With the file unchanged, every line says unchanged and nothing redeploys. Change web to a new image tag and apply once more: web reports updated in both environments and rolls out, with the old version serving until the new one is healthy. emails stays unchanged.

ResultMeaning
createdIt didn't exist, so it was made and deployed
updatedThe file changed it, so it was redeployed
unchangedIt already matches. Nothing happened
failedThe line below it says why

Apply only adds

Deleting a service from the file doesn't delete it from ComputeSphere. When you retire a service, delete it on purpose in the console or with csph, and remove it from the file in the same change.

Check yourself

You remove the emails service from computesphere.yaml and apply. What happens to emails?

When something fails

The first line's status sums up the run. success exits 0. partial (some failed, the rest kept) and rolled_back (with --atomic, everything this run created was removed) exit 1, so a failed apply fails a CI job.

When a run creates or updates exactly one service, apply waits for it to reach Running and prints its URL. When several change, it returns once they're accepted; follow them with csph deployments list.

Every flag
FlagWhat it does
--file, -fThe manifest to apply. Required
--dry-runValidate only; create nothing
--atomicAll or nothing: if any resource fails, remove what this run created
--project, --environmentWhere to apply a manifest without its own project
--no-waitDon't wait for Running
--wait-timeoutHow long to wait. 5 minutes by default
--output jsonPrint the result as JSON

csph deploy uses the same engine: with no flags it applies the computesphere.yaml in the current directory.

In the docs