Gitlab CI/CD 使用存储库

Gitlab CI/CD using repository

我在同一组中有几个 GitLab 项目。它们之间存在依赖关系。假设我有 3 个项目:

服务器和客户端项目都依赖于 api。

我正在尝试设置 CI/CD 以将图像部署到 GitLab 注册表。基于本教程 https://docs.gitlab.com/ee/user/packages/maven_repository/index.html#create-maven-packages-with-gitlab-cicd 我已成功将版本部署到此注册表。

问题是,如果我在api和服务器上做同样的事情,之后将不会下载依赖的api包,因为${env.CI_PROJECT_ID}会生成不同的id。

什么是最优雅的解决方案,而不是在 pom.xml 中添加多个具有硬编码项目 ID 的服务器。

另一个问题是我如何才能从本地环境使用这个存储库。

谢谢大家的帮助。

对此有几种不同的可能解决方案。

  1. 将所有工件发布到一个项目。

    您可以在组中选择一个项目(或创建一个新项目)并将所有工件发布到该注册表中。如果您不想对项目 ID 进行硬编码,您可以为其定义一个 CI 变量并使用它代替 ${env.CI_PROJECT_ID}.

  2. 将工件发布到多个项目和多个注册表,并在 pom.xml[=19= 中添加相应的 <repository> 条目]

    (我想你想避免这种情况,但我想我还是会列出这种可能性)

  3. 定义自定义 settings.xml 而不是将存储库添加到您的 pom.xml

    您可以将它添加到您的存储库中并与

    一起使用
    mvn clean install -s ./settings.xml
    

带有两个自定义 CI 变量的示例:

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <profiles>
        <profile>
          <repositories>
            <repository>
              <id>registry1</id>
              <url>https://gitlab.com/api/v4/projects/${env.REGISTRY1}/packages/maven</url>
            </repository>
            <repository>
              <id>registry2</id>
              <url>https://gitlab.com/api/v4/projects/${env.REGISTRY2}/packages/maven</url>
            </repository>
          </repositories>
          <id>glregistry</id>
        </profile>
      </profiles>
      <activeProfiles>
        <activeProfile>glregistry</activeProfile>
      </activeProfiles>
</settings>

Another question is how can I then use this repository from local environment.

为此,我建议使用方法三。将必要的 <repository> 标记添加到 ~/.m2/settings.xml,Maven 将能够正确解析它们。 如果该文件还不存在,您可以复制上面的示例,只需将变量替换为相应的项目 ID。