首页 > 解决方案 > 如何将一个 GitLab 实例镜像到另一个,同时应用一些简单的重写规则?

问题描述

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

我已经能够使用项目访问令牌设置基于推送的镜像,这就像一个魅力。但是,它不允许我应用任何重写规则。

对于我们来说,对于master他们的 40 个存储库中的每个供应商分支(或只是分支),在供应商存储库中使用重写代码的影子分支也是可行的。即,在供应商端重写这些 URL/标识符将没有问题。如何使用 git hooks 以无冲突的方式设置它,例如在每次推送时?还是你有更好的主意?

谢谢!

标签: giturl-rewritinggitlabmigrationhook

解决方案


我设法使用项目访问令牌并运行重写脚本以及在 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

这是一个自动解决方案,适用于任何分支,也适用于边缘情况(新仓库、新分支等)。标签被覆盖。它要求您事先创建相应的从仓库,并在其中配置一个项目访问令牌(有write_repository权限)($GIT_PASSWORD)。


推荐阅读