0

I have a GitLab project looking like this: enter image description here

During the build of both first-api and second-api I will need to execute the database-migration to be able to run integration tests with a database started as a service.

Since the repository is public I tried to clone database-migration, build and then execute. Unfortionatly it fails on the clone step. This is from my .gitlab-ci.yml:

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

I get this error:

$ 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

Is this the correct approach or is there a better solution? If this is the correct approach; how do I make it work?

Edit, trying submodules

After input that I should use submodules I changed my approach. I have added a file .gitmodules to first-api looking like this:

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

In .gitlab-ci.yml I have the following:

image: maven:latest

variables:
  GIT_SUBMODULE_STRATEGY: recursive

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

The job fails because the folder "database-migration" does not exist.

If you want to look at the repository it can be found here. I am trying to use submodules in "game-rest-api".

What am I doing wrong?

David Berg
  • 1,958
  • 1
  • 21
  • 37
  • Possible duplicate of [\`git clone project2\` in gitlab-ci.yml?](https://stackoverflow.com/questions/44444616/git-clone-project2-in-gitlab-ci-yml) – secustor Feb 15 '18 at 21:38
  • I know this is a little late, but why have three repos instead of one? – Rob Grant Jul 22 '22 at 09:16

1 Answers1

4

You can make database-migration as submodule of first-api. See https://docs.gitlab.com/ce/ci/git_submodules.html

Do not forget to add

variables:
  GIT_SUBMODULE_STRATEGY: recursive

to .gitlab-ci.yml

i.e.

  1. Create submodule

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

  1. Edit .gitmodules

nano gitmodules

Replace git@gitlab.com:groupname/database-migration.git with ../../groupname/database-migration.git

  1. Add

variables: GIT_SUBMODULE_STRATEGY: recursive

to .gitlab-ci.yml

  1. Commit and push

git commit -am "Add submodule" git push

ilia
  • 1,082
  • 11
  • 19
  • Can someone describe a bit more in detail how to define a submodule? I have created .gitsubmodules and set path to ../database-migration.git. In the log I get ```Updating/initializing submodules recursively...``` but no indication that anything has been downloaded. When I run ```ls``` as a script I do not see anything downloaded either. – David Berg Feb 16 '18 at 21:50
  • 2
    @DavidBerg I added some details to my answer. You have to use `git submodule add` command and not just add .gitmodules file – ilia Feb 18 '18 at 07:46