如何将一个GitLab实例镜像到另一个实例,同时在此过程中应用一些简单的重写规则



我们面临以下用例:一个外部供应商正在开发一个项目,该项目由其GitLab实例中的多个repo(约40个(组成。我们想成为他们GitLab(主(的镜像(奴隶(。然而,我们也需要应用一些简单的重写规则(替换一些URL和标识符,这可以用一个简单的脚本来完成(。解决这一问题的最佳方法是什么?

我已经能够使用Project Access Tokens设置基于推送的镜像,这就像一个魅力。不过,它不允许我应用任何重写规则。

对于40个repo中的每个供应商分支(或仅master分支(,在供应商repo中都有一个影子分支,并重写代码。也就是说,在供应商端重写这些URL/标识符是没有问题的。如何用git挂钩以无冲突的方式设置,例如在每次推送时?或者你有更好的主意吗?

谢谢!

我使用Project Access令牌,并在GitLab CI/CD作业(在供应商端(内运行重写脚本和git mergegit push,成功地解决了这个问题。大致如下:

# setup git credentials
print "https://${GIT_USER}:${GIT_PASSWORD}@${GIT_HOST}n" >> /root/.git-credentials
git config --global credentials.helper 'store --file=/root/.git-credentials'
# clone the slave (our) repo (works because of the credentials set above)
git clone slave-repo-url
# checkout the commit branch in question (might already exist or not)
git checkout -B branch --track origin/branch || git checkout -b branch
# add a new remote (vendor)
git remote add --tags vendor vendor-repo-url
# pull from the vendor branch
# note: this uses a recursive merge strategy but this is fine
git pull --no-edit -X theirs vendor branch
# rewrite the files; create the rewrite commit
# ...
# push the changes back to the origin (our slave)
git push --tags --set-upstream origin branch

这是一个自动解决方案,适用于任何分支机构,也适用于边缘情况(新回购、新分支机构等(。标签包括在内。它要求您预先创建相应的从repo,并在其中配置一个项目访问令牌(具有write_repository权限(($GIT_PASSWORD(。

相关内容