Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push from Gitlab to Github with webhooks

My Google-fu is failing me for what seems obvious if I can only find the right manual.

I have a Gitlab server which was installed by our hosting provider The Gitlab server has many projects. For some of these projects, I want that Gitlab automatically pushes to a remote repository (in this case Github) every time there is a push from a local client to Gitlab. Like this: client --> gitlab --> github Any tags and branches should also be pushed.

AFAICT I have 3 options:

  1. Configure the local client with two remotes, and push simultaneous to Gitlab and Github. I want to avoid this because developers.
  2. Add a git post-receive hook in the repository on the Gitlab server. This would be most flexible (I have sufficient Linux experience to write shell scripts as git hooks) and I have found documentation on how to do this, but I want to avoid this too because then the hosting provider will need to give me shell access.
  3. I use webhooks in Gitlab. I am unfamiliar with what the very basics of webhooks are, and I am unable to locate understandable documentation or even a simple step-by-step example. This is the documentation from Gitlab that I found and I do not understand it: http://demo.gitlab.com/help/web_hooks/web_hooks

I would appreciate good pointers, and I will summarize and document a solution when I find it.

EDIT

I'm using this Ruby code for a web hook:

class PewPewPew < Sinatra::Base
  post '/pew' do
    push = JSON.parse(request.body.read)
    puts "I got some JSON: #{push.inspect}"
  end
end

Next: find out how to tell the gitlab server that it has to push a repository. I am going back to the GitLab API.

EDIT

I think I have an idea. On the server where I run the webhook, I pull from GitLab and then I push to Github. I can even do some "magic" (running tests, building jars, deploying to Artifactory,...) before I push to GitHub. In fact it would be great if Jenkins were able to push to a remote repository after a succesful build, then I wouldn't need to write my own webhook, because I'm pretty sure Jenkins already provides a webhook for Gitlab, either native or via a plugin. But I don't know. Yet.

EDIT

I solved it in Jenkins. You can set more than one git remote in an Jenkins job. I used Git Publisher as a Post-Build Action and it worked like a charm, exactly what I wanted.

like image 414
Amedee Van Gasse Avatar asked Oct 13 '14 15:10

Amedee Van Gasse


People also ask

Does GitLab support Webhooks?

GitLab Webhooks allows using Webhook notifications for all of the major lifecycle events in the software development process. It can trigger Webhooks for Code Push Events, Issue Changes, Comments, Merge Requests, Pipeline Events, Wiki Page Events, Deployment Events, Release Events, etc.

How do I link a webhook to GitHub?

To set up a webhook, go to the settings page of your repository or organization. From there, click Webhooks, then Add webhook. Alternatively, you can choose to build and manage a webhook through the Webhooks API. Webhooks require a few configuration options before you can make use of them.

Can I link GitLab to GitHub?

In GitLab, enable GitHub project integration: On the left sidebar, select Settings > Integrations. Select the Active checkbox. Paste your personal access token and HTTPS repository URL into the form and select Save.


1 Answers

  1. would work of course.

  2. is possible but dangerous because GitLab shell automatically symlinks hooks into repositories for you, and those are necessary for permission checks: https://github.com/gitlabhq/gitlab-shell/tree/823aba63e444afa2f45477819770fec3cb5f0159/hooks so I'd rather stay away from it.

  3. Web hooks are not suitable directly: they make an HTTP request with fixed format on certain events, in your case push, not Git protocol requests.

    Of course, you could write a server that consumes the hook, clones and pushes, but a service (single push and no deployment) or GitLab CI (already implements hook management) would be strictly better solutions.

  4. services are a the best option if someone implements it: live in the source tree, would do a single push, and require no extra deployment overhead.

  5. GitLab CI or othe CIs like Jenkins are the best option currently available. They are essentially already implemented server for the webhooks, which automatically clone for you: all you have to do then is to push from them.

The keywords you want to Google for are "gitlab mirror github". That has led me to: Gitlab repository mirroring for instance. There seems to be no perfect, easy solution today.

Also this has already been proposed at the feature request forum at: http://feedback.gitlab.com/forums/176466-general/suggestions/4614663-automatic-push-to-remote-mirror-repo-after-push-to Always check there ;) Go and upvote the request.

The key difficulty now is how to store the push credentials.