CheapConfigHacks

nicolaw 17th June 2019 at 9:59am
CodeSnippets
sed -i.bak -r "s/^#?\s*(ses_email_region\s*=\s*)\"?[^\"]+\"?/\1\"$JENKINS_PARAMETER_VARIABLE_HERE\"/" mytest.tfvars
jq --arg url "$MY_JENKINS_VARIABLE" -r '.url=$url' foo.json > foo-new.json
jq --arg deployment "$MY_JENKINS_VARIABLE" -r 'walk(if type == "string" then gsub("test-fullstack"; $deployment) else . end)' < foo.json

Walk solution with older versions of jq:

replace_in_json () {
  jq --arg from "$1" --arg to "$2" -f <(cat<<'EOF'
### walk() was added in jq 1.6rc1
def walk(f):
  . as $in
  | if type == "object" then
      reduce keys_unsorted[] as $key
        ( {}; . + { ($key):  ($in[$key] | walk(f)) } ) | f
  elif type == "array" then map( walk(f) ) | f
  else f
end;
###
walk(if type == "string" then gsub($from; $to) else . end)
EOF
)
}

replace_in_json "test-fullstack" "$MY_JENKINS_VARIABLE" < foo.json