What it does
yq (the Go version by Mike Farah, v4) uses the same filter language as jq, but for YAML (and JSON, XML, TOML). It keeps comments and the order of keys, so it is safe for files in Git.
Typical use: set an image tag in a manifest in a pipeline, read a value from values.yaml, merge two YAML files, or turn YAML into JSON.
Note
there is also an older Python yq; the commands here are for the Go yq v4 (yq --version shows "mikefarah").
How to write it
yq [eval|eval-all] [OPTIONS] EXPRESSION [FILE ...]Options 16
Most used
Main options
Expressions
Nothing matches this filter.
Examples 12
$ run as a normal user# run as root (or with sudo)
1Read one value
yq '.image.tag' values.yaml v1.3.22Set the image tag in place (in a pipeline)
TAG=v1.4.0 yq -i '.image.tag = strenv(TAG)' charts/shop/values.yaml3Change the image of a Deployment
yq -i '(.spec.template.spec.containers[] | select(.name == "app")).image = "ghcr.io/acme/shop:1.4.0"' deploy.yaml4YAML to JSON and back
yq -o=json deploy.yaml > deploy.json yq -p=json -o=yaml deploy.json5Merge a base and an override file
yq ea '. as $item ireduce ({}; . * $item)' values.yaml values-prod.yaml > merged.yaml6Only the Services in a multi-document file
yq 'select(.kind == "Service")' all.yaml7Count the documents
yq ea '[.] | length' all.yaml8Remove a key
yq -i 'del(.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration")' svc.yaml9Add an item to a list
yq -i '.env += [{"name": "LOG_LEVEL", "value": "info"}]' container.yaml10Read the jobs of a GitHub Actions workflow
yq '.jobs | keys' .github/workflows/ci.yml11Split a big file into one file per resource
yq -s '.kind + "-" + .metadata.name' all.yaml12Check that a value exists (CI gate)
yq -e '.resources.limits.memory' values.yaml >/dev/null || echo "memory limit missing"
Nothing matches this filter.
Watch out
-ichanges the file without a backup; commit first or work on a copy.- Use
strenv(VAR)for versions like1.10—env(VAR)would turn them into the number 1.1. - Two different tools are called yq; check
yq --version.
Tips to remember
- Kustomize or Helm is better for big changes; yq is great for one value in CI.
Exit codes
0 | OK. |
1 | Error, or no match with -e. |