【问题标题】:YAML merge levelYAML 合并级别
【发布时间】:2018-04-20 11:47:50
【问题描述】:

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

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

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

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

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

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

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

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

【问题讨论】:

    标签: yaml


    【解决方案1】:

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

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

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

    虽然不是这样:

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

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

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

    .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
    

    【讨论】:

    • 感谢您的帮助!我最终听从了您的建议,将 cache_common 中的值分开。
    猜你喜欢
    • 1970-01-01
    • 2018-08-10
    • 2020-02-16
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    相关资源
    最近更新 更多