主页

linux安装git并使用WebHooks自动拉取

回頭是墻   IP属地: 青海
0 点赞 0阅读 1821

1.安装git

安装命令
yum install git

已安装的可以查看git版本
git --version

2.配置git信息

git config --global user.name "5287xxx"
git config --global user.email "528772xxx@qq.com"
git config --global credential.helper store
//会生成.gitconfig 的文件,查看
cat ~/.gitconfig //查看配置内容

3.由于linux默认安装是禁用了shell_exec函数,我门需要开启,这里我使用的是宝塔

在php.ini的配置文件disable_functions里把shell_exec删掉,如果是使用宝塔就更为方便了
下面截图是使用宝塔的方式删除shell_exec


可以在查看配置文件里的配置看shell_exec是否被删除掉

建议:开发阶段使用就可以了,如果正式上线就把该函数禁用

4.编写请求代码



<?php
 
//本地路径
$local = '/www/wwwroot/ceshi.jifengyinyue.com/testmonsoont';
//仓库地址
$remote = 'https://1550xxxx:528772xxx@gitee.com/showlb/monsoons.git';
//https://528772568@qq.com:528xxxxx@gitee.com/showlb/monsoons.git linux系统使用方式
//$remote = 'https://gitee.com/showlb/monsoons.git';//win系统使用方式使用
// echo shell_exec("cd {$local} && git pull {$remote} 2>&1");
//密码
$password = '528xxxx';
//获取请求参数
$request = file_get_contents('php://input');
if (empty($request)) {
    die('request is empty');
}
//验证密码是否正确
$data = json_decode($request, true);
if ($data['password'] != $password) {
    die('password is error');
}
echo shell_exec("cd {$local} && git pull {$remote} 2>&1");
die('done ' . date('Y-m-d H:i:s', time()));

注意:地址格式 https://[username]:[password]@gitee.com/[username]/project.git

username:手机号,注意避坑不要使用邮箱
用码云里的webHooks管理调试,如果出现如下提示代表拉取成功



可能在调试拉取的过程中报错:

warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:

  git config pull.rebase false  # merge (the default strategy)
  git config pull.rebase true   # rebase
  git config pull.ff only       # fast-forward only

You can replace "git config" with "git config --global" to set a default
preference for all repositories. You can also pass --rebase, --no-rebase,
or --ff-only on the command line to override the configured default per
invocation.

remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 51.49 KiB | 850.00 KiB/s, done.

直接按照提示执行git config pull.rebase false 命令就可以了,如果报的是
fatal: could not read Username for 'https://gitee.com': No such device or address

那就是请求地址username错误,格式https://[username]:[password]@gitee.com/[username]/project.git,username是手机号不是邮箱


© 著作权归作者所有,转载或内容合作请联系作者