Updating value in yaml file with YQ causes it do delete certain parts

154 views Asked by At

Currently I am trying to update a value in Github CI with YQ for my deployment on digital ocean app platform. I download the app spec via the CLI and want to update that spec with new values if the jobs are done.

APP SPEC LAYOUT:

alerts:
  - rule: DEPLOYMENT_FAILED
  - rule: DOMAIN_FAILED
domains:
  - domain: example.com
    type: PRIMARY
    zone: example.com
features:
  - buildpack-stack=ubuntu-22
ingress:
  rules:
    - component:
        name: example
      match:
        path:
          prefix: /
name: example
region: ams
services:
  - alerts:
      - operator: GREATER_THAN
        rule: MEM_UTILIZATION
        value: 90
        window: TEN_MINUTES
      - operator: GREATER_THAN
        rule: CPU_UTILIZATION
        value: 90
        window: TEN_MINUTES
    envs:
      - key: NITRO_PRESET
        scope: RUN_AND_BUILD_TIME
        value: node-cluster
      - key: KEY
        scope: RUN_AND_BUILD_TIME
        type: SECRET
        value: VALUE
      - key: URL
        scope: RUN_AND_BUILD_TIME
        type: SECRET
        value: URL
      - key: ID
        scope: RUN_AND_BUILD_TIME
        value: 29349034
      - key: NUXT_PUBLIC_VERSION
        scope: RUN_AND_BUILD_TIME
        value: v0.3.0 <-- I want to change this and keep the structure of the file.
    http_port: 3000
    image:
      deploy_on_push: {}
      registry_type: DOCR
      repository: example-website
      tag: v0.4.0
    instance_count: 1
    instance_size_slug: basic-xxs
    name: example-website

I want to update the value under services.envs.key (NUXT_PUBLIC_VERSION).value

Currently if i run this code:

yq e '.services[0].envs[] | select(.key == "NUXT_PUBLIC_VERSION").value = "v0.5.0"' current-app-spec.yaml -i

It will change the value but only paste back the part inside envs section.

Anyone that can guide me in the right direction?

1

There are 1 answers

0
Inian On BEST ANSWER

With mikefarah/yq, you can use the update select operator |= to update the result and get the whole YAML back, right from the root node.

yq '.services[0].envs[] |= select(.key == "NUXT_PUBLIC_VERSION").value = "v0.5.0"' current-app-spec.yaml

Recommend using the -i, --inplace flag, only after you dry-run the command without and confirm the YAML produced is as expected, so that any invalid writes do not occur.


Also the e flag no longer needs to be provided, as it has been made the default command since 4.18.1. See Notice for v4.x versions prior to 4.18.1