【发布时间】:2014-03-24 15:30:55
【问题描述】:
我想在 Gitlab 中创建一个 webhook,以便在发生 push 事件时自动更新 Github 上的镜像存储库。我已经检查了这个page,但我不明白它是如何完成的。
我的 Gitlab 版本是 6.5。这是配置页面:
我应该在 URL 中输入什么?我需要将脚本放在哪里来更新存储库?
【问题讨论】:
我想在 Gitlab 中创建一个 webhook,以便在发生 push 事件时自动更新 Github 上的镜像存储库。我已经检查了这个page,但我不明白它是如何完成的。
我的 Gitlab 版本是 6.5。这是配置页面:
我应该在 URL 中输入什么?我需要将脚本放在哪里来更新存储库?
【问题讨论】:
如果您没有托管自己的 GitLab,GitLab.com 已直接引入此功能,没有任何变通方法。
https://yourgithubusername:yourgithubpassword@github.com/agaric/guts_discuss_resource.git — 如 cmets 中所述,在此处使用您的 GitHub 访问令牌而不是登录凭据要安全得多;我测试后会更新答案。【讨论】:
您不需要为此使用 webhook。一个常规的 post-receive 钩子会很好用。
要创建和使用这样的钩子,您只需登录安装 gitlab 的服务器并为 git 用户创建一个 ssh 密钥。
sudo -u git ssh-keygen -f /home/git/.ssh/reponame_key
(提示时不要输入任何密码)
转到您的 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
现在将新的远程(使用您刚刚创建的别名)添加到您的存储库:
cd /home/git/repositories/namespace/reponame.git
git remote add --mirror github reponame:youruser/reponame.git
现在一切就绪,您必须创建实际的钩子:
cd /home/git/repositories/namespace/reponame.git/hooks
echo "exec git push --quiet github &" >> post-receive
chmod 755 post-receive
最后一条命令非常重要,因为 git 会在运行之前检查钩子是否可执行。
就是这样!
(根据你的真实账户替换reponame、namespace和youruser并享受)。
最后一点:如果您希望您的姓名和头像靠近 github 上的提交,请确保您在 gitlab 上使用的电子邮件地址也是与您的 github 帐户相关的地址之一。否则你会看到你的 gitlab 用户名。
【讨论】:
.git(例如dotfiles.git),它们不是git 存储库。所以,我无法执行添加 GitHub 镜像的命令。
/home/git/.ssh/config 中包含新主机名后,可以通过运行sudo -u git ssh reponame 来查看它是否有效。它将显示:Hi youruser/reponame! You've successfully authenticated, but GitHub does not provide shell access. Connection to github.com closed.
post-receive文件后,我的手动运行命令exec git push github后才开始工作。
对于 WebHooks 处理,我使用的是 sinatra 网络服务器。
require 'sinatra'
post '/pew' do
puts JSON.parse request.body.read
# here can be placed signal code to run commit processing script
end
在 GitLab 中注册用于推送事件(或其他)的 webhook 到 http://localhost:4567/pew
并且由于每次提交的这一刻,gitlab 都会将提交信息发送到 url。
【讨论】: