如何合并YAML数组? [英] How to merge YAML arrays?

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

问题描述

我想在YAML中合并数组,并通过ruby加载它们-

I would like to merge arrays in YAML, and load them via ruby -

some_stuff: &some_stuff
 - a
 - b
 - c

combined_stuff:
  <<: *some_stuff
  - d
  - e
  - f

我想将组合数组设为[a,b,c,d,e,f]

我收到错误:解析块映射时未找到预期的密钥

I receive the error: did not find expected key while parsing a block mapping

如何在YAML中合并数组?

How do I merge arrays in YAML?

推荐答案

如果目标是运行一系列Shell命令,则可以实现以下目标:

If the aim is to run a sequence of shell commands, you may be able to achieve this as follows:

# note: no dash before commands
some_stuff: &some_stuff |-
    a
    b
    c

combined_stuff:
  - *some_stuff
  - d
  - e
  - f

这等效于:

some_stuff: "a\nb\nc"

combined_stuff:
  - "a\nb\nc"
  - d
  - e
  - f

我一直在gitlab-ci.yml上使用它(回答有关问题的@ rink.attendant.6评论).

I have been using this on my gitlab-ci.yml (to answer @rink.attendant.6 comment on the question).

我们用于支持requirements.txt从gitlab获得私有存储库的工作示例:

Working example that we use to support requirements.txt having private repos from gitlab:

.pip_git: &pip_git
- git config --global url."https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com".insteadOf "ssh://git@gitlab.com"
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts

test:
    image: python:3.7.3
    stage: test
    script:
        - *pip_git
        - pip install -q -r requirements_test.txt
        - python -m unittest discover tests

use the same `*pip_git` on e.g. build image...

其中requirements_test.txt包含例如

-e git+ssh://git@gitlab.com/example/example.git@v0.2.2#egg=example

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

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