如何在 YAML 中创建包含冒号的配置文件?

How to create configuration files that contain colons in YAML?

根据 CI's lint,此 yml 无效:

pages:
  stage: deploy
  image: python:3.5
  script:
  - echo "foo: $VAR" > site.yml
  - cat ~/.python-gitlab.cfg

  artifacts:
    paths:
      - _build
  only:
    - master

错误:

jobs:pages:script config should be a string or an array of strings

如果我删除 echo 行中的冒号,它会起作用。

我想做的是即时创建一些配置文件,以符合现有工具,使用私有变量,例如 echo "url: $CI_PROJECT_URL" > site.yml 来生成

url: "https://gitlab.com/group/project"

但我无法执行此操作,因为 yaml 被认为无效,而且我找不到解决方法。或者我必须围绕我的工具编写代码来传递命令行参数而不是读取配置文件。不过,这个冒号的东西似乎是个错误。

如果你将整行用引号括起来,它应该可以工作:

- 'echo "foo: $VAR" > site.yml'

Gitlab 的 CI lint 将其标记为正确的语法。

有关详细信息,请参阅 here

如@Jawad 所述,整行单引号有效:

- 'echo "foo: $VAR" > site.yml'

但如果您的命令包含其他单引号,我发现使用 pipe 块缩放器样式最简单:|

- |
  echo "I want to echo the key"
  echo 'foo: $VAR' > site.yml

可以在此处找到有关块缩放器样式的更多文档:http://www.yaml.org/spec/1.2/spec.html#id2760844