动态包含文件于GitLab-CI
Dynamically include files in GitLab-CI

原始链接: https://www.zufallsheld.de/2025/10/03/dynamic-gitlab-ci-includes/

最近在一个Gitlab-CI流水线中,我需要动态包含几个存储在yaml文件中的变量。需要包含的文件被定义为一个CICD变量,来自触发CI流水线的外部API调用。这些文件以版本号命名,例如.gitlab/vars/1.0.yml或.gitlab/vars/2.0.yml。 它们的内容如下: ```yaml variables: LONG_VERSION: "1.0.0" SHORT_VERSION: "1" more: variables ``` 这些文件使用`include`关键字包含在流水线中: ```yaml include: - local: .gitlab/vars/$VERSION.yml ``` 这样,我就可以通过API调用来触发流水线,从而定义要加载的变量: ```bash curl --request POST \ --form token=TOKEN \ --form ref=main \ --form "variables[VERSION]=1.0.0" \ "https://gitlab.example.com/api/v4/projects/123456/trigger/pipeline" ```

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 在 GitLab-CI 中动态包含文件 (zufallsheld.de) 11 分,来自 zufallsheld 2 天前 | 隐藏 | 过去 | 收藏 | 3 条评论 turtleyacht 2 天前 | 下一个 [–] Gitlab 文档:https://docs.gitlab.com/ci/yaml/includes/ xyzzy123 1 天前 | 上一个 [–] “你们的科学家太专注于他们是否能做到,而没有停下来思考他们是否应该。” zufallsheld 1 天前 | 父级 [–] 是的。在这个我需要这样做的一个项目中,我们考虑过完全移除流水线,并用定制的东西替换它。但一如既往:gitlab 运行正常,没有立即需要替换它的必要。 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文


Recently in a Gitlab-CI pipeline I needed to dynamically include several variables that lived inside a yaml-file. The file that needed to be included was defined as a CICD-variable coming from an external API-call that triggered the CI-pipeline.

The file were called after version-numbers, e.g. .gitlab/vars/1.0.yml or .gitlab/vars/2.0.yml. This was their content:

variables:
  LONG_VERSION: "1.0.0"
  SHORT_VERSION: "1"
  more: variables

These files were included in the pipeline using the include keyword:

include:
  - local: .gitlab/vars/$VERSION.yml

This way, I can define “externally” (via the API-call to trigger a pipeline), what variables are to be loaded:

curl --request POST \
    --form token=TOKEN \
    --form ref=main \
    --form "variables[VERSION]=1.0.0" \
    "https://gitlab.example.com/api/v4/projects/123456/trigger/pipeline"
联系我们 contact @ memedata.com