How do you update a Kubernetes manifest's image with `yq` using a shell variable?
IntermediateConceptfind, xargs, jq and yq
Answer
export IMAGE="ghcr.io/shack/api:1.4.2"
yq -i '(.spec.template.spec.containers[] | select(.name == "api")).image = strenv(IMAGE)' deploy.yaml
# multi-document file: only Deployments
yq -i '(select(.kind == "Deployment") | .spec.replicas) = 3' all.yaml
# read a value
replicas=$(yq '.spec.replicas' deploy.yaml)
strenv(VAR) reads an exported environment variable as a string, avoiding quoting and injection issues. env(VAR) parses it as YAML (so "123" becomes a number).
More on find, xargs, jq and yq
What are the most useful `find` predicates for operations work?BeginnerWhat is the difference between `-exec cmd {} \;`, `-exec cmd {} +` and `-delete`?IntermediateHow does `find` evaluate `-o` (OR) and why do you need parentheses?AdvancedWhat does `xargs` do, and which flags matter?IntermediateWhy is `find | xargs rm` dangerous and what is the safe form?IntermediateWhat is `jq` and what are the filters you use every day?Beginner