jsonnet: Evaluate Jsonnet to JSON in CI
jsonnet config.jsonnet evaluates a Jsonnet program and prints the resulting JSON, failing on any evaluation error.
Jsonnet is a config templating language that compiles to JSON. Evaluating it in CI both renders your config and catches errors before the JSON reaches a deploy step.
What it does
The jsonnet CLI evaluates a Jsonnet source file, resolving imports, functions, and variables, and emits JSON. External variables are supplied with --ext-str/--ext-code and top-level arguments with --tla-str/--tla-code. -m writes a JSON file per object in a multi-file output.
Common usage
# evaluate to JSON on stdout
jsonnet config.jsonnet
# pass external variables
jsonnet --ext-str env=prod --ext-code replicas=3 deploy.jsonnet
# top-level arguments
jsonnet --tla-str name=api service.jsonnet
# multi-file output (one JSON file per key)
jsonnet -m out/ manifests.jsonnetOptions
| Flag | What it does |
|---|---|
| -e, --exec | Treat the argument as inline Jsonnet |
| --ext-str <k=v> | External variable as a string |
| --ext-code <k=expr> | External variable as Jsonnet code |
| --tla-str <k=v> | Top-level argument as a string |
| -m, --multi <dir> | Write one file per output object |
| -J, --jpath <dir> | Add a library search path for imports |
| -o, --output-file <file> | Write JSON to a file |
In CI
Evaluate every .jsonnet as a build step: a syntax or logic error exits non-zero and fails the job before the rendered JSON is applied. Pipe the output into a schema validator (ajv-cli or check-jsonschema) to gate the generated config too.
Common errors in CI
RUNTIME ERROR: field does not exist: replicas means a referenced field is missing. STATIC ERROR: expected token ... got ... is a syntax error with a file:line:col marker. RUNTIME ERROR: couldn't open import "lib.libsonnet": no match locally or in the Jsonnet library paths means a missing import; add -J. RUNTIME ERROR: max stack frames exceeded signals infinite recursion.