garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /.forgejo/workflows/deploy-sim.yml

raw

name: Deploy simulator

# Build the headless simulator and publish it to the host that runs it.
#
# **The fallback.** `midas deploy sim` is the usual path since 2026-10-16: it
# builds a landed, gate-verified commit on a workshop node and publishes it with the
# same checks (the `deploy.sim` recipe in the hub's /etc/midas/hub.conf), with
# no runner slot spent. This workflow is for when the workshop is down.
#
# **Manual, unlike the admin site.** The simulator is a live service with people
# connected to it, and publishing restarts it — every session drops and every
# client falls back to offline (`session::teardown` is written for exactly this,
# but a drop is still a drop). A README typo on `main` is not a reason for that.
# The web client's own deploy job is manual for a related reason and says so.
#
#   curl -X POST -H "Authorization: token $FORGEJO_TOKEN" \
#     -d '{"ref":"main"}' \
#     "$FORGEJO_API/repos/$FORGEJO_REPO/actions/workflows/deploy-sim.yml/dispatches"
on:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  CARGO_NET_RETRY: "3"
  CARGO_INCREMENTAL: "0"
  # Same reasoning as ci.yml: capacity 3 on an 8-core box shared with nominatim,
  # overpass and the agent containers. On the command line rather than as
  # `CARGO_BUILD_JOBS`, because `rust-cache` hashes every `CARGO*` variable into
  # its key and a job count changes no byte of any artifact — see ci.yml's env
  # block for the cold run that taught us.
  JOBS: "2"
  RUSTC_WRAPPER: sccache
  SCCACHE_DIR: /sccache
  SCCACHE_CACHE_SIZE: 20G

jobs:
  deploy-sim:
    name: build & publish the simulator
    runs-on: ubuntu-latest
    container:
      image: cartopolis-ci:latest
      # `/data/cartopolis/sim` is where the systemd unit reads the binary from,
      # and it has to be in the runner's `valid_volumes` or this mount is refused.
      #
      # Its own registry copy rather than the `test` job's — deploy-wasm.yml has
      # the long version. Short: a bind mount of `registry/` excludes cargo's
      # lock (`$CARGO_HOME/.package-cache`, one level above it), so two
      # concurrent containers corrupt one registry, and on 2026-08-21 the three
      # `deploy-*` lanes dispatched together did exactly that.
      options: >-
        --volume /data/cartopolis/sim:/publish
        --volume /data/ci-cache/sccache:/sccache
        --volume /data/ci-cache/cargo-registry-sim:/root/.cargo/registry
    steps:
      # No `lfs: true`. Unlike every other job in this directory, the simulator
      # embeds no assets at all — it holds no map data and never will — so the LFS
      # objects are ~200 MB of fetch for a binary that does not reference one.
      - uses: actions/checkout@v4

      - uses: https://github.com/Swatinem/rust-cache@v2
        with:
          key: simulator-sim-release

      # `sim-release` rather than dev: this is a server that runs for weeks, and
      # the profile inherits `release`'s fat LTO over one codegen unit. It fits
      # here where it does not for the client (`android-release` exists because
      # that link OOMs) — the simulator has no Bevy under it. What the profile
      # changes is `panic = "unwind"`: per-connection isolation depends on tokio
      # catching a panicking connection task at the task boundary, and the
      # workspace `release` profile aborts instead (see its note in Cargo.toml).
      - name: Build
        run: cargo build -j "$JOBS" -p cartopolis_simulator --bin simulator --profile sim-release

      - name: sccache stats
        if: always()
        # **A diagnostic may not fail a deploy.** On 2026-09-05 this exact
        # command ended a web publish: the wasm build had finished and trunk had
        # written the distribution, and `--show-stats` then answered "Failed to
        # read response header … Mismatch of client/server versions?" and exited
        # 2 — so the threading check, the pre-compression and the publish never
        # ran, and a good artifact was thrown away over the answer to "how did
        # the cache do?".
        #
        # Why the server stopped answering is **not established**. It is not a
        # stale server shared between lanes: each container has its own network
        # namespace, so each has its own. What is established is that every
        # compile had already succeeded, so whatever happened to the server
        # happened after the work. Print the stats, say so when they cannot be
        # printed, and let the checks that actually inspect the artifact decide.
        run: sccache --show-stats || echo "sccache could not report its stats — the build above is unaffected"

      # Prove the binary runs *before* it replaces the one currently serving
      # people. `--create-admin` against a throwaway database exercises the whole
      # startup path — config, SQLite open, every migration including the new
      # `admin` column — and exits. A binary that cannot migrate is the one
      # failure that would otherwise take the service down and keep it down,
      # because `Restart=always` would then restart it into the same error
      # forever.
      - name: Smoke-test the binary
        run: |
          CARTO_ADMIN_PASSWORD=smoke-test-not-a-real-password \
            ./target/sim-release/simulator \
              --database-path /tmp/smoke/world.db \
              --create-admin smoketest
          echo "startup + migrations ok"

      # The smoke test above migrates an *empty* database, which proves the
      # migrations run and says nothing about whether they agree with the live
      # one. They can disagree: this workflow deploys any ref, so the live schema
      # can hold a V<n> that this ref names differently (divergent) or does not
      # have at all (missing), and refinery refuses to start on either — after the
      # swap, so `Restart=always` crash-loops it. That took the public simulator
      # down for ten minutes on 2026-08-29
      # (docs/notes/migration-numbers-are-claimed-by-a-deploy.md). So migrate a
      # *copy* of the live database with the new binary first; failing here
      # leaves the old binary serving.
      #
      # A plain copy, because the image has no sqlite3 for `.backup`: the WAL is
      # copied beside the main file and replayed on open. A write landing
      # mid-copy can tear it, which shows up as "malformed" rather than as a
      # refinery error, so that one case is retried once on a fresh copy.
      - name: Migrate a copy of the live database
        run: |
          live=/publish/data/world.db
          if [ ! -f "$live" ]; then
            echo "no live database at $live — nothing to check against"
            exit 0
          fi
          attempt() {
            rm -rf /tmp/livecopy && mkdir -p /tmp/livecopy
            cp "$live" /tmp/livecopy/world.db
            [ -f "$live-wal" ] && cp "$live-wal" /tmp/livecopy/world.db-wal
            # Usage counts live beside it with their own migrations (crate::stats).
            stats=/publish/data/stats.db
            [ -f "$stats" ] && cp "$stats" /tmp/livecopy/stats.db
            [ -f "$stats-wal" ] && cp "$stats-wal" /tmp/livecopy/stats.db-wal
            CARTO_ADMIN_PASSWORD=smoke-test-not-a-real-password \
              ./target/sim-release/simulator \
                --database-path /tmp/livecopy/world.db \
                --create-admin smoketest-live-copy 2>&1 | tee /tmp/livecopy.log
            return "${PIPESTATUS[0]}"
          }
          if ! attempt; then
            if grep -qi "malformed" /tmp/livecopy.log; then
              echo "torn copy — retrying once"
              attempt || { rm -rf /tmp/livecopy; exit 1; }
            else
              rm -rf /tmp/livecopy
              echo "::error::this ref's migrations do not agree with the live database — not publishing. See docs/notes/migration-numbers-are-claimed-by-a-deploy.md"
              exit 1
            fi
          fi
          # It holds every account's row; do not leave it in the job container.
          rm -rf /tmp/livecopy
          echo "live schema + this ref's migrations ok"

      # Written to a temporary name and moved into place: a copy that is
      # interrupted half-way leaves a truncated file, and `Restart=always` would
      # then loop on it. `mv` within one filesystem is atomic.
      - name: Publish
        run: |
          install -m 0755 target/sim-release/simulator /publish/simulator.new
          mv /publish/simulator.new /publish/simulator
          # The signal that `cartopolis-sim-reload.path` is watching for. Last,
          # so the restart can never race the copy.
          date -u +%FT%TZ > /publish/restart.stamp
          echo "published $(du -h /publish/simulator | cut -f1) — restart requested"