我想在Gitlab中创建一个Webhook,以便在发生push
事件时自动更新Github上的镜像存储库。我查看了这个页面,但我不知道如何操作。
我的Gitlab版本是6.5。这是配置页面:
URL字段应该填什么?我需要把脚本放在哪里来更新存储库?
你不需要使用Webhook来实现这个功能。一个常规的post-receive hook就可以非常好地工作。
为了创建和使用这样的hook,你只需要在安装了你的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
现在将新的远程仓库(使用您刚创建的别名)添加到您的代码库中:
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
现在一切都准备就绪,你需要创建实际的钩子:cd /home/git/repositories/namespace/reponame.git/hooks
echo "exec git push --quiet github &" >> post-receive
chmod 755 post-receive
最后一个命令非常重要,因为Git将在运行钩子之前检查它是否可执行。
就是这样!
(根据您的真实帐户替换reponame、命名空间和youruser,并享受它)。
最后注意:如果您想在GitHub上显示提交时使用的名称和头像,请确保您在GitLab上使用的电子邮件地址也是链接到您GitHub帐户的地址之一。否则,您将看到您的GitLab用户名。
.git
(例如dotfiles.git
),但它们不是git
仓库。因此,我无法执行添加GitHub镜像的命令。 - Yamaneko/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.
- Yamanekopost-receive
文件中后,我的只有在手动运行命令 exec git push github
后才开始工作。 - Yamaneko如果您不想自己托管GitLab,GitLab.com已经直接引入了此功能,无需任何变通方法。
https://yourgithubusername:yourgithubpassword@github.com/agaric/guts_discuss_resource.git
- 正如注释中所述,更安全的做法是在此处使用GitHub访问令牌而不是登录凭据;我测试过后将更新答案。为了处理WebHooks,我使用sinatra web服务器。
require 'sinatra'
post '/pew' do
puts JSON.parse request.body.read
# here can be placed signal code to run commit processing script
end
http://localhost:4567/pew
,从此每次提交后GitLab都会将提交信息发送到该URL。