How to remove an attribute in YAML file using yq?
Asked Answered
A

2

6

Because of a recent change in the ubuntu-latest image that introduced a buggy version of docker-compose, I had to lock down the version of docker-compose on my pipelines.

However, there was a task that I used to help clean up my deploy scripts namely DockerCompose@0. What I am trying to implement the equivalent of

- task: DockerCompose@0
  displayName: 'Remove build options'
    inputs:
      action: 'Combine configuration'
      removeBuildOptions: true

So basically I was thinking of using yq which will parse the YAML file and remove the build options which are not applicable on the stack deployment. However, I am not exactly sure how to do it. Since I need to remove it from every service that MAY include it.

So given the following input

services:
  def:
    build: ./def
    image: trajano/def
  ghi:
    image: trajano/ghi
version: '3.7'

I want to get

services:
  def:
    image: trajano/def
  ghi:
    image: trajano/ghi
version: '3.7'
Allieallied answered 11/9, 2020 at 22:8 Comment(0)
A
8

For newer yq versions (see Docs):

yq eval 'del(services.[].build)' foo.yml
Anglicanism answered 28/4, 2021 at 8:38 Comment(2)
yq eval 'del(.services.def.build)' foo.yml worked for meStenosis
Yeah, for me to it worked with the period before services as in above comment: yq eval 'del(.services.def.build)' foo.ymlEndblown
A
0
yq d foo.yml 'services.*.build'

To do this in Azure pipelines

steps:
- bash: |
    URL="https://github.com/docker/compose/releases/download/1.26.2/docker-compose-Linux-x86_64"
    sudo curl -sL $URL -o /usr/local/bin/docker-compose
    sudo snap install yq
  displayName: Install additional software
- bash: |
    docker-compose config | yq d - 'services.*.build' > $(Build.ArtifactStagingDirectory)/docker-compose.yml
  displayName: Clean up docker-compose.yml
Allieallied answered 11/9, 2020 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.