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.
Not yet available (preset SDK): the preset SDK,
git.prod.wtf/xeonr/forge/ci/sdk/preset, is in branchd's private compiler module, so writing a preset needs access to it. Using a published preset is available (Presets).
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.
Publishing it
- Build the module for
wasip1, from the preset's Go main package:GOOS=wasip1 GOARCH=wasm go build -trimpath -ldflags='-s -w' -o preset.wasm . - Commit
preset.wasmat<path>/preset.wasmin the publishing repository (service/preset.wasminxeonr/forge/presets), so the commit a tag names holds the module. - List
<path>in.branchd/packages.yamlon the default branch. - Tag
vX.Y.Z. Users writepreset: {uses: <group>/<repo>/<path>@v1}and get the newestv1.x.ywhen their next run is compiled. 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 or organisation. 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 xeonr/forge/presets/service@v1: …). The repository's other files
compile as usual.