branchd Docs
On this page

Writing a preset

What a preset program receives, what it returns, and the limits it runs under.

Available in repositories branchd reads pipeline files for. It is switching repositories on one at a time: until yours is, it keeps running its .forge-ci.yaml or .xeonr-ci.yaml. See What is available.

The preset SDK, branchd.run/branchd/sdk/preset, is MIT licensed and published from the public repository branchd/sdk.

Start from branchd/template-preset: a preset with its tests, a repository that uses it, golden files, and the CI that builds and publishes it.

A preset is a Go program, compiled to WebAssembly (wasip1), that branchd runs inside the compiler at every push. It is untrusted code, so it gets no network, no environment, no arguments, a fixed clock and a read-only view of the repository.

What it receives

  • Its configuration: the pipeline file's preset.with, decoded into the preset's own configuration type. An unknown key is refused, and the refusal lists the known ones. A refusal names a dotted path into with:, and the compiler shows it at that key's line.
  • The run's facts: the event, branch, tag, ref and commit, the changed paths, the run number, the repository's path and default branch, whether the ref is protected, and a pull request's number and target.
  • The repository's files at the commit, read-only: read a file, ask whether one exists, list files by glob.

What it returns

  • Jobs in the pipeline-file job form (reference), each at an address: the path of the configuration that produced it (apps.api.build). branchd adds preset.. An address is never another's prefix.
  • Default triggers, which a file's own on: narrows.
  • Notes and warnings, which branchd ci plan shows.

Return every job the configuration asks for and give each an if: for the events it runs on, rather than leaving it out: a need on a job that does not run then says why. Needs name the preset's own addresses, or the repository's own jobs the configuration names (by convention a needs: key on the entry).

Describe mode

The same program answers a description request with its name, summary, default triggers, the JSON Schema of its configuration, and its address patterns and outputs. The documentation and the compiler's messages come from it.

Testing it

The SDK's presettest package has two levels:

  • In process: presettest.Expand calls the preset as the compiler does and returns its jobs; Refusal returns the refusal of a configuration that must be refused; Describe the description. PushFacts, TagFacts and PullRequestFacts are the usual runs.

    go
    jobs := presettest.Expand(t, expand, presettest.Case{
    	Repo: "testdata/repos/app",
    	With: `{"binaries": ["./cmd/app"]}`,
    })
  • Through the compiler: presettest.Plan(t, presettest.PlanOptions{Repo: "testdata/repos/app", Presets: map[string]string{"acme/presets/go": "."}}) commits a real repository's files to a scratch repository and runs branchd ci plan --json --offline on it with your preset's source in place of the published module: the compiler branchd runs at a push, so the steps your jobs use are resolved and their inputs checked. RequireOK fails the test naming each refusal. Without the branchd CLI on PATH the test is skipped; set PRESETTEST_REQUIRE_CLI=1 in CI.

presettest.Golden(t, "testdata/golden/app.json", got) compares jobs or a plan with a file and rewrites it under UPDATE_GOLDEN=1.

Publishing it

Rolling out: a release run publishing the module (below) needs branchd's preset publishing, which is being deployed. Until it is live, a preset is published by committing the built module at <path>/preset.wasm (GOOS=wasip1 GOARCH=wasm go build -trimpath -ldflags='-s -w' -o preset.wasm .), as branchd/presets does.

The repository holds source only; a release run builds the module and publishes it as the tag's version.

  1. List the preset's directory in .branchd/packages.yaml on the default branch (. for a repository that is one preset). Users write preset: {uses: <group>/<repo>[/<path>]@v1}.
  2. Protect the default branch and v* tags: only the repository's own run of a protected vX.Y.Z tag at its commit publishes.
  3. Add a release pipeline on v* tags with permissions: {packages: write} whose job runs go run branchd.run/branchd/sdk/cmd/publish preset <path>. It builds the module (wasip1, -trimpath -buildvcs=false -ldflags='-s -w -buildid=', so the same source and Go give the same bytes), uploads it, and branchd checks the upload and records the version with the commit and the module's digest. A published version never changes, and every run records the digest it compiled with.
  4. Tag vX.Y.Z. Users on @v1 get the newest published v1.x.y when their next run is compiled; a tag with no published module is skipped. Keep each job's secrets, environments and runner labels stable within a major: a user's narrowing (preset.secrets, allow, runs-on) refuses a job past it.
  5. Set the visibility to who may use it: company, organisation or public, on the repository (branchd repo visibility) or on a group of presets (branchd group visibility). A private repository's preset is usable by that repository alone, and a repository cannot use its own preset (./<path> is refused for now).

Limits

LimitValue
Module size8 MiB
Memory64 MiB of linear memory
Time5 seconds a call
Output1 MiB on stdout (64 KiB of stderr kept)
Files1,000 different files a call, 1 MiB each
Globs1,000 a call
Request (its with: and the facts)4 MiB
A push8 presets, 15 seconds of module time in all

A preset that fails, passes a limit, or returns jobs that do not check fails the compile of that pipeline file only, with a message naming the preset (preset branchd/presets/service@v1: …). The repository's other files compile as usual.

Getting it listed

A preset is listed in Steps & presets when its directory has a preset.yaml and its release has published a version, for whoever can read your repository's code. preset.yaml, beside the preset's main.go, has three keys: name and description for the listing (describe mode's preset.Name and preset.Summary win when set), and main, the preset's Go main package (. for the directory itself). branchd/template-preset has one to copy. Without it the preset still runs for anyone who may use it, but is not listed. A version is listed as soon as its release publishes it. Its page shows what describe mode answers: the with: schema, the jobs from preset.Address, and the steps from preset.Steps. It also shows a README.md beside preset.yaml. branchd ci step lint in the preset's directory shows what the listing lacks.