git - 如何将一个 GitLab 实例镜像到另一个,同时应用一些简单的重写规则?
问题描述
我们面临以下用例:外部供应商正在开发一个项目,该项目由其 GitLab 实例中的许多存储库(大约 40 个)组成。我们想成为他们 GitLab(主人)的镜像(奴隶)。但是,我们还需要应用一些简单的重写规则(替换一些 URL 和标识符,这可以通过一个简单的脚本来完成)。解决这个问题的最佳方法是什么?
我已经能够使用项目访问令牌设置基于推送的镜像,这就像一个魅力。但是,它不允许我应用任何重写规则。
对于我们来说,对于master
他们的 40 个存储库中的每个供应商分支(或只是分支),在供应商存储库中使用重写代码的影子分支也是可行的。即,在供应商端重写这些 URL/标识符将没有问题。如何使用 git hooks 以无冲突的方式设置它,例如在每次推送时?还是你有更好的主意?
谢谢!
解决方案
我设法使用项目访问令牌并运行重写脚本以及在 GitLab CI/CD 作业中(在供应商方面)解决了git merge
它git 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
)。
推荐阅读
- java - 爪哇。从一个 jar 执行测试
- azure - Azure 搜索 Microsoft 英语搜索分析器
- python - AWS Glue Boto3 - 在哪里可以找到服务员列表?
- android - NestedScrollView 的 smoothScrollTo() 行为怪异
- c++ - 正确实现 Timer1 以生成 PWM
- bash - sed 或 awk 删除匹配正则表达式的行和以下行
- symfony - Symfony 4 和 Zoho SKD 2.0
- java - 太阳球衣文件上传OutOfMemoryError
- amazon-web-services - AWS CloudFront Lambda@Edge 部署
- php - 在 PHP 中删除字符串中的所有类型的制表符、空格、回车符