Installing the builds¶
The builds run the manifests of a project's repository on each push, as builds.sr.ht does, and show the results on the project's Builds tab and on its commits. See Builds for how a project uses them.
They have two parts:
- the control plane, in the forge: Myxo services that keep the jobs, the runners and the secrets in the forge's database, and the build API, the runners' API and the pages, served by the web application under
/buildsand on each project; - runners, on other machines: each takes jobs from the control plane over HTTPS, runs them in fresh containers, and sends their logs back. A runner has no database and no credentials but its own token.
The control plane¶
Install the sourcelab-builds package with its forge extra, in the environment of the forge. It is a plugin of SourceLab: once installed, the forge loads it, and leaves it out if PLUGINS_DISABLED names builds.
Then place its services in the layout, /etc/sourcelab/layout.toml, in the web application's process, as the development layout, layouts/dev.toml, does:
[processes.web]
services = ["web", "builds", "builds_blobs", "builds_forge_accounts", "builds_forge"]
[services.builds]
database = "postgresql://..." # the forge's DB_URL
origin = "https://forge.example.org/builds"
job_page = "{origin}/jobs/{id}" # a job's JOB_URL: its build's page
secret_key = "${BUILDS_SECRET_KEY}" # see Secrets below
[services.builds_blobs]
directory = "/var/lib/sourcelab/builds" # the jobs' logs
[services.builds_forge_accounts]
database = "postgresql://..."
[services.builds_forge]
database = "postgresql://..."
buildskeeps the jobs and the runners. Itsjob_timeoutis how long a job may run, in seconds, 2700 (45 minutes) by default. Itsjob_pageis the URL that a job gets asJOB_URL: in the forge,{origin}/jobs/{id}, which leads to the page of the job's build.builds_blobskeeps the logs of the jobs, in its directory.builds_forge_accountslets the forge's users use the build API, with an API token that has thebuildsACL.builds_forgebuilds the pushes: it submits the manifests of each branch pushed, records them as a build of the project, and flags the commits as their jobs end. It also sends the mail of the manifests' email triggers, with the forge's mail settings (SMTP_SERVER,FROM_EMAIL...: see Configuration).
Without these services, the forge runs as before, and the projects have no Builds tab.
myxo migrate /etc/sourcelab/layout.toml creates their tables. The two ACLs that the builds need, builds and fetch, come from the forge's migrations (alembic upgrade head): a job of a private project clones it with a token of its own, which has the fetch ACL and is deleted when the job ends.
Runners¶
Register each runner in the forge's database. DATABASE_URL is the forge's database, and --token-from names the variable that holds the runner's token. Without it, the command prints a new one. Run again, it leaves an existing runner as it is, and fails if its token isn't the one given:
RUNNER_TOKEN=... DATABASE_URL=postgresql://... sourcelab-builds-admin runner create docker-1 --token-from RUNNER_TOKEN
On the runner's machine, install the sourcelab-builds package with its runner extra, Docker, and Sysbox, which runs each job's container in a user namespace, where its root is nobody on the machine. Use a machine that runs nothing else, on Ubuntu 24.04 with its latest kernel: Sysbox doesn't run on every distribution, and the machine's own AppArmor profiles apply to the programs of the jobs. The package's pyinfra deploy deploy/runner_host.py prepares such a machine from a stock Ubuntu 24.04: it upgrades it and boots the latest kernel, then installs Docker and Sysbox (uvx pyinfra HOST deploy/runner_host.py --ssh-user root). Build the images the jobs run in, from the recipes that the package holds. --docker-host builds them on another machine's daemon, such as ssh://user@host:
It builds eight images, from Alpine, Debian, Rocky Linux and Ubuntu: see Images for the names the manifests use. Run it again to update them, from the latest upstream images. The builds service accepts the same names by default; its images setting, a list separated by spaces, narrows them when the runners offer fewer.
The runner is the Myxo service builds_runner. The package's layouts/runner.toml takes its settings from the environment:
| Variable | Setting | Value |
|---|---|---|
BUILDER_CONTROL_URL |
control_url |
The forge's URL, followed by /builds |
BUILDER_RUNNER_TOKEN |
token |
The token registered above |
BUILDER_GUEST |
guest |
docker (local runs jobs on the runner's own machine, with no isolation: for development only) |
BUILDER_SLOTS |
slots |
How many jobs it runs at once, 2 by default |
BUILDER_WORK_DIR |
work_directory |
Where it keeps a job's files while it runs |
With docker, the settings docker_host (the local daemon by default), docker_runtime (sysbox by default, or privileged), cpus, memory and pids (each job's, 2, 3g and 4096 processes by default) can be added to the layout. Start the runner with myxo serve layout.toml runner, from a systemd unit for example.
Warning
Where Sysbox doesn't run, docker_runtime = "privileged" runs the jobs in privileged containers, which a job can escape to reach the runner's machine. Only run such runners for people you trust, on machines that hold nothing else.
Under Sysbox, a job can't run containers as an ordinary user with podman, and on Alpine edge, Docker inside a job fails: its runc 1.5 doesn't work in Sysbox's containers yet.
Secrets¶
A job may name SSH keys, its owner's secrets, which it gets in ~/.ssh/. The control plane keeps them encrypted with AES-256-GCM, under a key of the installation, and decrypts them only into the job that it hands to a runner.
Make the key once, 32 random bytes in base64:
Keep it like a password, apart from the database's backups: whoever has both reads the secrets. Give it to the builds service, as secret_key in the layout, from the environment as above. Without it, the forge runs, but the jobs that name a secret fail, saying the installation has no secret key.
To give a user a secret, set the same key in BUILDS_SECRET_KEY, and run:
BUILDS_SECRET_KEY=... sourcelab-admin build-secret add alice --name "deploy key" --ssh-key /path/to/id_ed25519
The command prints the secret's UUID, which the user's manifests name it by. With --uuid, the secret keeps a UUID you choose: to move a secret from builds.sr.ht, give it the UUID it had there, and the manifests that name it need no change. Running the command again with the same owner and UUID, and --yes, replaces the key; without --yes, it refuses. --database names the builds' database, if it isn't the forge's.
The file must hold the private key, without a passphrase, since nobody is there to type one. After the command, delete the file, or keep it where you keep your other keys.
A job only gets the secrets of its owner. If one is missing, or can't be decrypted with the installation's key, the job fails before any runner starts it, and its page says why.
Warning
Changing the installation's key makes every secret unreadable: the jobs that name them fail until each secret is added again under the new key.