YAML 合并级别 [英] YAML merge level

查看:30
本文介绍了YAML 合并级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个包含重复部分的 gitlab-ci yaml 文件.

We have a gitlab-ci yaml file with duplicate parts.

test:client:
  before_script:
    - node -v
    - yarn install
  cache:
    untracked: true
    key: client
    paths:
      - node_modules/
  script:
    - npm test

build:client:
  before_script:
    - node -v
    - yarn install
  cache:
    untracked: true
    key: client
    paths:
      - node_modules/
    policy: pull
  script:
    - npm build

我想知道,通过合并语法,我是否可以提取公共部分以在这两个部分的上下文中有效地重用它.

I would like to know, with the merge syntax, if I can extract the common part to reuse it efficiently in the context of these two parts.

.node_install_common: &node_install_common
  before_script:
    - node -v
    - yarn install
  cache:
    untracked: true
    key: client
    paths:
      - node_modules/

但真正的问题是:我必须在哪个缩进级别合并块以确保策略:pull 应用于缓存部分.我试图这样做:

But the real question is: at which indent level do I have to merge the block to ensure policy: pull is applied to the cache section. I tried to so that:

test:client:
  <<: *node_install_common
  script:
    - npm test

test:build:
  <<: *node_install_common
    policy: pull
  script:
    - npm build

但是我收到一个无效的 yaml 错误.如何缩进以获得正确的合并行为?

But I get an invalid yaml error. How to indent to get the correct merge behavior?

推荐答案

请注意,合并键不是 YAML 规范的一部分,因此不能保证有效.它们也是为过时的 YAML 1.1 版本指定的,并且尚未针对当前的 YAML 1.2 版本进行更新.我们打算在即将推出的 YAML 1.3 中明确删除合并键(并可能提供更好的替代方案).

Note that merge keys are not part of the YAML specification and therefore are not guaranteed to work. They are also specified for the obsolete YAML 1.1 version and have not been updated for the current YAML 1.2 version. We intend to explicitly remove merge keys in upcoming YAML 1.3 (and possibly provide a better alternative).

话虽如此:没有合并语法.合并键 << 必须像映射中的普通键一样放置.这意味着该键必须与其他键具有相同的缩进.所以这将是有效的:

That being said: There is no merge syntax. the merge key << must be placed like a normal key in a mapping. This means that the key must have the same indentation as other keys. So this would be valid:

test:client:
  <<: *node_install_common
  script:
    - npm test

虽然这不是:

test:build:
  <<: *node_install_common
    policy: pull
  script:
    - npm build

请注意,与您的代码相比,我在 test:clienttest:build 行中添加了 :.

Note that compared to your code, I added : to the test:client and test:build lines.

现在 merge 被指定为将引用映射的所有键值对放入当前映射中如果它们不存在于当前映射中.这意味着您不能如您所愿地替换子树中更深的值——merge 不支持部分替换子树.但是,您可以多次使用合并:

Now merge is specified to place all key-value pairs of the referenced mapping into the current mapping if they do not already exist in it. This means that you can not, as you want to, replace values deeper in the subtree – merge does not support partial replacement of subtrees. However, you can use merge multiple times:

.node_install_common: &node_install_common
  before_script:
    - node -v
    - yarn install
  cache: &cache_common
    untracked: true
    key: client
    paths:
      - node_modules/

test:client:
  <<: *node_install_common
  script:
    - npm test

test:build:
  <<: *node_install_common
  cache: # define an own cache mapping instead of letting merge place
         # its version here (which could not be modified)
    <<: *cache_common  # load the common cache content
    policy: pull       # ... and place your additional key-value pair
  script:
    - npm build

这篇关于YAML 合并级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆