前言
出于安全或其他考虑,可以为每个子仓库配置独立的账号,分别授予主仓库和子仓库相应的访问权限。在部署服务器上使用对应的账号进行代码拉取操作。
Tip
在.gitmodules文件中,需要将子模块的URL修改为相对路径。
GitLab实现方式
以下步骤不包括账号权限配置。
第 1 步
新增GitLab Runner
第 2 步
在主仓库中添加.gitlab-ci.yml配置文件
第 3 步
在主仓库中配置流水线触发器(WebHook)
第 4 步
在各个子仓库中分别添加钩子
.gitlab-ci.yml
stages:
- update
update_submodules:
stage: update
only:
- triggers
tags:
- docker
- ci
image: docker:git
variables:
GIT_SUBMODULE_STRATEGY: recursive
before_script:
- git config --global user.name "gitlab bot"
- git config --global user.email "gitlab-bot@tiho-mobi.com"
- git remote set-url origin https://gitlab-ci-token:${GIT_PUSH_TOKEN}@gitlab.test.com/test.git
script:
- |
git submodule update --remote --merge
git add .
if git diff-index --quiet HEAD; then
echo "Nothing changed"
else
git commit -m "Auto update submodules"
git push origin HEAD:master
fi
本地克隆
首次克隆
git clone https://dc-cn-sy:xxx@gitlab.xxx.com/xxx.git
git submodule init
git submodule update --remote deploy/${ENV}
后续更新
git pull && git submodule update --remote deploy/${ENV}
添加子模块
git submodule add https://dc-cn-sy:xxx@gitlab.tmofamily.com/xxx.git deploy/${ENV}