目标服务器切www用户: su www
切换用户(www)时:this account is currently not available
cat /etc/passwd | grep www # 查看是否为 /sbin/nolgin
解决办法: vim /etc/passwd
修改 /sbin/nolgin
为 /bin/bash
php
在目标服务器上生成 ssh 公钥,生成公钥在 /home/www/.ssh 文件夹下: ssh-keygen -t rsa -C "your_name"
git
部署公钥至Git平台: 公钥位置:/home/www/.ssh/id_rsa.pub
在工蜂平台新增SSH公钥:工蜂平台 -> 我的设置 -> SSH密钥 -> 添加SSH密钥, 复制粘贴 id_rsa.pub
中内容; github
使用 www 用户 git clone 代码。 须要使用 SSH 地址 git@github.com:someaccount/someproject.git
,若以前是使用 https 地址更新,则使用如下命令切换至 SSH 地址: git remote set-url origin git@github.com:someaccount/someproject.git
针对非www用户已部署代码,可修改其用户权限至 www: chown -R www:www code_folder
web
安装 githook.php 文件至外网能够访问的位置,如 test.com/githook.phpshell
<?php
/* security */
$token = 'token12345';
$project = '/home/wwwroot/default/test/test';
//ip地址为gitlab服务器请求地址
$access_ip = [];
/* get user token and ip address */
$client_token = isset($_SERVER['HTTP_X_TOKEN']) ? $_SERVER['HTTP_X_TOKEN'] : '';
$client_ip = $_SERVER['REMOTE_ADDR'];
//查询服务器运行的php-fpm用户和文件所属权限是否一致
//print_r($_SERVER);
//文件记录日志
/* create open log */
$fs = fopen($project . '/storage/webhook.log', 'a');
fwrite($fs, '===================================start====================================' . PHP_EOL);
fwrite($fs, 'Request on [' . date("Y-m-d H:i:s") . '] from [' . $client_ip . ']' . PHP_EOL);
/* test token */
if ($client_token !== $token) {
echo "error 403";
fwrite($fs, "Invalid token [{$client_token}]" . PHP_EOL);
exit(0);
}
/* test ip*/
// if ( ! in_array($client_ip, $access_ip))
// {
// echo "error 503";
// fwrite($fs, "Invalid ip [{$client_ip}]".PHP_EOL);
// exit(0);
// }
/* get json data */
$json = file_get_contents('php://input');
$data = json_decode($json, true);
/* get branch */
$branch = $data["ref"];
$total_commits_count = $data["total_commits_count"];
fwrite($fs, '=======================================================================' . PHP_EOL);
/* if you need get full json input */
//fwrite($fs, 'DATA: '.print_r($data, true).PHP_EOL);
/* branch filter */
if ($branch === 'refs/heads/develop' && $total_commits_count > 0) {
/* if master branch*/
fwrite($fs, 'BRANCH: ' . print_r($branch, true) . PHP_EOL);
fwrite($fs, '=======================================================================' . PHP_EOL);
/* then pull master */
// $result = shell_exec("cd {$project} && git pull 2>&1");
$result = shell_exec("cd {$project} && git pull 2>&1");
fwrite($fs, 'RESULT: ' . print_r($result, true) . PHP_EOL);
fwrite($fs, '===================================end====================================' . PHP_EOL);
$fs and fclose($fs);
}
复制代码
在工蜂平台添加 hook,注意秘密令牌须要与 githook.php 文件中的 token 一至 位置:项目 -> 设置 -> 高级设置 -> 网络回调勾子 json
部署完成,当 git 有 push 更新时,目标服务器端将自动拉取;vim