问题描述
是否可以将 gitlab 设置为自动同步(镜像)托管在其他位置的存储库?
Is it possible to have gitlab set up to automatically sync (mirror) a repository hosted at another location?
目前,我所知道的最简单的方法是手动推送到两个(gitlab 和另一个)存储库,但这既耗时又容易出错.
At the moment, the easiest way I know of doing this involves manually pushing to the two (gitlab and the other) repository, but this is time consuming and error prone.
最大的问题是,如果两个用户同时将更改推送到两个不同的存储库,则镜像可以重新同步.我能想出的防止此问题的最佳方法是确保用户只能推送到其中一个存储库.
The greatest problem is that a mirror can resynchronize if two users concurrently push changes to the two different repositories. The best method I can come up with to prevent this issue is to ensure users can only push to one of the repositories.
推荐答案
2016 年 12 月更新:GitLab EE 8.2+ 支持镜像:参见仓库镜像".
Update Dec 2016: Mirroring is suported with GitLAb EE 8.2+: see "Repository mirroring".
正如齐晓东所评论:
无需使用任何命令行即可简化此答案(只需在 Gitlab repo 管理界面上设置即可)
原始答案(2013 年 1 月)
Original answer (January 2013)
如果您的远程镜像仓库是一个 bare 仓库,然后你可以在你的 gitlab 管理的仓库中添加一个 post-receive 钩子,然后推送到你的远程仓库中.
If your remote mirror repo is a bare repo, then you can add a post-receive hook to your gitlab-managed repo, and push to your remote repo in it.
#!/bin/bash
git push --mirror slave_user@mirror.host:/path/to/repo.git
作为 Gitolite(由 Gitlab 使用)提及:
As Gitolite (used by Gitlab) mentions:
如果您只想在几个特定的存储库中安装钩子,请直接在服务器上进行.
将在:
~git/repositories/yourRepo.git/hook/post-receive
警告(2014 年 10 月更新)
Caveat (Update Ocotober 2014)
Ciro Santilli 指出 在评论中:
今天(2014 年第 4 季度)这将失败,因为 GitLab 会自动将 github.com/gitlabhq/gitlab-shell/tree/... 符号链接到它管理的每个存储库中.
因此,如果您进行此更改,您修改的每个存储库都将尝试推送.
更不用说升级 gitlab-shell
时可能发生的冲突,而且当前脚本是 ruby 脚本,而不是 bash(你不应该删除它!).
您可以通过读取当前目录名称并确保它与远程目录之间的双射来纠正此问题,但我建议人们远离这些东西
You could correct this by reading the current directory name and ensuring bijection between that and the remote, but I recommend people to stay far far away from those things
查看(并投票)反馈"推送到 GitLab 仓库后自动推送到远程镜像仓库".
See (and vote for) feeadback "Automatic push to remote mirror repo after push to GitLab Repo".
2016 年 7 月更新:我看到为 GitLab EE(企业版)添加了这种功能:MR 249
Update July 2016: I see this kind of feature added for GitLab EE (Enterprise Edition): MR 249
- 在镜像存储库设置下添加输入远程推送 URL 的功能
- 添加实现代码以推送到远程存储库
- 添加新的后台工作人员
- 显示最新更新日期和同步错误(如果存在).
- 每小时同步一次远程镜像.
注意最近的Remote Mirror Repository
(issues17940) 可能很棘手:
Note that the recent Remote Mirror Repository
(issues 17940) can be tricky:
我目前正在尝试转移我公司 Lossless GmbH (https://www.npmjs.com/~lossless) 从 GitHub.com 到 GitLab.com
我正在从 GitHub 导入所有存储库,但是当我尝试关闭 Mirror Repository
并使用原始 GitHub URL 打开 Remote Mirror Repository
时,我得到一个错误提示:
I'm importing all the repos from GitHub, however when I try to switch off Mirror Repository
and switch on Remote Mirror Repository
with the original GitHub URL I get an error saying:
Remote mirrors url is already in use
这是失败的存储库之一:https://gitlab.com/pushrocks/npmts2个月前编辑
Here is one of the repos this fails with: https://gitlab.com/pushrocks/npmtsEdited 2 months ago
事实证明,它只需要多个步骤:
- 禁用镜像存储库
- 按保存
- 删除网址
- 按保存
- 然后添加远程镜像
这篇关于Gitlab 仓库镜像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!