提问人:Amin Ba 提问时间:12/16/2021 最后编辑:Amin Ba 更新时间:7/1/2023 访问量:5208
如何在 gitlab ci 中引用作业规则中的变量?
How reference variables in job rules in gitlab ci?
问:
我需要在 gitlab ci 作业规则中重用变量
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
但是,我看到这个 linting 错误: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
答:
-1赞
SPMSE
12/16/2021
#1
如上面的评论所述,您为 GitLab CI linting 工具生成了一个语法错误,该工具试图从引用部分解析变量数组。
更改您的配置以具有如下所示的标签:!reference
staging:
stage: staging
rules: !reference [.staging_rules, rules]
script:
- echo $CONFIG_NAME
tags:
- staging
请注意,此处已更改为- !reference
rules: !reference […]
这应该可以解决您的错误
评论
1赞
Amin Ba
12/16/2021
它不起作用,这与我引用的问题无关
4赞
Jakob Liskow
12/18/2021
#2
目前无法在引用部分中使用 -关键字。!reference
!reference
参考文档:
您不能重复使用已包含 !reference 标记的部分。只 支持一个嵌套级别。
根据您的需要,您可以使用 YAML 锚点。(未测试)
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 14.8 开始,可以使用多达 10 个级别的嵌套引用。
评论