我想在 GitLab 管道定义中使用
!reference
标签。
同时,我在
check-yaml
工具(https://pre-commit.com/hooks.html)中有像
pre-commit
这样的yaml-checkers。例如,此检查器使用 ruamel.yaml
python 包加载 yaml 文件。然后包会按照 “无法确定标签的构造函数”. 的方式抱怨
是否可以在我的管道中包含标签的定义以满足检查器?否则,这使得
!reference
标签无法在我的几个项目中使用。
!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'
我无法确定
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