首页 > 解决方案 > gitlab子模块foreach推送创建

问题描述

Gitlab 提供了这种方法可以将新项目直接推送到 gitlab,而无需先在 web-gui 上对其进行初始化。以下命令效果很好:

git push --set-upstream git@my.gitlab.com:username/project-name.git master

我想做同样的事情,但迭代新项目中的所有子模块。我正在尝试使用子模块 foreach 获取以下命令来执行此操作:

git submodule foreach --recursive 'sudo git push --set-upstream https://my.gitlab.com/namespace/$project-name.git master'

但它失败了:

Entering 'sub-repo-1'
> GitLab: The project you were looking for could not be found.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Stopping at 'sub-repo-1'; script returned non-zero status.

在我的用例中,我有一个项目树,其中包含我想推送到 gitlab 的数百个子模块。最终,我想将其集成到 CI 工作流程中,但首先要做的是。

为什么子模块 foreach push-to-create 在 gitlab CI 上不起作用的任何想法?

标签: gitgitlabgitlab-cigit-submodules

解决方案


通过反复试验,我解决了自己的问题。这是一个文件名、权限和回显问题。该命令有效。请注意,子模块 foreach 公开了该$name变量。

git submodule foreach --recursive 'git push --set-upstream git@my.gitlab.com:root/`printf $name | sed "s/[^a-zA-Z0-9]/-/g"`.git master'

推荐阅读