WebHook using automated deployment project
today in laravist.com see this Webhook Git project automatically deploy this instructional videos, before and he wanted to do such a use Git WebHook automated deployment, but always unsuccessful, to think of today see the video try it again, although there are video reference but still encountered some pit, where I write down their deployment.
Note:
1. I was in de style = "box-sizing: border-box; font-family: Menlo, monaco, Consolas, 'Courier New' , monospace; font-size: 12.6px; padding: 2px 4px; color: rgb (199, 37, 78); border-radius: 4px; "> coding.netde> operation to do, other similar git repository should be able to use and reference.
2. My Web environment to run user is de style = "box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb (199, 37, 78); border-radius: 4px; "> wwwde>, so that part of the operation will switch to de style =" box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb (199, 37, 78); border-radius: 4px; "> wwwde> user.
3.需要把de style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; " >shell_exec()de>或者de style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; " >exec()de>两个函数从de style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; " >php.inide>的de style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78);border-radius: 4px; "> disable_functionde> cancel.
Run the user 4.Web environment requires read and write permissions to deploy Web directory.
Process
1. In the de style = "box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb ( 199, 37, 78); border -radius: 4px; "> coding.netde> a new library
2 for generating a Git Key deployed on the server: de style =" box-sizing : border-box; font- family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb (199, 37, 78); border-radius: 4px; "> sudo -Hu www -t-keygen rsade SSH>
3. deploying public key added to the project.
4. cloning operation performed in the server: de style = "box-sizing : border-box; font-family: Menlo, Monaco, Consolas , 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb (199, 37, 78); border-radius: 4px; "> sudo -Hu www git clone [email protected] the .NET: XXXXXX / XXXXXXde>
5.Add an accessible deployment files deploy.php
de style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: inherit; padding: 0.5em; color: rgb(248, 248, 242); border-radius: 0px; display: block; background: rgb(35, 36, 31);" ><?php error_reporting(1); $target = '/www/web/imwhere/weixin/weiphp'; // 生产环境web目录 $token = '**************';//自定义token $json = json_decode(file_get_contents('php://input'), true); if (empty($json['token']) || $json['token'] !== $token) { exit('error request'); } $cmd = "cd $target && git pull"; exec($cmd); de>
6. Add deploy.php link to WebHook in.
7. Local push updates to coding.net server automatically deployed.
Recommended Reference:
1. Using Git deployment site: http://www.imwhere.cn/index.php/archives/3/
2. WebHook use Git to automatically deploy to achieve PHP Code: http://m.aoh.cc/149 .html
3.Webhook automatically deploy Git project: https://laravist.com/series/something-that-a-little-helpful/episodes/3
4. questions about LINUX subdirectory permissions required parent directory X: HTTP: / /www.imwhere.cn/index.php/archives/39/
5.coding.net WebHook parameters: https://open.coding.net/webhook.html
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1. generate a public key
There are two public key: 1 git user's public key, 2 to deploy a public key.:
git user's public key
1
2
3
|
ssh-keygen -t rsa -C "[email protected]"
# Then press Enter on the line has been
# Resulting file is usually /root/.ssh/id_rsa, if non-root users check out tips on path
|
The deployment of public key
sudo -Hu www ssh-keygen -t rsa # Please select "no passphrase", has been going Enter
2. Prepare hook file
Create and modify directory permissions:
1
2
|
mkdir /home/wwwroot/website.com/hook
chown -R www:www /home/wwwroot/website.com/hook
|
Write hooks file:
1
|
sudo -Hu www touch /home/wwwroot/website.com/hook/index.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php
error_reporting(1);
$ Target = '/home/wwwroot/website.com'; // production environment web directory
$ Token = 'you fill in the coding hook token';
$wwwUser = 'www';
$wwwGroup = 'www';
$json = json_decode(file_get_contents('php://input'), true);
if (empty($json['token']) || $json['token'] !== $token) {
exit('error request');
}
$repo = $json['repository']['name'];
// $cmds = array(
// "cd $target && git pull",
// "chown -R {$wwwUser}:{$wwwGroup} $target/",
// );
// foreach ($cmds as $cmd) {
// shell_exec($cmd);
// }
// Thanks @ ink where correction can be used to pull the www user code directly without having to pull and then modify each user group
// $ cmd = "sudo -Hu www cd $ target && git pull"; a problem
$ Cmd = "cd $ target && git pull"; read this
shell_exec($cmd);
|
Make sure your hook files can be accessed: http: //example.com/hook/index.php, hooks ready.
3. Modify the git git configure and save user name and password
1
2
3
|
sudo -Hu www git config --global credential.helper store # permanent preservation
sudo -Hu www git config --global user.name "Bantes"
sudo -Hu www git config --global user.email "[email protected]" # 邮箱请与conding上一致
|
在Coding网站
1.添加用户公钥
复制de style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.5px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; " >/root/.ssh/id_rsa.pubde>内容到个人设置页的SSH公钥里添加即可(https://coding.net/user/account/setting/keys)
2.添加部署公钥
复制de style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13.5px; padding: 2px 4px; color: rgb(199, 37, 78); border-radius: 4px; " >/home/www/.ssh/id_rsa.pubde>的内容并添加到部署公钥:
选择项目 > 设置 > 部署公钥 > 新建 > 粘贴到下面框并确认
3.添加hook
选择项目 > 设置 > WebHook > 新建hook > 粘贴你的hook/index.php所在的网址。比如:http://example.com/hook/index.php, 令牌可选,但是建议写上。
稍过几秒刷新页面查看hook状态,显示为绿色勾就OK了。
初始化
1.我们需要先在服务器上clone一次,以后都可以实现自动部署了:
1
|
sudo -Hu www git clone https://git.coding.net/yourname/yourgit.git /home/wwwroot/website.com/ --depth=1
|
这个时候应该会要求你输入一次Coding的帐号和密码,因为上面我们设置了永久保存用户名和密码,所以之后再执行git就不会要求输入用户名和密码了。
**!!注意,这里初始化clone必须要用www用户**
2.往Coding.net提交一次代码测试:
在本地clone的仓库执行:
1
2
|
git commit -am "test hook" --allow-empty
git push
|
OK,稍过几秒,正常的话你在配置的项目目录里就会有你的项目文件了。