如何将GitLab变量扩展与YAML锚点结合使用?

时间:2019-04-03 00:11:09

标签: gitlab yaml gitlab-ci

.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哈希将被替换而不是合并。<<: *deploy-master-common移动到在deploy-master1的底部,我从GitLab CI lint API端点收到以下错误消息:

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

1 个答案:

答案 0 :(得分:0)

我能够使用自定义变量解决此问题,因为模板未指定任何内容:

.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