Kirby
Kirby

Reputation: 2008

How to use a submodule in my gitlab runner?

On https://gitlab.com

  1. I created a private group called test-kirby-group.
  2. I created a private project in that group called test-project-1
  3. I created another private project in that group called test-sub-project
  4. I instantiated both new projects with a default readme.
  5. In test-kirby-group/test-project-1 , i added a .gitmodules file
[submodule "vendor/submodules/test-sub-project"]
    path = submodules/test-sub-project
    url = ../../test-group-kirby/test-sub-project.git
  1. In test-kirby-group/test-project-1 , i added a .gitlab-ci.yml file
variables:
    GIT_SUBMODULE_STRATEGY: recursive

before_script:
    - apk update && apk add git
    - git submodule sync --recursive
    - git submodule update --init --recursive

test:
    script:
        - pwd
        - ls -al
        - ls -al ../
        - ls -al ../../

... I then checked the job output ...

Running with gitlab-runner 12.3.0 (a8a019e0)
  on docker-auto-scale ed2dce3a
Using Docker executor with image ruby:2.5 ...
Pulling docker image ruby:2.5 ...
Using docker image sha256:e216e233b2581d7f45d2bc2c4dce4f1f293267b29c45bfd929d038a9a67b4058 for ruby:2.5 ...
Running on runner-ed2dce3a-project-15133453-concurrent-0 via runner-ed2dce3a-srm-1572642947-b5456b44...
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/test-group-kirby/test-project-1/.git/
Created fresh repository.
From https://gitlab.com/test-group-kirby/test-project-1
 * [new ref]         refs/pipelines/93160262 -> refs/pipelines/93160262
 * [new branch]      master                  -> origin/master
Checking out b624748a as master...

Updating/initializing submodules recursively...
$ apk update && apk add git
/bin/bash: line 89: apk: command not found
$ git submodule sync --recursive
$ git submodule update --init --recursive
$ pwd
/builds/test-group-kirby/test-project-1
$ ls -al
total 48
drwxrwxrwx. 3 root root 4096 Nov  1 21:17 .
drwxrwxrwx. 4 root root 4096 Nov  1 21:17 ..
drwxrwxrwx. 6 root root 4096 Nov  1 21:17 .git
-rw-rw-rw-. 1 root root  274 Nov  1 21:17 .gitlab-ci.yml
-rw-rw-rw-. 1 root root  141 Nov  1 21:17 .gitmodules
-rw-rw-rw-. 1 root root   18 Nov  1 21:17 README.md
$ ls -al ../
total 32
drwxrwxrwx. 4 root root 4096 Nov  1 21:17 .
drwxrwxrwx. 3 root root 4096 Nov  1 21:17 ..
drwxrwxrwx. 3 root root 4096 Nov  1 21:17 test-project-1
drwxrwxrwx. 3 root root 4096 Nov  1 21:17 test-project-1.tmp
$ ls -al ../../
total 24
drwxrwxrwx. 3 root root 4096 Nov  1 21:17 .
drwxr-xr-x. 1 root root 4096 Nov  1 21:17 ..
drwxrwxrwx. 4 root root 4096 Nov  1 21:17 test-group-kirby
Job succeeded

Question: 1. Should I expect to see a directory called vendor? 2. If so, why didn't it clone?

Upvotes: 2

Views: 6361

Answers (1)

Rekovni
Rekovni

Reputation: 7364

It seems that you may have not added the git submodule correctly.

In test-kirby-group/test-project-1 do the following:

  • Remove the current .gitmodules which was created manually
  • Run git submodule add <git@gitlab ...> <path/to/submodule>
  • You might need to now edit the generated .gitmodules from the above command to use a relative URL (i.e. what you already have url = ../../test-group-kirby/test-sub-project.git), for it to work with GitLab CI
  • You shouldn't need your current before_script, as long as you are using gitlab-runner v1.10+, you only need the following in your CI script:
variables:
    GIT_SUBMODULE_STRATEGY: recursive

See Using Git submodules with GitLab CI for more information.

Upvotes: 5

Related Questions