是否可以使用git push部署一个网站?我有一种预感,它与使用git挂钩来执行git重置有关——在服务器端很难,但我该如何完成这一点呢?


当前回答

从本质上讲,你所需要做的就是:

server = $1
branch = $2
git push $server $branch
ssh <username>@$server "cd /path/to/www; git pull"

我在我的应用程序中有这些行,作为一个名为deploy的可执行文件。

所以当我想要部署时,我输入。/deploy myserver mybranch。

其他回答

我的方法是在我的部署服务器上有一个裸露的Git存储库,我在那里推送更改。然后我登录到部署服务器,更改到实际的web服务器文档目录,并执行git拉取。我没有使用任何钩子来自动地做这个,这似乎比它的价值更麻烦。

You could conceivably set up a git hook that when say a commit is made to say the "stable" branch it will pull the changes and apply them to the PHP site. The big downside is you won't have much control if something goes wrong and it will add time to your testing - but you can get an idea of how much work will be involved when you merge say your trunk branch into the stable branch to know how many conflicts you may run into. It will be important to keep an eye on any files that are site specific (eg. configuration files) unless you solely intend to only run the one site.

或者,你有没有考虑过把改变推到网站上?

有关git钩子的信息,请参阅githooks文档。

我对基督徒解决方案的看法。

git archive --prefix=deploy/  master | tar -x -C $TMPDIR | rsync $TMPDIR/deploy/ --copy-links -av username@server.com:/home/user/my_app && rm -rf $TMPDIR/deploy

存档主分支到tar 解压tar档案到系统临时文件夹的部署目录。 Rsync更改到服务器 从临时文件夹中删除部署目录。

我使用toroid.org的以下解决方案,它有一个更简单的钩子脚本。

服务器端:

$ mkdir website.git && cd website.git
$ git init --bare
Initialized empty Git repository in /home/ams/website.git/

并在服务器上安装钩子:

$ mkdir /var/www/www.example.org
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
GIT_WORK_TREE=/var/www/www git clean -f -d # clean directory from removed files

$ chmod +x hooks/post-receive

在您的客户端:

$ mkdir website && cd website
$ git init
Initialized empty Git repository in /home/ams/website/.git/
$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -q -m "The humble beginnings of my web site."

$ git remote add web ssh://server.example.org/home/ams/website.git
$ git push web +master:refs/heads/master

然后要发布,只需输入

$ git push web

网站上有完整的描述:http://toroid.org/ams/git-website-howto

git配置——local receive.denyCurrentBranch updateInstead

在Git 2.3中添加了一个很好的可能性:https://github.com/git/git/blob/v2.3.0/Documentation/config.txt#L2155

您在服务器存储库上设置它,如果工作树是干净的,它也会更新工作树。

在2.4中有进一步的改进,包括push-to-checkout钩子和未出生分支的处理。

示例用法:

git init server
cd server
touch a
git add .
git commit -m 0
git config --local receive.denyCurrentBranch updateInstead

cd ..
git clone server local
cd local
touch b
git add .
git commit -m 1
git push origin master:master

cd ../server
ls

输出:

a
b

这确实有以下GitHub公告中提到的缺点:

Your server will contain a .git directory containing the entire history of your project. You probably want to make extra sure that it cannot be served to users! During deploys, it will be possible for users momentarily to encounter the site in an inconsistent state, with some files at the old version and others at the new version, or even half-written files. If this is a problem for your project, push-to-deploy is probably not for you. If your project needs a "build" step, then you will have to set that up explicitly, perhaps via githooks.

但是所有这些点都超出了Git的范围,必须由外部代码来处理。因此,从这个意义上说,这和Git钩子一起是最终的解决方案。