如何将 GitLab 变量扩展与 YAML 锚点相结合



我的.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"
]

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

.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

最新更新