自动化能解放人类的双手,并且更重要的是,由于按照规定的流程来走,也减小了不少误操做的产生。不知道你们平时都是怎么样更新本身生产环境的代码的,FTP 覆盖旧文件、服务器定时任务去 build 最新的源码,仍是有更高级的作法?前端
目前我在使用 Git Hook 来部署本身的项目。Git Hook 是 Git 提供的一个钩子,能被特定的事件触发后调用。其实,更通俗的讲,当你设置了 Git Hook 后,只要你的远程仓库收到一次 push 以后,Git Hook 就能帮你执行一次 bash 脚本。git
下面是我使用 Git Hook 进行简单的自动化部署,可能还有更高级的作法,你们本身去挖掘。web
git init
和 git --bare init
初始化出来的仓库是彻底不同的,具体我 Google 了下,英文却是理解了,可是要翻译出中文殊不知道用什么形容词去称呼这2种仓库。json
这里咱们要经过 git --bare init
初始化一个远程仓库vim
1
2
3
4
|
$
cd ~
$ mkdir
testRepo
$
cd testRepo
$ git --bare init
|
这个仓库就是经过 git init
初始化出来最多见的本地仓库,它的做用是拉去远程仓库(其实就在它旁边)最新的源码,而后在这个仓库里进行编译,把代码编译到 www 目录(网站的根目录)。segmentfault
1
2
3
4
5
6
7
8
9
10
11
|
$
cd ~
$ mkdir
testDeploy
$
cd testDeploy
$ git
clone ~/testRepo #从远程仓库 clone 出源码
```
### 为远程仓库设置 Hook
```bash
$
cd ~/testRepo/hooks
$ vim post-receive
|
post-receive
里面的执行脚本bash
1
2
3
4
5
6
7
8
9
10
11
12
|
unset GIT_DIR
DeployPath=/home/user/
testDeploy
WwwPath=/home/wwwroot/
testDeploy
cd $DeployPath
git add . -A && git stash
git pull origin master
# 下面这2步都是按照实际你本身添加的bash脚本
fis release -Dompd
$WwwPath # 我使用的FIS,对前端代码进行编译
qrsync /home/user/qiniutools/config.json
# 使用七牛同步工具进行同步
|
最后,为 post-receive
添加可执行权限服务器
1
|
chmod +x post-receive
|
此次的本地仓库就真的是你开发机上面的本地了。在你原有 Git 项目里面添加一条新的 remote 源,之后往这个 remote 源里面 push 代码就会自动触发上面那 bash 脚本了。工具
1
2
|
$ git remote add deploy user@server.ip:/home/user/
testRepo
$ git push deploy master
|
https://dearb.me/archive/2015-03-30/automate-deploy-your-websites-with-git-hook/post