为许多存储库重用构建作业

Reuse build job for many repositories

假设我有一个可以在 Gitlab 中描述的标准化 Maven 构建 CI。

我想在 100 个存储库中重用这个 Maven 构建。

以尽可能少的代码重复实现这一目标的最佳方法是什么?

为了重用作业定义,GitLab CI 模板很好地满足了这个用例。通过使用 include: 关键字,可以通过多种方式在项目配置中 include 模板。它可能看起来像这样:

# your template file
# mytemplate.yml
maven-build:
  variables:
    MY_MAVEN_OPS: "" # variables may be useful for customizing behavior easily 
  stage: build
  script:
    - maven ... # you build this 

然后在您的 100 个其他项目中,您可能在 .gitlab-ci.yml 中有一些最小的东西来重用上面的配置,例如:

include:
  - project: jfabianmeier/templates
    file: mytemplate.yml
    ref: main # or a tag, git SHA, or whatever

您还可以将项目配置为使用 remote CI configuration,这样可以完全避免将配置提交给所有项目。此设置也可以使用 GitLab API 配置。这肯定是重复代码数量绝对最少的解决方案,尽管有些不灵活。