0


PHP分布式部署代码同步Git实现

PHP 分布式部署后 代码自动同步实现

项目架构如下:
在这里插入图片描述

需要更新代码时我们只需要把代码传到主服务器后通过定时任务主服务器自动push 代码到Git服务端,之后其他从服务器则自动从Git云端拉取最新的代码即可

需要用到 expect 软件
安装expect

yum install expect

定时push shell(不要用win编辑器编辑 不然cd 找不到文件)

#!/usr/bin/bash
cd /home/wwwroot/test
git add .
git commit -a -m auto
set timeout 60
/usr/bin/expect <<-EOF
spawn git push ssh://[email protected]/home/gitprojects/test master
expect "*password:"
send "123456\r"
interact
expect eof
EOF

定时pull shell

#!/usr/bin/bash
cd /home/test
#延迟5s 等待push 后在拉取
sleep 5
set timeout 60
/usr/bin/expect <<-EOF
spawn git pull ssh://[email protected]/home/test master
expect "*password:"
send "123456\r"
interact
expect eof
EOF

定时任务 主服务器每分钟执行 push,从服务器每分钟执行pull

* * * * * sh /home/wwwroot/test/git_push.sh
* * * * * sh /home/wwwroot/test_server/git_pull.sh

gitignore 把不需要同步的文件处理掉

application/install.lock
application/upload.lock
/.idea
/runtime
/logs
/data
/uploads/pic_cache
/.htaccess
.htaccess
.project

更新gitignore

git rm -r --cached .
git add .
git commit -m 'update .gitignore'

**注意:
1:编辑shell 不要用win 编辑器,不然cd 容易找不到文件目录
2:git 服务仓库文件夹权限应该归属推送账号,客户端运行项目权限应该归属php所属账号
3:拓容服务器拉下代码时注意项目文件夹权限要归属于php所属用户,不然代码无法创建文件夹,些日志那些
4:拓容服务器的代码不要手动改动,改了最后搞完要还原回去,不然定时pull 会有冲突,pull失败,如果已经被修改则可以手动强制同步远端代码(命令如下)
**

$ git fetch --all
$ git reset --hard origin/master
$ git pull
标签: git php 分布式

本文转载自: https://blog.csdn.net/lqb3732842/article/details/128853718
版权归原作者 qq_540616979 所有, 如有侵权,请联系我们删除。

“PHP分布式部署代码同步Git实现”的评论:

还没有评论