A Tangled spindle that runs CI on other providers, including ephemeral DigitalOcean droplets.
tack docs droplet.md
15 kB 328 lines
Markdown
at main

DigitalOcean droplets #

The droplet provider runs each Tangled workflow on a DigitalOcean droplet created for that one run and destroyed when it ends. There is no third-party CI behind it: tack creates the machine, the machine runs the steps, and tack stores the output and serves it to Tangled like any other provider's.

Each run goes like this:

  1. A workflow triggers. Tack records the run, publishes pending, mints a random token for the run, and creates a droplet whose cloud-init user data carries that token.
  2. The droplet boots and downloads tack-runner from tack using the token. It checks the binary against the SHA-256 in its user data, then starts it.
  3. tack-runner fetches its job, starts one container from the job image, clones the commit, and runs the workflow's steps in that container. Every line of output goes back to tack in numbered batches.
  4. The last event ends the run. Tack publishes the result, revokes the token and destroys the droplet.

Tack never connects to the droplet. Every exchange is a request the droplet makes, so the droplet runs no sshd and its firewall admits nothing inbound.

A separate DigitalOcean team #

Put these droplets in a DigitalOcean team of their own, apart from anything else you run. A team is an account boundary, with its own API tokens, VPCs, snapshots, firewalls and billing:

  • a job droplet has no private route to your other machines, because it is in none of their VPCs;
  • the token tack holds can see and delete only CI droplets, so a leak costs no more than that;
  • CI spending shows up on its own.

The cost of the boundary is that the droplet reaches tack over the public internet. The runner endpoints are on tack's public HTTP server beside the XRPC API, and the per-run token is their only protection. The security model below covers why that is enough.

Set up DigitalOcean #

  1. Create a team for CI, and set a spending alert on it.

  2. In that team, create an API token with these custom scopes:

    Scopes Used for
    droplet:create, droplet:read, droplet:delete Creating, reaping and destroying runners
    droplet:update, image:create, actions:read Snapshotting the builder and waiting for it
    snapshot:read, snapshot:delete, image:read, image:delete Choosing the newest snapshot and pruning old ones
    tag:create, tag:read Tagging droplets
    firewall:create, firewall:read Ensuring the runner firewall
    ssh_key:create, ssh_key:read Ensuring the unusable account key (see below)
  3. Set TACK_DIGITALOCEAN_TOKEN to it (in the NixOS module, through environmentFile) along with the settings below, and start tack.

On startup, tack creates the following in the team if they are missing:

  • the tack tag;
  • a cloud firewall named tack-runners on that tag. It allows no inbound traffic and all outbound traffic, and an existing firewall of that name is never changed;
  • an account SSH key named tack-no-login. Tack generates this key and discards its private half on the spot. It exists only because DigitalOcean emails the account a root password for every droplet created without a key, and a key nobody holds is the quiet way to avoid that.

Build the runner snapshot #

$ tack droplet-snapshot

This builds the image runner droplets boot. It starts from the base image, installs podman, pulls the job image, removes openssh-server, and powers off. Then it snapshots the droplet as tack-runner-<timestamp>, deletes the builder droplet, and prunes older snapshots.

It reads the same environment as tack, so run it wherever tack runs. The NixOS module defines it as a one-shot service: systemctl start tack-droplet-snapshot. Run it again whenever the job image changes. Tack uses the newest matching snapshot for each new run, so it needs no restart.

Without a snapshot, runs still work: the droplet boots the base image and installs podman itself, and the job image is pulled on first use. That adds minutes to every run.

Configure tack #

Env var Default Description
TACK_DIGITALOCEAN_TOKEN API token for the CI team. Enables the provider.
TACK_DIGITALOCEAN_DEFAULT_IMAGE Job container image, and the image the snapshot bakes in. Required.
TACK_DIGITALOCEAN_RUNNER_BINARY set by the Nix package Path of the tack-runner binary tack serves to droplets.
TACK_DIGITALOCEAN_REGION nyc1 Region droplets and snapshots live in.
TACK_DIGITALOCEAN_TIERS s=s-2vcpu-4gb:3,m=s-4vcpu-8gb:2,l=s-8vcpu-16gb:1 Tiers as name=size:max. max caps how many droplets of that tier run at once; further runs wait, showing pending.
TACK_DIGITALOCEAN_DEFAULT_TIER first tier Tier for workflows that name none.
TACK_DIGITALOCEAN_CALLBACK_URL https://$TACK_HOSTNAME Base URL droplets reach tack at. Must be reachable from the internet.
TACK_DIGITALOCEAN_BASE_IMAGE ubuntu-26-04-x64 Image the snapshot is built from, and the fallback when no snapshot exists.
TACK_DIGITALOCEAN_SNAPSHOT_PREFIX tack-runner- Name prefix of the runner snapshots.
TACK_DIGITALOCEAN_MAX_LIFETIME 2h Longest a droplet may exist. Past it, the run is marked timeout and the droplet destroyed.
TACK_DIGITALOCEAN_BOOT_TIMEOUT 15m How long a new droplet has to report in at all.
TACK_DIGITALOCEAN_HEARTBEAT_TIMEOUT 5m How long a runner may go silent. It sends a heartbeat every 30s, so this is only reached if the runner or its network dies.
TACK_DIGITALOCEAN_LOG_RETENTION 90d How long a finished run and its logs are kept, as a number of days (90d) or a Go duration. 0 keeps them forever.
TACK_DIGITALOCEAN_SNAPSHOT_SIZE s-2vcpu-4gb Size of the snapshot builder droplet.
TACK_DIGITALOCEAN_SNAPSHOT_KEEP 2 Snapshots to keep when pruning.
TACK_DIGITALOCEAN_SNAPSHOT_TIMEOUT 30m How long the builder may take to prepare. A builder that fails to prepare never powers off, so this is also how long a failed build takes to be reported.

tack-runner is a static x86-64 Linux binary, which is what DigitalOcean droplets run. The Nix package builds it on the host tack is built for, so deploy tack from an x86_64-linux build.

Workflow YAML #

The steps use the stock spindle's schema, so a workflow moves across by changing engine: and adding a tack: block:

when:
  - event: ["push", "pull_request"]
    branch: ["main"]

engine: tack

tack:
  droplet:
    tier: l          # optional; a tier the operator defined
    image: ...       # optional; defaults to TACK_DIGITALOCEAN_DEFAULT_IMAGE
    timeout: 90m     # optional; see below

environment:
  NIX_CONFIG: "experimental-features = nix-command flakes"

steps:
  - name: flake check
    command: nix flake check -L
  - name: build
    command: |
      nix build .#default
      ls -l result/
    environment:
      EXTRA: value

Each step runs with bash -eo pipefail -c, in /work/repo, inside one container that lasts the whole run. The Nix store a step fills is still there for the next step. The first step that fails ends the run.

The container gets /dev/kvm whenever the droplet has it, so NixOS VM tests run with hardware virtualization. Multi-machine suites want the largest tier.

timeout defaults to, and is capped at, TACK_DIGITALOCEAN_MAX_LIFETIME minus TACK_DIGITALOCEAN_BOOT_TIMEOUT. This leaves the droplet time to boot and to report a timeout before the lifetime cap destroys it.

A workflow that cannot run is reported as failed in Tangled, with the reason in its error, and no droplet is created. Causes include an unknown tier, no steps, a bad timeout, and a trigger with no commit.

Injected environment #

These are set for every step. The workflow's environment: and each step's environment: are layered on top in that order, and may override them.

Variable Value
CI true
TACK_KNOT Knot host the trigger came from
TACK_PIPELINE_RKEY Pipeline record rkey
TACK_WORKFLOW Workflow name
TACK_WORKFLOW_RAW Raw workflow YAML body
TACK_ACTOR Triggering DID
TACK_COMMIT Commit SHA
TACK_BRANCH Branch

Steps as Tangled shows them #

Step Kind What it is
0 system Start the job container (and pull the image, if the snapshot lacks it)
1 system Clone the repository at the commit, shallowly
2… user The workflow's steps, in order

Status mapping #

What happened Status
Run recorded, waiting for a slot or for the droplet to boot pending
The runner fetched its job running
Every step exited 0 success
A step exited non-zero failed, with its exit code
Cancelled from Tangled cancelled
The workflow's timeout passed timeout, exit code 124
The droplet reached TACK_DIGITALOCEAN_MAX_LIFETIME timeout
DigitalOcean has no droplets of the tier's size in the region stays pending and is retried after 2.5, 2.5, 5, 10, 20 and 20 minutes; failed if none of those works
The droplet could not be created, never reported in, went silent, powered off without reporting, or disappeared failed, with the reason
Tack restarted while the run waited for a slot failed

Capacity shortages #

DigitalOcean sometimes has no droplets of a size in a region, and answers a create with a 422 such as "Size is not available in this region." Shortages come and go, so tack treats that as temporary. The run stays pending, keeps its tier slot, and tack tries again after 2.5, 2.5, 5, 10, 20 and 20 minutes, about an hour in all. If every attempt fails, the run fails, naming the size, the region and how long it waited. Cancelling a waiting run works as usual. Any other error creating a droplet fails the run at once, since a bad token or image will not fix itself.

Log retention #

Tack is the only copy of a droplet run's logs, since the droplet is gone once the run ends. So tack keeps them itself, for TACK_DIGITALOCEAN_LOG_RETENTION after the run finishes: 90 days unless set otherwise. The reaper deletes older runs and their logs once an hour. It never deletes a run that is still going, however old.

A pruned run still shows its final status in Tangled, because that is kept with the pipeline's records rather than with the logs. Only its logs are gone.

Secrets #

The secrets section of a repository's settings in Tangled works once tack has an encryption key. Add one to tack's environment file:

$ echo "TACK_SECRETS_KEY=$(openssl rand -base64 32)" >> tack.env

Keep it safe: tack cannot read secrets stored under a different key, and a run that needs them fails saying so. Without the key, secrets are disabled, the settings page reports that, and runs get none.

The repository's owner and its collaborators may add, list and remove secrets. Each one becomes an environment variable in every step, above the workflow's environment: and beneath each step's own.

  • Stored encrypted. Values are encrypted with AES-256-GCM under TACK_SECRETS_KEY. The database and its backups never hold them in the clear, and a value copied to another repository's row does not decrypt.
  • Delivered late. A run's secrets reach the droplet only in the job it fetches over TLS, never in its user data, which DigitalOcean keeps and shows in its control panel. On the droplet they pass to the container through podman's environment, never its command line.
  • Masked. Tack replaces each value, and its base64 forms, with *** in everything the runner reports, before storing it. A value printed across more than 16 KiB of one unbroken line can straddle two chunks and escape the mask.
  • Never for a fork. A run building a fork's code, such as a pull request from one, gets no secrets, as with the stock spindle.

Cancelling #

The cancel button on a workflow in Tangled works. Anyone who may trigger a repository's pipelines may cancel them.

Cancelling marks the run cancelled, revokes its token and destroys its droplet; the runner is simply switched off. A run still waiting for a slot is cancelled before it gets one, so no droplet is ever created for it.

The runner protocol #

The runner protocol is defined in internal/runnerapi. Every request carries Authorization: Bearer <token>.

Request Purpose
GET /droplet/v1/agent The tack-runner binary.
GET /droplet/v1/job The job: image, clone URL, commit, steps, environment, deadline.
POST /droplet/v1/events A batch of numbered events. The answer is the highest sequence number stored.

Events are stored by (run, sequence number), so a batch resent after a lost acknowledgement changes nothing. An empty batch is a heartbeat.

A restart of tack does not lose a healthy run. The runner keeps retrying until tack answers again, and the run carries on where it was. The reaper starts again with tack and picks up anything that did go wrong meanwhile.

The reaper #

Every 30 seconds, and once at startup, tack lists the droplets carrying its own host tag (tack-host-<hostname>). It then:

  • destroys any droplet whose run has finished or is unknown to it;
  • adopts a droplet its run never recorded, which happens when tack stops between creating a droplet and saving its ID;
  • fails runs whose droplet powered off, disappeared, never reported in, went silent, or outlived the lifetime cap, and destroys the droplet.

It only ever sees droplets with its own host tag. The token can only see the CI team in any case, so the reaper cannot touch anything else.

Security model #

The token is the only credential in the protocol:

  • It is 32 random bytes, minted per run. It exists only in the droplet's user data and in tack's memory while the droplet is created.
  • Tack stores only its SHA-256.
  • It works for one run: the run is found from the token, never from the URL.
  • It stops working the moment the run finishes, however it finishes.

Requests without a valid token are refused with a 401 before their body is read. Accepted bodies are size-limited, and every event is checked against the protocol's limits.

A job step can read the token from the droplet's metadata service. With it, the step can forge events for its own run and nothing else, and it could already print whatever it likes to its own log. The token grants nothing beyond that.

The droplet itself:

  • runs no sshd: it is removed from the snapshot, and masked at boot when the base image is used;
  • carries only a key whose private half does not exist;
  • sits behind a firewall that admits nothing inbound;
  • runs one job and is then destroyed.

Job code can reach the internet but none of your other machines' private networks, because those are in a different team.

Limitations #

  • x86-64 only. tack-runner is built for x86-64 Linux, which is what DigitalOcean offers.