简体   繁体   English

如何使用webhooks从Gitlab推送到Github

[英]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. 如果我只能找到合适的手册,我的Google-fu会让我失望。

I have a Gitlab server which was installed by our hosting provider The Gitlab server has many projects. 我有一个由我们的托管服务提供商安装的Gitlab服务器Gitlab服务器有很多项目。 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. 对于其中一些项目,我希望Gitlab每次从本地客户端推送到Gitlab时自动推送到远程存储库(在本例中为Github)。 Like this: client --> gitlab --> github Any tags and branches should also be pushed. 像这样:client - > gitlab - > github也应该推送任何标签和分支。

AFAICT I have 3 options: AFAICT我有3个选择:

  1. Configure the local client with two remotes, and push simultaneous to Gitlab and Github. 使用两个遥控器配置本地客户端,并同时推送到Gitlab和Github。 I want to avoid this because developers . 我想避免这种情况,因为开发人员
  2. Add a git post-receive hook in the repository on the Gitlab server. 在Gitlab服务器的存储库中添加一个git post-receive钩子。 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. 这将是最灵活的(我有足够的Linux经验将shell脚本编写为git hooks)并且我已经找到了关于如何执行此操作的文档,但我也想避免这种情况,因为托管服务提供商需要给我shell访问权限。
  3. I use webhooks in Gitlab. 我在Gitlab中使用webhooks。 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. 我不熟悉webhooks的基础知识,我无法找到可理解的文档,甚至是一个简单的分步示例。 This is the documentation from Gitlab that I found and I do not understand it: http://demo.gitlab.com/help/web_hooks/web_hooks 这是我发现的Gitlab文档,我不明白它: 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: 我正在使用这个Ruby代码进行Web挂钩:

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. 下一步:了解如何告诉gitlab服务器必须推送存储库。 I am going back to the GitLab API. 我将回到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. 在我运行webhook的服务器上,我从GitLab拉出然后我推送到Github。 I can even do some "magic" (running tests, building jars, deploying to Artifactory,...) before I push to GitHub. 在推送到GitHub之前,我甚至可以做一些“魔术”(运行测试,构建jar,部署到Artifactory,......)。 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. 事实上,如果Jenkins能够在成功构建之后能够推送到远程存储库,那将是很棒的,然后我不需要编写自己的webhook,因为我非常确定Jenkins已经为Gitlab提供了一个webhook,无论是本机还是通过插件。 But I don't know. 但我不知道。 Yet. 然而。

EDIT 编辑

I solved it in Jenkins. 我在詹金斯解决了这个问题。 You can set more than one git remote in an Jenkins job. 您可以在Jenkins作业中设置多个git远程。 I used Git Publisher as a Post-Build Action and it worked like a charm, exactly what I wanted. 我使用Git Publisher作为后构建动作,它就像一个魅力,正是我想要的。

  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. 可能但很危险,因为GitLab shell会自动将符号链接挂钩到存储库中,这些是权限检查所必需的: https//github.com/gitlabhq/gitlab-shell/tree/823aba63e444afa2f45477819770fec3cb5f0159/hooks所以我宁愿远离它。

  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. Web挂钩不适合直接使用:它们在某些事件上以固定格式发出HTTP请求,在您的情况下是推送,而不是Git协议请求。

    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. 当然,您可以编写一个消耗钩子,克隆和推送的服务器,但是服务(单推和无部署)或GitLab CI(已实现挂钩管理)将是更好的解决方案。

  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. GitLab CI或其他像Jenkins这样的CI是目前可用的最佳选择。 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. 它们基本上已经为webhooks实现了服务器,它会自动为你克隆:你所要做的就是从它们中推送。

The keywords you want to Google for are "gitlab mirror github". 您想要Google的关键字是“gitlab mirror github”。 That has led me to: Gitlab repository mirroring for instance. 这导致我:例如Gitlab存储库镜像 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. 这也已经在功能请求论坛上提出: http//feedback.gitlab.com/forums/176466-general/suggestions/4614663-automatic-push-to-remote-mirror-repo-after-push-to总是检查那里;)去和upvote请求。

The key difficulty now is how to store the push credentials. 现在的关键难点是如何存储推送凭证。

I solved it in Jenkins. 我在詹金斯解决了这个问题。 You can set more than one git remote in an Jenkins job. 您可以在Jenkins作业中设置多个git远程。 I used Git Publisher as a Post-Build Action and it worked like a charm, exactly what I wanted. 我使用Git Publisher作为后构建动作,它就像一个魅力,正是我想要的。

I added "-publisher" jobs that run after "" is built successfully. 我添加了在成功构建“”之后运行的“-publisher”作业。 I could have done it in one job, but I decided to split it up. 我本可以在一份工作中完成它,但我决定把它分开。 The build jobs are triggered by a web hook in GitLab; 构建作业由GitLab中的Web钩子触发; the publisher jobs are using a @daily schedule from the BuildResultTrigger plugin. 发布者作业正在使用BuildResultTrigger插件中的@daily计划。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM