git 如何设置 Gitlab Web Hook 进行部署
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22169225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to setup Gitlab Web Hook to deployment
提问by bala-16
I have created a new project and branches in that project. I need to configure one particular branch to my development server. Whenever pushed to that branch it should be automatically deployed to the server.
我在该项目中创建了一个新项目和分支。我需要为我的开发服务器配置一个特定的分支。每当推送到该分支时,它应该自动部署到服务器。
I have tried with Web Hook in Gitlab. But it did not work. Please give a ref. step by step link. What configurations I need to do in my local machine as well as the server system. I have found that we need to setup some "Post Receive WebHook in server", can someone please give an idea.
我在 Gitlab 中尝试过 Web Hook。但它没有用。请给一个参考。一步一步链接。我需要在我的本地机器以及服务器系统中进行哪些配置。我发现我们需要设置一些“在服务器中发布接收 WebHook”,有人可以提供一个想法。
"Here I will explain what is in my hand. The server already setup without git. I have one project inside one branch "develop". I have cloned in my local and pushing to the branch. It is working fine. (I am using window machine git bash). But here after when I push some update I need to push to server. So, I have tried with Web Hook. But it is not working. "
“在这里我将解释我手中的东西。服务器已经在没有 git 的情况下设置。我在一个分支“开发”中有一个项目。我已经在本地克隆并推送到分支。它工作正常。(我正在使用window machine git bash)。但在这里,当我推送一些更新后,我需要推送到服务器。所以,我尝试过使用 Web Hook。但它不起作用。“
回答by Shalako Lee
I also ran into this issue, there is hardly any documentation for beginners.
我也遇到了这个问题,初学者几乎没有任何文档。
Here are the steps I took to get this working with gitlab:
以下是我在 gitlab 中使用的步骤:
make sure to have an SSH key on the server that you want to auto deploy on, for me this was an Ubuntu server. I had to generate an SSH key for the www-data so the user could connect to the gitlab server.
确保在要自动部署的服务器上有一个 SSH 密钥,对我来说这是一个 Ubuntu 服务器。我必须为 www-data 生成一个 SSH 密钥,以便用户可以连接到 gitlab 服务器。
I had to run this command to generate an ssh key for the www-data user:
我必须运行这个命令来为 www-data 用户生成一个 ssh 密钥:
sudo -u www-data ssh-keygen -t rsa
After you generate the ssh key you have to add it to the gitlab to your projects deploy keys. (project->settings->deploy keys).
生成 ssh 密钥后,您必须将其添加到 gitlab 到您的项目部署密钥中。(项目-> 设置-> 部署密钥)。
now goto the folder that you want your project to push to (for me it was /var/www/website/) and do the following:
现在转到您希望项目推送到的文件夹(对我来说是 /var/www/website/)并执行以下操作:
git init
git clone origin-url .
the dot at the end makes it so it only pulls the files, and doesn't include the folder your project is in on gitlab.
最后的点使它只提取文件,不包括您的项目在 gitlab 上的文件夹。
then...
然后...
chgrp -R www-data .git
chmod -R g+rwxs .git
That gives permission to the git folder to execute php commands and adds the www-data group to the folder.
这将授予 git 文件夹执行 php 命令的权限,并将 www-data 组添加到该文件夹中。
after that i had to make a script that would run the commands i wanted when the hook was called.
之后,我必须制作一个脚本,该脚本可以在调用钩子时运行我想要的命令。
here is the php that i am using, i named the file webhook.php
这是我正在使用的 php,我将文件命名为 webhook.php
<?php
if($payload = file_get_contents('php://input')) {
try {
$payload = json_decode($payload);
} catch(Exception $ex) {
echo $ex;
exit(0);
}
// put the branch you want here
if($payload->ref != "refs/heads/master") {
echo "wrong head";
exit(0);
}
//put the branch you want here, as well as the directory your site is in
$result = `cd /var/www/website && git fetch origin && git merge origin/master`;
echo $result;
} else {
echo "failed request";
}
?>
the webhook.php will need execute permissions from www-data as well.
webhook.php 也需要来自 www-data 的执行权限。
once all that is done you will need to make the webhook in your project (project->settings->webhooks)
完成所有操作后,您需要在项目中创建 webhook(项目->设置->webhooks)
add the url to the webhook.php
将 url 添加到 webhook.php
I put my webhook.php in a subdomain on the server so I could use it to push different sites with the same file.
我将 webhook.php 放在服务器上的一个子域中,以便我可以使用它来推送具有相同文件的不同站点。
http://deploy.website.com/webhook.php
http://deploy.website.com/webhook.php
I also added used http://requestb.inso that I could make sure of the data that was getting sent to the server.
我还添加了 used http://requestb.in以便我可以确保发送到服务器的数据。
Not sure if i'm forgetting anything, but I think this was everything i did
不确定我是否忘记了什么,但我认为这就是我所做的一切
回答by Lo?c
There is a configuration option which allows that (turned off by default). On push it will synchronize the server directly.
有一个配置选项允许(默认情况下关闭)。在推送时,它将直接同步服务器。
If your git server isn't on the same machine than your http server you can use that : http://ryanflorence.com/deploying-websites-with-a-tiny-git-hook/
如果您的 git 服务器与您的 http 服务器不在同一台机器上,您可以使用它:http: //ryanflorence.com/deploying-websites-with-a-tiny-git-hook/