【问题标题】:How to remove an attribute in YAML file using yq?如何使用 yq 删除 YAML 文件中的属性?
【发布时间】:2020-12-30 10:33:23
【问题描述】:

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

但是,我曾经有一个任务可以帮助清理我的部署脚本,即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'

【问题讨论】:

    标签: docker-compose yaml azure-pipelines yq


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:
      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
      

      【讨论】:

        猜你喜欢
        • 2018-07-21
        • 1970-01-01
        • 2021-09-20
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 2021-09-09
        • 2017-04-18
        • 2022-01-25
        相关资源
        最近更新 更多