Passing a json-string command arg in a YAML file

2/6/2018

I'm trying to run a command which changes kubernetes cronjob image value as described here. But instead of typing this command to cmd i'm trying to run it via Container Registry's build trigger.

I tried ' and \ as escape chars.

  steps:
     - name: 'gcr.io/cloud-builders/kubectl'
      args: [
      "patch",
       "cronjob",
       "cron-history-orders",
       "--type=json",
      "-p=[{'"op'":'"replace'",'"path'":'"/spec/jobTemplate/spec/template/spec/containers/0/image'",'"value'":'"python:3.5'"}]"
       ]
      env:  ['CLOUDSDK_COMPUTE_ZONE=europe-west3-c','CLOUDSDK_CONTAINER_CLUSTER=xxx-prod']

or when I try

   "-p='[{\"op\":\"replace\", \"path\": \"/spec/jobTemplate/spec/template/spec/containers/0/image\", \"value\":\"python:3.5\"}]'"

or

  "-p='[{'op':'replace','path':'/spec/jobTemplate/spec/template/spec/containers/0/image','value':'python:3.5'}]'"

I get error loading template: json: cannot unmarshal object into Go value of type []json.RawMessage

-- Tamer Aktaş
build-triggers
google-container-registry
google-kubernetes-engine
json
yaml

1 Answer

2/7/2018

As you no doubt realize, the challenge here is that you are dealing with layers of escaping -- your yaml contains json that will be passed on a command line.

I was able to pass this command through with this yaml:

steps:
- name: 'gcr.io/cloud-builders/kubectl'
  args: [ "patch", "cronjob", "cron-history-orders", "--type=json", "-p='[{\"op\":\"replace\", \"path\": \"/spec/jobTemplate/spec/template/spec/containers/0/image\", \"value\":\"python:3.5\"}]'" ]
  env:  ['CLOUDSDK_COMPUTE_ZONE=europe-west3-c','CLOUDSDK_CONTAINER_CLUSTER=xxx-prod']
-- David Bendory
Source: StackOverflow