如何在我的 gitlab runner 中使用子模块?
How to use a submodule in my gitlab runner?
- 我创建了一个名为 test-kirby-group 的私人群组。
- 我在该组中创建了一个名为 test-project-1 的私人项目
- 我在该组中创建了另一个名为 test-sub-project 的私人项目
- 我使用默认自述文件实例化了两个新项目。
- 在 test-kirby-group/test-project-1 中,我添加了一个 .gitmodules 文件
[submodule "vendor/submodules/test-sub-project"]
path = submodules/test-sub-project
url = ../../test-group-kirby/test-sub-project.git
- 在 test-kirby-group/test-project-1 中,我添加了一个 .gitlab-ci.yml 文件
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 ../../
...然后我检查了作业输出...
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
问题:
1. 我是否应该看到名为 vendor 的目录?
2. 如果是,为什么不克隆?
看来您可能没有正确添加git submodule
。
在 test-kirby-group/test-project-1
中执行以下操作:
- 删除手动创建的当前
.gitmodules
- 运行
git submodule add <git@gitlab ...> <path/to/submodule>
- 您现在可能需要编辑上述命令生成的
.gitmodules
以使用相对 URL(即您已经拥有的 url = ../../test-group-kirby/test-sub-project.git
),以便它与 GitLab CI
- 您不需要当前的
before_script
,只要您使用的是 gitlab-runner
v1.10+,您只需要在 CI 脚本中添加以下内容:
variables:
GIT_SUBMODULE_STRATEGY: recursive
有关详细信息,请参阅 Using Git submodules with GitLab CI。
- 我创建了一个名为 test-kirby-group 的私人群组。
- 我在该组中创建了一个名为 test-project-1 的私人项目
- 我在该组中创建了另一个名为 test-sub-project 的私人项目
- 我使用默认自述文件实例化了两个新项目。
- 在 test-kirby-group/test-project-1 中,我添加了一个 .gitmodules 文件
[submodule "vendor/submodules/test-sub-project"]
path = submodules/test-sub-project
url = ../../test-group-kirby/test-sub-project.git
- 在 test-kirby-group/test-project-1 中,我添加了一个 .gitlab-ci.yml 文件
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 ../../
...然后我检查了作业输出...
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
问题: 1. 我是否应该看到名为 vendor 的目录? 2. 如果是,为什么不克隆?
看来您可能没有正确添加git submodule
。
在 test-kirby-group/test-project-1
中执行以下操作:
- 删除手动创建的当前
.gitmodules
- 运行
git submodule add <git@gitlab ...> <path/to/submodule>
- 您现在可能需要编辑上述命令生成的
.gitmodules
以使用相对 URL(即您已经拥有的url = ../../test-group-kirby/test-sub-project.git
),以便它与 GitLab CI - 您不需要当前的
before_script
,只要您使用的是gitlab-runner
v1.10+,您只需要在 CI 脚本中添加以下内容:
variables:
GIT_SUBMODULE_STRATEGY: recursive
有关详细信息,请参阅 Using Git submodules with GitLab CI。