GitHooks使用.md

简书迁移

Posted by thrfox on June 29, 2020

问题描述: 服务器需要部署Django应用,且需要继续在本地windows下继续开发,GitHooks实现从本地推送至服务器并自动部署


###1. 服务器上建一个裸仓库(git的远端仓库)

1
2
cd /home/thrza/workspace
git init --bare AmazingCode.git

###2.建立一个普通仓库,拉取服务器远端仓库,存放网站源代码

1
2
cd /home/thrza/nginx/project/AmazingCode
git clone /home/thrza/workspace/AmazingCode.git

###3.配置GitHook

1
2
cd /home/thrza/workspace/AmazingCode.git/hooks
vi post-receive

该post-receive文件会在每次git push之后触发

1
2
3
4
5
6
7
8
9
10
11
12
post-receive

#!/bin/sh
unset GIT_DIR
Path="/usr/local/nginx/project/AmazingCode"
echo "deploying the blog web project"
cd $Path
git fetch --all
git reset --hard origin/master
touch /home/thrza/shell/uwsgi-reload
time='date'
echo "pull at webserver at time: $time."

还有post-commit等其他触发 详见Git挂钩Git-Hooks

###4.本地关联服务器的远端仓库

1
2
3
4
5
git init
git add .
git commit -m "first commit"
git remote add origin ssh://xxx:22/home/thrza/workspace/AmazingCode.git
git push -u origin master