如何使用 yq 删除 YAML 文件中的属性?

How to remove an attribute in YAML file using yq?

由于最近 ubuntu-latest image 的更改引入了 docker-compose 的错误版本,我不得不锁定 docker- 的版本在我的管道上作曲。

但是,我曾使用一项任务来帮助清理我的部署脚本,即 DockerCompose@0。我正在尝试实现相当于

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

所以基本上我在考虑使用 yq 来解析 YAML 文件并删除不适用于堆栈部署的构建选项。但是,我不确定该怎么做。因为我需要从可能包含它的每个服务中删除它。

所以给定以下输入

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

我想得到

services:
  def:
    image: trajano/def
  ghi:
    image: trajano/ghi
version: '3.7'
yq d foo.yml 'services.*.build'

在 Azure 管道中执行此操作

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

对于较新的 yq 版本 (see Docs):

yq eval 'del(services.[].build)' foo.yml