GitLab:如何在 yaml-checkers 存在的情况下使用 !reference 标签

问题描述 投票:0回答:3

我想在 GitLab 管道定义中使用

!reference
标签。

同时,我在

check-yaml
工具(
https://pre-commit.com/hooks.html
)中有像pre-commit这样的yaml-checkers。例如,此检查器使用
ruamel.yaml
python 包加载 yaml 文件。然后包会按照 “无法确定标签的构造函数”.

的方式抱怨

是否可以在我的管道中包含标签的定义以满足检查器?否则,这使得

!reference
标签无法在我的几个项目中使用。

gitlab gitlab-ci ruamel.yaml
3个回答
1
投票

!reference
是非标准扩展,因此您需要使用 --unsafe 选项

    -   id: check-yaml
        args: [--unsafe]

0
投票

!reference
是一个自定义的 YAML 标签,用于 GitLab(
!reference
标签
)和其他服务的配置文件中。其他自定义标签可能会发生相同的错误,例如 AWS !Ref
.
serverless.yml

我认为有3种可能的解决方案。

选项 1:如果您有其他 YAML 文件(例如

gitlab-ci.yml
),解决此问题的一种方法是从检查中排除非标准文件(例如
serverless.yml
)。

-   repo: https://github.com/pre-commit/pre-commit-hooks
    hooks:
    -   id: check-yaml
        exclude: serverless.yml

选项 2:或者如 anthony 所述,您只能运行不安全检查(但这将禁用对其他 YAML 文件的更高级检查):

-   repo: https://github.com/pre-commit/pre-commit-hooks
    hooks:
    -   id: check-yaml
        args: [--unsafe]

Option 3:如果你像我一样有其他 YAML 文件并且你想严格检查它们,最好的选择是从完整检查中排除

serverless.yml
并使用
--unsafe
选项检查该文件。

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    hooks:
    # basic checks for custom yaml syntax
    -   id: check-yaml
        name: Check YAML (unsafe)
        args: [--unsafe]
        files: serverless.yml
    # check other yaml files normally
    -   id: check-yaml
        exclude: serverless.yml

注意:我会在这里复制我自己找到解决方案之前得到的错误消息,因为我在检查AWS

serverless.yml
文件时找不到这个页面。

could not determine a constructor for the tag '!Ref'


0
投票

我无法确定

ruamel.yaml
在gitlab中是如何使用的,但是如果你能控制
ruamel.yaml.YAML()
实例是如何创建的,你应该确保它 使用默认的
typ
(通过不指定它),或等效的(`typ='rt')。 这将允许加载(和转储)任何有效的标签,而无需 需要指定构造函数。该标签可用于两个集合 类型(映射、序列)以及大多数标量。

import sys
import ruamel.yaml

yaml_str = """\
- !reference {a: 1, b: 2}
- !reference [1, 2, 3]
- !reference "abc"
- !reference 3.14
"""

yaml = ruamel.yaml.YAML(typ='rt')
data = yaml.load(yaml_str)
yaml.dump(data, sys.stdout)

给出:

- !reference {a: 1, b: 2}
- !reference [1, 2, 3]
- !reference "abc"
- !reference 3.14
© www.soinside.com 2019 - 2024. All rights reserved.