Plugins and Hooks
Status: ⚪ Planned —
[hooks]parses and thekargo-plugincrate is scaffolded, but no lifecycle phase dispatches hooks and no plugin mechanism is live. This page describes the intended design. See Implementation Status.
Kargo's manifest is declarative on purpose, which means it cannot express everything. Extensibility is the escape hatch — deliberately narrower than Gradle's "a build script is a program", and correspondingly easier to reason about.
Three tiers, in increasing power and decreasing isolation.
Hooks
Run a command at a lifecycle boundary:
[hooks]
pre-build = ["fmt --check", "lint"]
post-compile = ["./scripts/generate-openapi.sh"]
post-test = ["coverage-report"]
Entries are Kargo subcommands or external commands. They run in the project
directory with the full set of
KARGO_* environment variables
available, so a script knows the target, profile, variant, and output directory
without being told.
Hook points
| Phase | Hooks |
|---|---|
| Resolution | pre-resolve, post-resolve |
| Fetching | pre-fetch, post-fetch |
| Build | pre-build, post-build |
| Compilation | pre-compile, post-compile |
| Test | pre-test, post-test |
| Lint and format | pre-lint, post-lint, pre-fmt, post-fmt |
| Publish | pre-publish, post-publish |
| Clean | pre-clean, post-clean |
| Per variant | pre-variant-build, post-variant-build |
A non-zero exit from a pre-* hook stops the build. That is what makes
pre-build = ["fmt --check"] a gate rather than a suggestion.
pre-variant-build fires once per variant during --all-variants, which is where
per-variant codegen belongs.
Keep hooks boring
A hook that takes 400ms runs on every build, every save under kargo watch, and
every CI job. Hooks are for cheap, deterministic steps. Expensive codegen belongs
in an annotation processor whose output is
fingerprinted and cached — a hook is not.
Subcommand plugins
Any executable named kargo-<name> on your PATH becomes kargo <name>:
# ~/.local/bin/kargo-deploy
kargo deploy staging
This is Cargo's model, and it is the right default for team tooling: a plugin is an ordinary program in any language, with no API to learn and nothing to version against Kargo itself.
Plugins get project context from the environment and from
kargo metadata:
#!/usr/bin/env bash
set -euo pipefail
PROJECT=$(kargo metadata --format json)
NAME=$(jq -r .package.name <<< "$PROJECT")
echo "Deploying $NAME to ${1:?environment required}"
kargo plugin install my-plugin
kargo plugin list
kargo plugin remove my-plugin
Script plugins
For logic that needs to participate in the build rather than wrap it, embedded Rhai scripts:
[plugins.my-codegen]
script = "kargo-plugins/codegen.rhai"
hook = "pre-compile"
// kargo-plugins/codegen.rhai
let manifest = get_manifest();
let version = manifest.package.version;
emit_file(
"generated/Version.kt",
`package com.example
object Version {
const val NAME = "${version}"
}
`
);
log("info", `generated Version.kt for ${version}`);
Scripts are sandboxed: file writes are scoped to the project's generated-source directory, and there is no process spawning or network access. The trade is capability for the guarantee that a script cannot do something surprising to the machine running the build.
Script API
| Function | Purpose |
|---|---|
get_manifest() | The parsed manifest |
get_dependencies() | The resolved dependency graph |
get_targets() | Active targets |
emit_file(path, content) | Write a generated source file |
register_task(name, deps, handler) | Define a task |
register_command(name, desc, handler) | Add a subcommand |
register_hook(phase, handler) | Attach to a lifecycle phase |
log(level, message) | Structured logging into the build output |
Files written with emit_file are added to the compilation and fingerprinted, so
generated code participates in incremental builds properly.
WASM plugins
Status: ⚪ Design only.
A third tier for distributable plugins: compiled to WebAssembly, run in a wasmtime sandbox with explicit capabilities, installable from a registry. This is how a third-party plugin could be trusted enough to run in your build without auditing its source.
Which tier to use
| You want to | Use |
|---|---|
| Run a check before builds | A hook |
| Add a team-specific command | A subcommand plugin |
| Generate sources from the manifest | A script plugin |
| Transform compiler behavior | A Kotlin compiler plugin in [plugins] |
| Process annotations | KSP |
Try the list top-down. A hook you can read in one line is worth more than a plugin you have to maintain.
Next
- Manifest Format
- Environment Variables — what hooks receive
- Build Lifecycle — where hooks fire