Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Plugins and Hooks

Status: ⚪ Planned — [hooks] parses and the kargo-plugin crate 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

PhaseHooks
Resolutionpre-resolve, post-resolve
Fetchingpre-fetch, post-fetch
Buildpre-build, post-build
Compilationpre-compile, post-compile
Testpre-test, post-test
Lint and formatpre-lint, post-lint, pre-fmt, post-fmt
Publishpre-publish, post-publish
Cleanpre-clean, post-clean
Per variantpre-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

FunctionPurpose
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 toUse
Run a check before buildsA hook
Add a team-specific commandA subcommand plugin
Generate sources from the manifestA script plugin
Transform compiler behaviorA Kotlin compiler plugin in [plugins]
Process annotationsKSP

Try the list top-down. A hook you can read in one line is worth more than a plugin you have to maintain.

Next