我在开发机上有一个本地的git仓库。
我想使用git将代码推送到生产服务器,并在代码被推送到生产服务器后触发web服务器的重启。
请问应该使用哪些命令?我正在使用debian系统。
/var/www/example.com
是我的本地仓库。
生产环境主机名为production.example.com
,我已经设置好了ssh密钥。
直接将代码推到运行中的网站通常不是一个好主意。为此,您可以在公共目录之外的服务器上创建一个裸仓库。该仓库将向您的Live网站仓库推送更改,并像“门卫”一样保护您的网站。要设置这些内容,请在您的运行中的网站目录周围创建一个仓库:
$ cd ~/www
$ git init
$ git add .
$ git commit -m"initial import of pre-existing web files"
创建完成后,进入一个不可通过HTTP访问的目录。为了保持简单,我们将这个空目录称为“HUB”,网站目录称为“LIVE”。在此处初始化一个裸仓库:
$ cd; mkdir site_hub.git; cd site_hub.git
$ git --bare init
$ cd ~/www
$ git remote add hub ~/site_hub.git
$ git remote show hub
* remote hub
URL: /home/rizwan/site_hub.git
$ git push hub master
你需要钩子来提交更改到实时存储库。在HUB存储库中创建一个post-update
钩子:
#!/bin/sh
echo
echo "**** Pulling changes into Live [Hub's post-update hook]"
echo
cd $HOME/www || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info
在这个钩子函数中,你可以编写代码,在拉取完成后重启服务器。
此外,在LIVE仓库中创建一个post-commit
钩子函数,将对网站所做的更改发送回HUB。
#!/bin/sh
echo
echo "**** pushing changes to Hub [Live's post-commit hook]"
echo
git push hub
git remote add hub <hub-repository-url>
这是如何工作的:您编写一些代码并将其推送到裸仓库,该裸仓库使用其 post-update 钩子将更改推送到实时仓库并重新启动服务器。
GIT_DIR=/path/to/bare/repo/.git
GIT_WORK_TREE=/var/www/www.example.org git checkout -f