Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Gitlab webhook to update a mirror repo on Github?

I would like to create a webhook within Gitlab to automatically update a mirror repository on Github, whenever a push event happens. I've checked this page, but I didn't understand how it is done.

My Gitlab version is 6.5. Here is the configuration page:

inserir a descrição da imagem aqui

What should I put in URL? Where do I need to place the script to update the repository?

like image 312
Yamaneko Avatar asked Feb 23 '14 01:02

Yamaneko


People also ask

How do I mirror a GitHub repository?

Navigate to the repository you just cloned. Pull in the repository's Git Large File Storage objects. Mirror-push to the new repository. Push the repository's Git Large File Storage objects to your mirror.


2 Answers

You don't need a webhook for that. A regular post-receive hook will work very well.

To create and use such a hook you just have to login on the server where your gitlab is installed and create an ssh key for git user.

sudo -u git ssh-keygen -f /home/git/.ssh/reponame_key 

(do not type any passphrase when prompted)

Go to your github account and add the public key (it's been created as /home/git/ssh/reponame_key.pub) to your project as a deploy key. have a look at https://help.github.com/articles/managing-deploy-keys if you need help with that.

Once that is done, you just have to configure the connection between your git server and github's: add an alias to git user's ssh configuration (add following lines to /home/git/.ssh/config - create it if it's not present)

Host reponame   IdentityFile /home/git/.ssh/reponame_key   HostName github.com   User git  

Now add the new remote (using the alias you just created) to your repository:

cd /home/git/repositories/namespace/reponame.git  git remote add --mirror github reponame:youruser/reponame.git 

Now that everything is in place you'll have to create the actual hook:

cd /home/git/repositories/namespace/reponame.git/hooks  echo "exec git push --quiet github &" >> post-receive  chmod 755 post-receive 

The lastcommand is very important because git will check if a hook is executable before running it.

That's it!

(Replace reponame, namespace and youruser according to your real accounts and enjoy).

Last note: if you want your name andavatar near commits on github, make sure that the email address you are using on gitlab is one of the addresses inked to your github account as well. You'll see your gitlab username otherwise.

like image 53
novalore Avatar answered Oct 06 '22 12:10

novalore


If you aren't hosting your own GitLab, 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 you can include your username and password in the URL, like so: https://yourgithubusername:[email protected]/agaric/guts_discuss_resource.git —as noted in the comments, it is much better securitywise to use your GitHub access token here instead of login credentials; will update the answer when i've tested.
like image 31
mlncn Avatar answered Oct 06 '22 11:10

mlncn