阿里云code结合git管理代码,运用webHook同步部署服务器代码(php)
项目场景:
阿里云code结合git管理代码,运用webHook同步部署服务器代码(php)
问题描述:
git生成**时linux用户为root,php运行用户是www。结果导致在服务器端git pull没有问题,但是触发webHook调用同步的php脚本时却一直不行。
原因分析:
切换到www用户,手动git pull时报错Permission denied (publickey)。
**不对,重新生成。
解决方案:
重新生成**,并添加到阿里云code的**列表中。手动测试git pull,结果出现The authenticity of host 'gitee.com (120.55.226.24)' can't be established。Are you sure you want to continue connecting (yes/no)? 输入yes。
原因:在新生成**之后,在.ssh文件夹(之前的文章有提到过)中少了一个known_hosts文件,本来**文件应该是三个,现在是两个,便报了这样的错误,此时选择yes回车之后,便可,同时生成了缺少了的known_hosts文件。
之后检查项目文件,发现代码已经同步。
git 命令
- 查看git 配置 git config -l
- 查看远程仓库 git remote -v
- 添加远程仓库 git remote add origin <url>
- 删除远程仓库 git remote rm origin(仓库名称)
- 下载远程项目 git clone ssh地址
- 生成ssh公钥 ssh-****** -t rsa -C "邮箱@example.com" 按3次Enter,不需要设置名称与密码。
- 设置用户名 git config --global user.name 'userName'
-
删除配置 git config --global unset user.name
php脚本
echo shell_exec('/var/www/gitupdate/update.sh');
注意:php需要开启shell_exec()函数,并且update.sh需要可执行权限。添加域名使php脚本可访问,例:http://gitUpdate.XXX.com/pull.php
shell脚本
#!/bin/bash
cd /var/www/xiangmu && git pull
说明:/var/www/xaingmu 是你的项目目录,就是需要同步的文件目录
添加配置webHook