kubectl "error parsing STDIN: error converting YAML to JSON" in CI
You piped a manifest into kubectl apply -f - and the bytes on STDIN are not valid YAML. The conversion to JSON fails before anything reaches the API server - usually a broken heredoc, bad templating output, or interpolated values that corrupt the indentation.
What this error means
cat ... | kubectl apply -f - (or a heredoc) fails with error: error parsing STDIN: error converting YAML to JSON: yaml: line N: .... The same template applied from a file would show the same line - the problem is the generated YAML, not the pipe.
error: error parsing STDIN: error converting YAML to JSON: yaml: line 8:
could not find expected ':'Common causes
Broken heredoc or interpolation
An unquoted shell variable expands to a multi-line or special value that breaks indentation, or a heredoc terminator is mis-indented, producing invalid YAML on STDIN.
Templating emitted malformed YAML
envsubst/sed/Helm/kustomize output a value with a stray colon, tab, or unescaped character, so the piped document does not parse.
How to fix it
Inspect the generated YAML before piping it
Render to a file (or print it) so you can see line N exactly as kubectl receives it.
envsubst < template.yaml | tee /tmp/rendered.yaml | sed -n '5,11p' | cat -A
kubectl apply -f /tmp/rendered.yamlQuote interpolated values and fix the heredoc
- Quote shell-substituted values so colons, spaces, and newlines do not break YAML.
- Use a quoted heredoc (
<<'EOF') when you do not want shell expansion inside it. - Lint the rendered output (
yamllint) instead of piping unchecked.
How to prevent it
- Render templates to a file and lint before applying, rather than piping blind.
- Quote interpolated values in manifests and use quoted heredocs where appropriate.
- Validate with
kubectl apply --dry-run=client -f -in CI.