如何让gitlab CI checkout 我的子模块?

lmvvr0a8  于 2023-06-28  发布在  Git
关注(0)|答案(1)|浏览(157)

我的应用程序由. NET C#WebApi后端和npm/node/Javascript前端组成。
我有一个. gitlab-ci. yml脚本下面是gitlab CI配置文件。我删除了不相关的文本。

stages:
 - build
build-backend:
  image: mcr.microsoft.com/dotnet/core/sdk:3.1
  stage: build
  script:
    - echo "build-backend"
    - dotnet clean WebApiServer --configuration Release
    - dotnet build WebApiServer --configuration Release    
  artifacts:
    name: "Backend-${CI_JOB_NAME}_${CI_COMMIT_REF_NAME}"
    expire_in: 1 day
    paths:
      - WebApiServer/bin/Release  
build-frontend:
  image: node:12
  stage: build
  script:
    - echo "build-frontend"
    - git submodule init
    - git submodule update
    - cd WebApiServer/client
    - npm install
    - node node_modules/webpack/bin/webpack.js --env=prod --progress --profile --config=webpack.prod.js  
  artifacts:
    name: "Frontend-${CI_JOB_NAME}_${CI_COMMIT_REF_NAME}"
    paths:
      - build
    expire_in: 1 day

后端构建正确。
但是前端给出了一个错误。gitlab似乎无法检出子模块。尽管子模块与后端托管在同一个gitlab服务器上。

$ git submodule init
$ git submodule update
Cloning into '/builds/subcompany/clientname/WebApiServer/WebApiServer/client'...
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'git@git.company.ab:subcompany/clientname/WebApiServerclient.git' into submodule path '/builds/subcompany/clientname/WebApiServer/WebApiServer/client' failed
Failed to clone 'WebApiServer/client'. Retry scheduled
Cloning into '/builds/subcompany/clientname/WebApiServer/WebApiServer/client'...
Host key verification failed.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
fatal: clone of 'git@git.company.ab:subcompany/clientname/WebApiServerclient.git' into submodule path '/builds/subcompany/clientname/WebApiServer/WebApiServer/client' failed
Failed to clone 'WebApiServer/client' a second time, aborting
ERROR: Job failed: exit code 1

. gitmodules

[submodule "WebApiServer/client"]
    path = WebApiServer/client
    url = git@git.company.ab:subcompany/clientcompany/client.git
    branch = development

问题
1.如何让gitlab checkout 子模块?
1.在gitmodules中,我应该使用SSH类型的URL还是HTTPS类型的URL?

    • 更新**

我试过GIT_SUBMODULE_STRATEGY: recursive。我不想使用GIT_SUBMODULE_STRATEGY: recursive
使用GIT_SUBMODULE_STRATEGY: recursive时的错误消息

Synchronizing submodule url for 'WebApiServer/client'
17Cloning into '/builds/subcompany/clientname/WebApiServer/WebApiServer/client'...
18error: cannot run ssh: No such file or directory
19fatal: unable to fork
20fatal: clone of 'git@git.company.ab:subcompany/clientname/client.git' into submodule path '/builds/subcompany/clientname/WebApiServer/WebApiServer/client' failed
21Failed to clone 'WebApiServer/client'. Retry scheduled
22Cloning into '/builds/subcompany/clientname/WebApiServer/WebApiServer/client'...
23error: cannot run ssh: No such file or directory
24fatal: unable to fork
25fatal: clone of 'git@git.company.ab:subcompany/clientname/client.git' into submodule path '/builds/subcompany/clientname/WebApiServer/WebApiServer/client' failed
26Failed to clone 'WebApiServer/client' a second time, aborting
28ERROR: Job failed: exit code 1.
pinkon5k

pinkon5k1#

除了设置variables.GIT_SUBMODULE_STRATEGY,并在.gitmodules文件中使用相对URL之外,还可以使用以下内容:

[submodule "WebApiServer/client"]
path = WebApiServer/client
url = ../client.git

您还需要在子模块的项目配置中启用令牌访问:

Settings > CI/CD Settings > Token Access

确保启用了“允许使用CI_JOB_TOKEN访问此项目”,并将父项目的路径添加到“允许来自以下项目的CI作业令牌访问此项目”下方的字段,然后单击“添加项目”。
这将确保您的父项目具有克隆子模块存储库的权限。

相关问题