Skip to content
goonstationPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

goonstation-builder

Builds Goonstation game releases for goonhub. goonhub's Horizon GameBuild job keeps orchestrating builds (build records, secrets, logs/UI, CDN deploy, Overseer uploads, notifications) and sends the heavy work here:

  • the per-server git checkout, GitHub mirrors and test merges
  • BYOND, rust-g and byond-tracy-writer downloads
  • the DreamMaker compile
  • the tgui and browserassets build
  • packaging of the game archive, rsc.zip and CDN assets

The service is a single Go binary (standard library only) packaged with the build toolchain in a Docker image.

Running

With Docker Compose (compose.yaml):

cp .env.example .env   # set BUILDER_API_TOKEN, BUILDER_GITHUB_TOKEN and BUILDER_BIND
docker compose up -d
  • BUILDER_BIND is the address the API is published on: the builder's private (WireGuard/Tailscale) address that goonhub connects to. It defaults to 127.0.0.1. The API has no TLS of its own, so never publish it on a public address.
  • Without BUILDER_IMAGE, compose builds goonstation-builder:local from the checkout; rebuild after updating with docker compose up -d --build. Set BUILDER_IMAGE to a published, pinned tag (ghcr.io/<owner>/goonstation-builder:<version>) to run a release instead, and update with docker compose pull && docker compose up -d.
  • Data lives in the builder-data volume. Keep it across updates; removing it means re-cloning everything on the next build.
  • The service restarts automatically, keeps its logs to 100 MB, and gets 40 seconds to stop so running builds can be cancelled and recorded.

Without compose:

docker run -d --name goonstation-builder --restart unless-stopped \
  -p 10.0.0.2:8080:8080 \
  -v goonstation-builder-data:/data \
  --env-file .env \
  ghcr.io/<owner>/goonstation-builder:<version>

Don't run the container with --security-opt no-new-privileges or with Docker's default capabilities dropped: the builder needs its file capabilities to run build commands as a separate user (see Security), and refuses to start without them.

Configuration

Variable Default Purpose
BUILDER_API_TOKEN required Bearer token goonhub uses. At least 32 characters.
BUILDER_GITHUB_TOKEN empty Read-only token for the game repo and private submodules (+secret), and for PR details.
BUILDER_LISTEN_ADDR :8080 HTTP listen address.
BUILDER_DATA_DIR /data Persistent workspace.
BUILDER_MAX_CONCURRENT 2 Builds that run at once; extra builds wait for a slot.
BUILDER_KEEP_BUILDS 3 Finished builds (with artifacts) kept per server.
BUILDER_COMPILE_TIMEOUT 300s DreamMaker timeout.
BUILDER_NODE_INSTALL_TIMEOUT 300s npm install timeout.
BUILDER_STEP_TIMEOUT 300s Timeout for every other command.
BUILDER_BYOND_BASE_URL https://byond-builds.dm-lang.org BYOND zip mirror.
BUILDER_RUSTG_BASE_URL https://rustg.goonhub.com rust-g zips ({version}.zip).
BUILDER_TRACY_WRITER_URL https://byond-tracy-writer.goonhub.com/latest.zip Profiler zip.
BUILDER_GIT_USER_NAME / BUILDER_GIT_USER_EMAIL robuddybot / robuddybot@goonhub.com Author of test merge commits.
BUILDER_ISOLATE true Run build commands as a separate user. Set false only for local development.
BUILDER_BUILD_UID / BUILDER_BUILD_GID 1000 / 1000 The build user (node in the image). Must differ from the builder's user and group.

Data layout

/data                                     owner
├── shared-git/<remote>/                  builder (readable)  bare mirrors of GitHub remotes
├── runtimes/{byond,rustg,byond-tracy-writer}/  builder (readable)
├── bin/git-askpass                       builder (private)
├── etc/build-gitconfig                   builder (readable)  global git config for the build user
├── builder-home/  builder-tmp/           builder (private)
├── build-home/    build-tmp/             build user
└── servers/<serverId>/                   builder
    ├── repo/                             build user  checkout, origin = mirror
    ├── build/                            build user  scratch copy that gets compiled
    ├── buildcdn/                         build user  tgui/browserassets workspace (keeps node_modules)
    ├── bootstrap-cache/python-X.Y/       build user  tools/bootstrap/python venv (TG_BOOTSTRAP_CACHE)
    └── builds/<buildId>/                 builder (private)  events.ndjson + artifacts

On start, the builder repairs ownership of builder-owned trees, so a data volume from an older, single-user version is migrated automatically.

The bootstrap venv is keyed on the system Python version because goonstation's bootstrap script only refreshes it when requirements.txt changes; a Python upgrade in the image would otherwise leave a broken venv.

A full build compiles the game and builds the CDN assets at the same time; the CDN build works from the checkout and its log is shown after the compile's, so the event stream reads in the same order as before.

Measured on a local test (December 2026, goonstation master): the shared mirrors are about 2.2 GB, and each server uses about 2.4 GB (checkout 0.46 GB, build copy 0.65 GB, buildcdn 0.67 GB, about 0.6 GB per retained full build).

Security

Test merges put unreviewed code into builds, and it runs during the build (git hooks and merge drivers, npm install scripts, the tgui build). The builder is designed so that code can't get at the GitHub token, the API token, or the runtime secrets in the build spec.

Two users. The service runs as builder, which holds the tokens and secrets. Every command that touches repo-controlled code runs as a separate build user (node, uid 1000): git on the checkouts, hooks.install, DreamMaker, npm, yarn and rsync. A process can't read another user's /proc/<pid>/environ. The builder needs CAP_SETUID/CAP_SETGID (start commands as the build user), CAP_KILL (stop them), and CAP_CHOWN/CAP_FOWNER/CAP_DAC_OVERRIDE (manage the build user's directories). The image grants these as file capabilities on the binary, so the service is not root and the processes it starts get none of them. tini runs as PID 1 and reaps orphaned processes.

The token never reaches the build user.

  • The builder maintains bare mirrors of the game repo and its submodules, and is the only process that talks to GitHub, using the token through GIT_ASKPASS.
  • Checkouts use their mirror as origin, so the build user's git fetches locally and needs no token. PR heads are fetched into the mirror first.
  • Submodules can be pinned to commits that are on no branch (for example a +secret commit only reachable from a pull request). Before each checkout, the builder reads the pinned commits from its mirror and, if a submodule mirror lacks one, fetches that exact commit from GitHub into refs/builder/pinned/<sha>. Checkouts borrow the mirror's objects, so they see it without fetching anything themselves.
  • The builder never runs git inside a checkout. Test-merged code can rewrite the checkout's .git/config (hooks, core.fsmonitor, credential helpers), but nothing that runs there has the token.
  • Only submodules declared in the upstream branch's .gitmodules (read from the builder's mirror, not the checkout) are mirrored, and only from https://github.com, so test-merged code can't choose what the token fetches or send it to another host. A mirror is only reused for the remote it was cloned from.

The builder never follows the build user's links. The build and CDN workspaces belong to the build user, and the builder can override file permissions, so every file it reads or writes there goes through an os.Root confined to the workspace, and files are replaced rather than written through. A symlink or hard link planted by repo code can't redirect the builder to its own tokens or files elsewhere in the container.

Secrets and artifacts are private to the builder. config.txt (with config.secrets appended) and .env.build are replaced by builder-owned 0600 files only while the game archive is packaged, after all repo-controlled code has run, and are deleted afterwards. The builder writes the archives itself, into builds/, which the build user can't enter. Secrets stay inside game.tar.gz until that build is pruned.

Leftover processes are stopped. Every command runs in its own process group, killed when it finishes, is cancelled or times out. Code that escapes its group (setsid, daemons) is killed before and after each build, whenever no other build is running.

Limits.

  • All servers share one build user, so code from one server's test merge could tamper with another server's in-progress build if two builds run at once (BUILDER_MAX_CONCURRENT=1 avoids this). It still can't read tokens, secrets or finished artifacts.
  • The repo and test-merged code can read everything a build compiles, including +secret, as before.
  • browserassets/node_modules is kept in buildcdn between builds so dependencies aren't reinstalled every time. Test-merged code could tamper with it, affecting later builds of the same server until it is deleted.
  • Container-level isolation (network, other containers) is the deployment's job.

These protections were checked in the image with a script that, running as the build user during whole builds, scanned every process environment, the build directory and the artifacts. It found no token, secret or artifact it could read.

API (v1)

Every /v1 endpoint requires Authorization: Bearer <BUILDER_API_TOKEN>. GET /livez is unauthenticated and only returns ok, for container health checks.

The API is versioned: GET /v1/health reports apiVersion, and breaking changes ship under a new prefix (/v2) alongside the old one.

Method & path Response
GET /v1/health 200 {status, version, apiVersion, running: [build]}
POST /v1/builds 202 {id} with Location. 409 if the server is already building or the id exists, 422 for an invalid spec, 503 while shutting down.
GET /v1/builds/{id} 200 {id, serverId, running, startedAt, result?}
GET /v1/builds/{id}/events?after=<seq> NDJSON stream (see below).
DELETE /v1/builds/{id} 202 if cancelled, 409 if not running, 404 if unknown.
GET /v1/builds/{id}/artifacts/{name} The file (Range supported). 409 unless the build succeeded.
GET /v1/runtimes/byond/{major}.{minor}.tar.gz Cached BYOND install archive.
GET /v1/runtimes/rustg/{version}.zip Cached rust-g zip.
GET /v1/runtimes/byond-tracy-writer/latest.zip Cached profiler zip.

Errors are JSON: {"error": "..."}.

Build spec

{
  "buildId": 1234,
  "serverId": "main1",
  "mapSwitch": false,
  "repo": "goonstation/goonstation",
  "branch": "master",
  "byond": { "major": 516, "minor": 1667 },
  "rustgVersion": "3.4.0",
  "mapId": "COGMAP2",
  "rpMode": false,
  "cdn": {
    "version": 1,
    "baseUrl": "https://cdn-main1.goonhub.com",
    "preloadRscUrl": "http://cdn-main1.goonhub.com/rsc.zip"
  },
  "timezone": "America/Toronto",
  "testMerges": [
    { "prId": 123, "commit": "0123456789abcdef0123456789abcdef01234567" },
    { "prId": 456 }
  ],
  "config": { "secrets": "KEY value\nOTHER_KEY value" },
  "buildEnv": { "DISCORD_BOT_URL": "...", "DISCORD_BOT_CRASH_KEY": "..." }
}
  • buildId is goonhub's GameBuild id. It is also the build id in URLs and the BUILDSTAMP.
  • mapId is required when mapSwitch is true. Map switches skip fetching and the CDN build, like the PHP builder.
  • timezone (IANA name, default UTC) controls the BUILD_TIME_* defines.
  • testMerges[].commit pins a PR. Without it the PR head is used and reported back in a testmerge event, so goonhub can pin it.
  • config.secrets is appended to config/config.txt. buildEnv adds lines to .env.build; BUILDSTAMP, BYOND_*_VERSION and RUSTG_VERSION are reserved.
  • Unknown fields are rejected.

Events

GET /v1/builds/{id}/events replays every event with seq > after and then follows the build live. The stream ends after the result event. To resume after a dropped connection, reconnect with after set to the last seq received.

Each line is one JSON object:

type Fields Meaning
log seq, ts, stream ("out"/"err"), group?, line A log line. group marks the start of a UI log group (Repo, Test Merges, Byond, Build Preparation, Compilation, Rust-G, BYOND Tracy Writer, CDN, Game Artifact).
commit seq, ts, commit The commit being built (before test merges).
testmerge seq, ts, prId, commit, merged, conflicts? A test merge was processed. commit is the PR commit used; conflicts lists conflicting files, one per line.
heartbeat ts Sent every ~2s while idle; has no seq.
result seq, ts, status, error?, branchCommit?, author?, message?, artifacts? Final event. status is succeeded, failed or cancelled. error is the failing command's stderr (or stdout), as the PHP builder reported it. branchCommit/author/message describe the branch head, for notifications.

artifacts lists {name, size, sha256} for game.tar.gz, rsc.zip, and cdn.tar.gz (full builds with CDN output only).

Artifacts

Name Contents goonhub use
game.tar.gz The deployed game (same include list as the PHP builder) Saved as artifacts/game-<buildId>.tar.gz, uploaded to Overseer
rsc.zip goonstation.rsc Moved to the CDN root
cdn.tar.gz Contents of browserassets/build Extracted into the CDN root with --ignore-existing semantics

Behaviour changes from the PHP builder

  • Map switch builds include the cdn-manifest.json from the last full build for that server. The PHP builder shipped map switch builds without one.
  • PRELOAD_RSC_URL and the CDN base URL come from the spec instead of being hard-coded.
  • Author names in VCS_AUTHOR/ORIGIN_AUTHOR are escaped for DM strings.
  • Secrets are appended to config.txt on a new line, even if the file lacks a trailing newline.
  • PR fetches force-update pr-<id>, so force-pushed PRs no longer fail to fetch.

Development

go vet ./...
go test -race ./...
BUILDER_API_TOKEN=$(openssl rand -hex 32) BUILDER_DATA_DIR=./data BUILDER_ISOLATE=false go run ./cmd/goonstation-builder

Outside the image the builder can't switch users, so local runs need BUILDER_ISOLATE=false, which gives build commands the builder's own user (and access to its tokens). A real build needs the toolchain from the Dockerfile (git, rsync, Python 3.12+, Node and the i386 libraries for DreamMaker), so the image is the easiest way to run one locally:

docker build -t goonstation-builder .

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages