最近申请了一个阿里云主机,准备搞个人项目,我希望最简化项目发布流程,用jenkins还是觉得不够简单,所以决定用git hooks
假设本地仓库叫做home,远程库叫做base(只保存版本信息、不保存代码),远程发布的仓库叫做deploy(最终访问的目录)
本地仓库
$ mkdir home && cd home
F:\UPUPW_NP7.0\htdocs\home>
$ git init
$ touch index.php
F:\UPUPW_NP7.0\htdocs\home [master +1 ~0 -0 !]>
$ echo "echo '1';">index.php
$ git add .
$ git commit -m "test"
远程保存版本信息的base仓库
//ssh 连接阿里云
ssh 127.28.74.349
# cd /home/workspace/
# mkdir base && cd base
root@xxxxxx:/home/workspace/base#
# git init --bare
创建post-receive脚本
root@xxxxxx:/home/workspace/base#
# touch hooks/post-receive
# vim hooks/post-receive
#!/bin/bash
echo "Running post-receive"
set -xe # 报错则停止
unset $(git rev-parse --local-env-vars)
cd /home/workspace/deploy/ # 发布的目录
/usr/bin/git checkout master # checkout master 分支
/usr/bin/git pull # 直接pull
:wq
# chmod u+x hooks/post-receive
发布目录
创建发布目录、直接clone即可
root@xxxxxx:/home/workspace/base#
# cd ..
root@xxxxxx:/home/workspace
# git clone base deploy
回到本地
F:\UPUPW_NP7.0\htdocs\home
$ git remote add test root@127.28.74.349:/home/workspace/base
上面也可以用ssh协议
$ git remote add test ssh://root@127.28.74.349/home/workspace/base
push
$ git push test master