GitLab:如何在管道作业中使用来自另一个存储库的代码?

GitLab: How can I use code from another repository in pipeline job?

我有一个如下所示的 GitLab 项目:

在 first-api 和 second-api 的构建过程中,我将需要执行数据库迁移,以便能够 运行 与启动的数据库进行集成测试一项服务。

由于存储库是 public 我尝试克隆数据库迁移,构建然后执行。不幸的是,它在克隆步骤中失败了。这是来自我的 .gitlab-ci.yml:

init_db:
  stage: build
  script:
    - git clone https://gitlab.com/groupname/database-migration.git
    - cd database-migration
    - mvn exec:java

我收到这个错误:

$ git clone https://gitlab.com/groupname/database-migration.git
Cloning into 'database-migration'...
fatal: could not read Username for 'https://gitlab.com': No such device or address
ERROR: Job failed: exit code 1

这是正确的方法还是有更好的解决方案?如果这是正确的方法;我该如何让它发挥作用?

编辑,尝试子模块

在输入我应该使用子模块之后,我改变了我的方法。 我已经添加了一个文件 .gitmodules 到 first-api 看起来像这样:

[submodule "database-migration"]
  path = database-migration
  url = ../database-migration.git

.gitlab-ci.yml 我有以下内容:

image: maven:latest

variables:
  GIT_SUBMODULE_STRATEGY: recursive

init_db:
  stage: build
  script:
    - ls
    - cd database-migration
    - mvn exec:java

作业失败,因为文件夹 "database-migration" 不存在。

如果您想查看存储库,可以找到 here。我正在尝试在 "game-rest-api".

中使用子模块

我做错了什么?

您可以将 database-migration 作为 first-api 的子模块。 参见 https://docs.gitlab.com/ce/ci/git_submodules.html

别忘了加上

variables:
  GIT_SUBMODULE_STRATEGY: recursive

到.gitlab-ci.yml

  1. 创建子模块

git clone git@gitlab.com:groupname/first-api.git cd first-api.git git submodule add git@gitlab.com:groupname/database-migration.git

  1. 编辑 .gitmodules

nano gitmodules

git@gitlab.com:groupname/database-migration.git替换为../../groupname/database-migration.git

  1. 添加

variables: GIT_SUBMODULE_STRATEGY: recursive

到.gitlab-ci.yml

  1. 提交并推送

git commit -am "Add submodule" git push