如何合并 YAML 数组? [英] How to merge YAML arrays?
问题描述
我想在 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
b
c"
combined_stuff:
- "a
b
c"
- 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屋!