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.yamlor.xeonr-ci.yaml. See What is available.
The preset SDK,
branchd.run/branchd/sdk/preset, is MIT licensed and published from the public repositorybranchd/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 intowith:, 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 addspreset.. An address is never another's prefix. - Default triggers, which a file's own
on:narrows. - Notes and warnings, which
branchd ci planshows.
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.Expandcalls the preset as the compiler does and returns its jobs;Refusalreturns the refusal of a configuration that must be refused;Describethe description.PushFacts,TagFactsandPullRequestFactsare the usual runs.gojobs := 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 runsbranchd ci plan --json --offlineon 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.RequireOKfails the test naming each refusal. Without the branchd CLI onPATHthe test is skipped; setPRESETTEST_REQUIRE_CLI=1in 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 .), asbranchd/presetsdoes.
The repository holds source only; a release run builds the module and publishes it as the tag's version.
- List the preset's directory in
.branchd/packages.yamlon the default branch (.for a repository that is one preset). Users writepreset: {uses: <group>/<repo>[/<path>]@v1}. - Protect the default branch and
v*tags: only the repository's own run of a protectedvX.Y.Ztag at its commit publishes. - Add a release pipeline on
v*tags withpermissions: {packages: write}whose job runsgo 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. - Tag
vX.Y.Z. Users on@v1get the newest publishedv1.x.ywhen 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. - 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
| Limit | Value |
|---|---|
| Module size | 8 MiB |
| Memory | 64 MiB of linear memory |
| Time | 5 seconds a call |
| Output | 1 MiB on stdout (64 KiB of stderr kept) |
| Files | 1,000 different files a call, 1 MiB each |
| Globs | 1,000 a call |
Request (its with: and the facts) | 4 MiB |
| A push | 8 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.