如何在GitLab ci中引用作业规则中的变量? [英] How reference variables in job rules in gitlab ci?

查看:31
本文介绍了如何在GitLab ci中引用作业规则中的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重用GitLab配置项作业规则中的变量

include:
  - template: "Workflows/Branch-Pipelines.gitlab-ci.yml"

.staging_variables:
  variables:
    CONFIG_NAME: "staging"

.staging_rules:
  rules:
    - if: $CI_COMMIT_BRANCH == $STAGING_BRANCH
      variables: !reference [.staging_variables, variables]

stages:
  - staging

staging:
  stage: staging
  rules:
    - !reference [.staging_rules, rules]
  script:
    - echo $CONFIG_NAME
  tags:
    - staging

但是,我看到此Syntax is incorrect皮棉错误:

jobs:staging:rules:rule:variables config should be a hash of key value pairs

我使用的是这里解释的示例:

https://docs.gitlab.com/ee/ci/yaml/yaml_optimization.html#reference-tags

请注意,我可以做到这一点,并且它是有效的:

include:
  - template: "Workflows/Branch-Pipelines.gitlab-ci.yml"

.staging_rules:
  rules:
    - if: $CI_COMMIT_BRANCH == $STAGING_BRANCH
      variables:
          CONFIG_NAME: "staging"

stages:
  - staging

staging:
  stage: staging
  rules:
    - !reference [.staging_rules, rules]
  script:
    - echo $CONFIG_NAME
  tags:
    - staging

推荐答案

目前无法将!reference-关键字用于!reference引用节和!reference

!reference documentation

您不能重复使用已包含!Reference标记的节。仅限 支持一级嵌套。

根据您的需要,您可以使用YAML-Anchors。(未测试)

include:
  - template: "Workflows/Branch-Pipelines.gitlab-ci.yml"

.staging_variables: &staging_variables
  variables:
    CONFIG_NAME: "staging"

.staging_rules: &staging_rules
  rules:
    - if: $CI_COMMIT_BRANCH == $STAGING_BRANCH
      variables: *staging_variables

stages:
  - staging

staging:
  stage: staging
  rules:
    - *staging_rules
  script:
    - echo $CONFIG_NAME
  tags:
    - staging

这篇关于如何在GitLab ci中引用作业规则中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆