【问题标题】:How to combine GitLab variable expansion with YAML anchors?如何将 GitLab 变量扩展与 YAML 锚点相结合?
【发布时间】:2019-08-24 09:31:41
【问题描述】:

我的 .gitlab-ci.yml 中有类似的东西:

.templates:
  - &deploy-master-common
    script:
      - ansible-playbook … --limit=${environment:name}.example.org
    environment:
      url: https://${environment:name}.example.org
…
deploy-master1:
  <<: *deploy-master-common
  environment:
    name: master1
  only:
    - master

不幸的是,在运行 deploy-master1 作业时,${environment:name} 被扩展为空字符串。 YAML/GitLab CI 不支持这种扩展吗?


我还不能确定这是 GitLab 还是 YAML 限制,但看起来 environment 哈希正在被替换而不是合并。&lt;&lt;: *deploy-master-common 移动到 @ 的底部987654325@ 我从 GitLab CI lint API 端点收到以下错误消息:

.gitlab-ci.yml is not valid. Errors:
[
  "jobs:deploy-master1:environment name can't be blank"
]

【问题讨论】:

  • 对于 YAML,标量 ansible-playbook … --limit=${environment:name}.example.org 没什么特别的,它不知道扩展它,这是由 gitlab 完成的。根据 YAML 规范,映射中合并键的位置无关紧要。所以这看起来像是 gitlab 未定义的行为(可能试图在加载 YAML 期间即时解决 ${} 扩展,他们应该在加载后进行后处理)

标签: gitlab yaml gitlab-ci


【解决方案1】:

我可以使用自定义变量来解决这个问题,因为模板没有指定任何变量:

.templates:
  - &deploy-master-common
    script:
      - ansible-playbook … --limit=${environment_name}.example.com
    environment:
      name: $environment_name
      url: https://${environment_name}.example.com
…
deploy-master1:
  <<: *deploy-master-common
  variables:
    environment_name: master1
  only:
    - master

【讨论】:

    猜你喜欢
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多