37

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?

Yamaneko
  • 3,433
  • 2
  • 38
  • 57

3 Answers3

40

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.

novalore
  • 586
  • 5
  • 11
  • Another thing, the Gitlab 6.5 directories have `.git` at the end of the name (e.g., `dotfiles.git`) and they are not `git` repositories. So, I couldn't execute the command that adds the GitHub mirror. – Yamaneko Feb 27 '14 at 21:58
  • 1
    @VictorHugo You are also right about the .git ending of the directories. You are, however, wrong when you say that they are not git repositories, they are [bare repositories](http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/) and contain a hook directory specifically designed for uses like the one I'm illustrating. – novalore Mar 06 '14 at 11:51
  • 1
    It worked. Thank you very much @novalore. I would like to share some debug steps to see if everything was set up fine. After including the new host name in `/home/git/.ssh/config`, one can see if it worked by running `sudo -u git ssh reponame`. It will show: `Hi youruser/reponame! You've successfully authenticated, but GitHub does not provide shell access. Connection to github.com closed.` – Yamaneko Mar 13 '14 at 22:31
  • After including the command into the `post-receive` file, mine only started working after manually running the command `exec git push github`. – Yamaneko Mar 13 '14 at 22:33
  • 2
    WARNING: this will affect all repositories! See: http://stackoverflow.com/questions/14288288/gitlab-repository-mirroring#comment41606464_14291690 – Ciro Santilli OurBigBook.com Oct 21 '14 at 12:01
  • how to do it in bitbucket? – Geshan Nov 07 '14 at 05:11
32

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:yourgithubpassword@github.com/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.
mlncn
  • 3,308
  • 2
  • 23
  • 20
  • Currently, this new approach seems to be the easiest if you're using GitLab.com. However, the answer by @novalore is more general and covers any GitLab setup. Thus, I'll keep it as the answer to the question, but this one may also be valid. – Yamaneko Jul 27 '16 at 15:46
  • 1
    In GitLab.com this option is only available for Bronze users right now – Francisco Puga Feb 09 '18 at 11:28
  • 3
    For better security, generate an access token with "repo" scope for use with your mirrors: https://github.com/settings/tokens Then use this token instead of your password when configuring step #4 – CenterOrbit Aug 05 '18 at 11:53
  • 3
    Please note that providing your github user & password in such a way to gitlab is probably not the best from a security perspective. Not only are those your main credentials, but you are also giving gitlab permissions to access any other github repositories in which you have push rights. Support for ssh-based push mirrors is being implemented in gitlab, you can combine that with github's single repo deploy keys, and should be available soon in gitlab 11.6: https://gitlab.com/gitlab-org/gitlab-ce/issues/49565 – dlouzan Nov 21 '18 at 15:38
  • the 'todo' is old, new version of gitlab work different – carlo denaro Mar 12 '19 at 11:25
1

For WebHooks processing I'm using sinatra web server.

require 'sinatra'
post '/pew' do
  puts JSON.parse request.body.read
  # here can be placed signal code to run commit processing script
end

register webhook for push events(or other) to http://localhost:4567/pew within GitLab and since this moment on each commit gitlab will be sending commit info to url.

Nikolay Ruban
  • 981
  • 9
  • 11