如何创建Gitlab webhook来更新Github上的镜像仓库?

Yam*_*eko 35 github webhooks gitlab

我想在Gitlab中创建一个webhook,以便在push事件发生时自动更新Github上的镜像存储库.我已经检查了这个页面,但我不明白它是如何完成的.

我的Gitlab版本是6.5.这是配置页面:

inserir描述da imagem aqui

我应该在URL中放什么?我需要在哪里放置脚本来更新存储库?

nov*_*ore 40

你不需要webhook.一个普通的post-receive钩子可以很好地工作.

要创建和使用这样的钩子,您只需登录安装了gitlab的服务器,并为git用户创建一个ssh密钥.

sudo -u git ssh-keygen -f /home/git/.ssh/reponame_key
Run Code Online (Sandbox Code Playgroud)

(提示时不要输入任何密码)

转到您的github帐户,并将公钥(它已创建为/home/git/ssh/reponame_key.pub)作为部署密钥添加到您的项目中.如果您需要帮助,查看https://help.github.com/articles/managing-deploy-keys.

完成后,你只需配置你的git服务器和github之间的连接:为git用户的ssh配置添加一个别名(添加以下行/home/git/.ssh/config- 如果它不存在则创建它)

Host reponame  
IdentityFile /home/git/.ssh/reponame_key  
HostName github.com  
User git 
Run Code Online (Sandbox Code Playgroud)

现在将新的远程(使用您刚刚创建的别名)添加到您的存储库:

cd /home/git/repositories/namespace/reponame.git

git remote add --mirror github reponame:youruser/reponame.git
Run Code Online (Sandbox Code Playgroud)

现在一切都到位了,你必须创建实际的钩子:

cd /home/git/repositories/namespace/reponame.git/hooks

echo "exec git push --quiet github &" >> post-receive

chmod 755 post-receive
Run Code Online (Sandbox Code Playgroud)

last命令非常重要,因为git会在运行之前检查钩子是否可执行.

而已!

(根据您的真实账户替换reponame,名称空间和用户并享受).

最后一点:如果你想在github上提交你的名字andavatar,请确保你在gitlab上使用的电子邮件地址也是你的github帐户的地址之一.否则你会看到你的gitlab用户名.

  • 警告:这将影响所有存储库!请参阅:http://stackoverflow.com/questions/14288288/gitlab-repository-mirroring#comment41606464_14291690 (2认同)

mln*_*ncn 29

如果您没有托管自己的GitLab,GitLab.com已直接引入此功能,无需任何解决方法.

  1. 在项目中使用齿轮图标选择Mirror Repository
  2. 向下滚动到Push to a remote repository
  3. 选中标记远程镜像存储库:每小时自动更新远程镜像的分支,标记和提交.
  4. 输入要更新的存储库; 对GitHub上,你可以包括你的用户名和密码的网址,例如:https://yourgithubusername:yourgithubpassword@github.com/agaric/guts_discuss_resource.git-如在评论中指出,这是很多更好securitywise,而不是用你的GitHub的访问令牌这里登录凭证; 我测试时会更新答案.

  • 为了更好的安全性,生成具有"repo"范围的访问令牌以与镜像一起使用:https://github.com/settings/tokens然后在配置步骤#4时使用此令牌而不是密码 (3认同)
  • 请注意,从安全角度来看,以gitlab的方式提供github用户和密码可能不是最好的.这些不仅是您的主要凭据,而且您还授予gitlab权限以访问您具有推送权限的任何其他github存储库.在gitlab中实现了对基于ssh的推镜的支持,你可以将它与github的单个repo部署密钥结合起来,并且应该很快就可以在gitlab 11.6中使用:https://gitlab.com/gitlab-org/gitlab-ce/issues/49565 (3认同)