Git for Websites / post-receive / Separation of Test and Production Sites

一个人想着一个人 提交于 2019-12-05 08:36:12

Would a better approach be to have my base repository be the test site repository (with corresponding working directory), and then have that repository push changes to a new live site repository, which has a corresponding working directory to the live site base? This would also allow me to move the production to a different server and keep the deployment chain intact.

Yes definitely. It's a very rare occasion you want your test site hosted right next to your production site. It's dangerous and unprofessional in almost every regard, not to speak about database corruption, webserver lockups etc.

I do usually have a VM setup for testing purposes. Works very well and I can have it with me on my laptop while travelling.

Using git to deploy your website is a very good idea, there are a lot of other people doing so (e.g. Rob Conery). If you happen to have a live and testing site anyway, you should have seperate branches for them in your repository, set-up as remote-tracking branches on the corresponding server repositories. Your workflow becomes as easy as doing work in your test branch, push it to test, test it, merge to live and push live.

Honestly, don't make it too hard for yourself.

Think that both ways will work.

You can also use "git archive master | tar -C c:/temp/BLAH -x" and "git archive live-site | ssh live-site 'tar -C /var/www -x'".

Keeping separate repositories may be useful, but "push inside another push-related hook" looks tricky and I expect it to be slow. Sort of long chain that will be slow and fragile.

May be live site updates should be manually triggered after testing the "testing" version?

I also followed the same guide at toroid.org, but I'd like to note that although you started with a bare repository, by adding a working directory, extra handling will most likely be needed. I found that the following hook is useful if you have content that may change dynamically or otherwise and don't want to lose data when using git checkout -f

pre-receive

#!/bin/sh
git add -A
git diff --quiet --cached
if [ $? -gt 0 ]; then
    git commit --quiet -m "autocommit"
    echo "Working Directory was out of sync. Pull to receive updated index."
    exit 1
fi

This will stop a push if there are changes in the remote working directory. Think of it as someone (the web server) making changes but forgetting to commit them. Using checkout with the -f will discard those changes. This hook is a good place to prevent this from happening, but it would be nice if there was also a hook called on the remote server prior to a pull so that you would receive these changes seamlessly.

post-receive

#!/bin/sh
git checkout -f
echo "Working directory synced."

Regarding have two branches, I thought your first solution was more elegant than having to deal with multiple repositories. If you really want to keep your production site isolated, you could use rsync locally which has similar delta patching. I would have a testing and stable branch in the repository with only the testing site as the working directory. When ready to release merge the testing into the stable branch, push, and have a hook looking for commits to stable branch make the call to rsync.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!