Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab repository mirroring

Is it possible to have gitlab set up to automatically sync (mirror) a repository hosted at another location?

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.

like image 662
loopbackbee Avatar asked Jan 11 '13 23:01

loopbackbee


People also ask

What does mirroring a repository mean?

Repository Mirroring is a way to mirror repositories from external sources. It can be used to mirror all branches, tags, and commits that you have in your repository. Your mirror at GitLab will be updated automatically. You can also manually trigger an update at most once every 5 minutes.

What is a GitHub mirror?

repository-mirroring-actionA GitHub Action for mirroring a repository to another repository on GitHub, GitLab, BitBucket, AWS CodeCommit, etc. This will copy all commits, branches and tags.

What is git remote mirroring?

Git mirroring is when a mirror copies the refs & the remote-tracking branches. It's supposed to be a functionally identical copy that is interchangeable with the original.


2 Answers

Update Dec 2016: Mirroring is suported with GitLAb EE 8.2+: see "Repository mirroring".

As commented by Xiaodong Qi:

This answer can be simplified without using any command lines (just set it up on Gitlab repo management interface)


Original answer (January 2013)

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 [email protected]:/path/to/repo.git 

As Gitolite (used by Gitlab) mentions:

if you want to install a hook in only a few specific repositories, do it directly on the server.

which would be in:

~git/repositories/yourRepo.git/hook/post-receive 

Caveat (Update Ocotober 2014)

Ciro Santilli points out in the comments:

Today (Q4 2014) this will fail because GitLab automatically symlinks github.com/gitlabhq/gitlab-shell/tree/… into every repository it manages.
So if you make this change, every repository you modify will try to push.
Not to mention possible conflicts when upgrading gitlab-shell, and that the current script is a ruby script, not bash (and you should not remove it!).

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

See (and vote for) feeadback "Automatic push to remote mirror repo after push to GitLab Repo".


Update July 2016: I see this kind of feature added for GitLab EE (Enterprise Edition): MR 249

  • Add ability to enter remote push URL under Mirror Repository settings
  • Add implementation code to push to remote repository
  • Add new background worker
  • Show latest update date and sync errors if they exists.
  • Sync remote mirror every hour.

Note that the recent Remote Mirror Repository (issues 17940) can be tricky:

I'm currently trying to shift main development of the Open Source npm modules of my company Lossless GmbH (https://www.npmjs.com/~lossless) from GitHub.com to GitLab.com

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 

Here is one of the repos this fails with: https://gitlab.com/pushrocks/npmts Edited 2 months ago

turns out, it just requires multiple steps:

  • disable the Mirror Repository
  • press save
  • remove the URl
  • press save
  • then add the Remote Mirror
like image 163
VonC Avatar answered Sep 24 '22 15:09

VonC


If not hosting your own GitLab, it's worth knowing GitLab.com has introduced this feature directly, without any workarounds.

  1. From within a project use the gear icon to select Mirror Repository
  2. Scroll down to Push to a remote repository
  3. Checkmark Remote mirror repository: Automatically update the remote mirror's branches, tags, and commits from this repository every hour.
  4. Enter the repository you want to update; for GitHub at least you can include your username and password in the URL, like so: https://yourgithubusername:[email protected]/agaric/guts_discuss_resource.git

Note that if you are pulling from a remote repository, it will still push on to the remote repository set here. I haven't tried it, but you should be able to push to and pull from the same repository.

like image 24
mlncn Avatar answered Sep 24 '22 15:09

mlncn