【发布时间】:2022-06-15 22:33:12
【问题描述】:
我正在使用 jsonnet 动态创建一个 gitlab-ci.yml 文件。 现在我想为我的客户目录中的每个目录创建一个作业。 为此,我想将所有目录名称读入一个数组。
我该怎么做?
伪代码如下所示:
customers:: [c for c in getAllDirectories("path/to/customers-directory")]
非常感谢!
【问题讨论】:
标签: jsonnet
我正在使用 jsonnet 动态创建一个 gitlab-ci.yml 文件。 现在我想为我的客户目录中的每个目录创建一个作业。 为此,我想将所有目录名称读入一个数组。
我该怎么做?
伪代码如下所示:
customers:: [c for c in getAllDirectories("path/to/customers-directory")]
非常感谢!
【问题讨论】:
标签: jsonnet
按照设计jsonnet 没有任何方法可以读取“环境数据”(广义地说:文件/目录/设备、环境变量等)。但是,它确实提供了通过 CLI 参数“注入”此数据的方法,请参阅 https://jsonnet.org/learning/tutorial.html#parameterize-entire-config。
在您的情况下,这可以通过以下示例代码来实现:
$ mkdir -p /tmp/customers.d/{foo,bar,baz}
$ ls -F /tmp/customers.d/
bar/ baz/ foo/
dirs.jsonnet)// Expect ext-var containing new-line separated list of dirs
local customers = std.split(std.extVar('customers'), '\n');
customers
$ jsonnet --ext-str customers="$(find /tmp/customers.d/ -mindepth 1 -maxdepth 1 -type d -printf '%f\n')" dirs.jsonnet
[
"bar",
"foo",
"baz"
]
【讨论】: