Deploy git branches

▼魔方 西西 提交于 2019-12-04 20:03:32

In git, you normally never push to repository that has checkout. At all. You can push to one except you can't push to the checked out branch, but you usually shouldn't. Mainly to keep things clearly separated and to avoid disrupting the central repository if the web server breaks and needs to be fixed up.

So you should have a bare (term used for repository without work tree) central repository. Everybody will be using that repository. It should live on your internal network and should not be in docroot of either public or test server. In fact I would strongly suggest not putting it on either server at all, but separate (possibly virtual) machine.

Now you have the web servers. They can be either working copies, or just exports created using git archive and can be updated either by cron, or post-update hook in the central repository.

Working copy is updated by git fetch central-repo-url master on site.com server and by git pull central-repo-url develop on test.site.com server (ok, the fetch + reset suggested in the other answer is probably better, because you are sure you don't have any changes there).

Export is updated by getting a zip or tar pack with git archive and extracting it. It's a bit more work.

Cron just runs the commands periodically. It's easier to set up, but the disadvantage is that there is a delay between push and the content appearing on the servers.

post-update hook is a more complicated to set up, but does not have that delay (there is obviously some delay since downloading the data takes some time too). Basically you create a script to run the update, that can be triggered either by ssh or web request on the servers. Than in the central repository you put script hooks/post-update, that will trigger the scripts on the servers. There should be no way to give them any parameters and the mechanism should not allow running any other code than those scripts for security reasons. You can make the hook more advanced by looking which branch was pushed (see git doc for where you find it) and triggering only the correct server.

Than git push origin develop will cause the test.site.com to be updated (but indirectly, via the scripts) and to make code live you'll do (on development machine; never work on server!)

git checkout master git merge develop git push origin master

and the last command will cause site.com to be updated, again indirectly via the scripts.

My suggestion is to have both docroots be git working copies.

After this you have two options

  1. Have a cron job do something like git fetch development-repo; git reset --hard development-repo/branch for both. I've done stuff like this before, and the hard reset is a must: it'll nuke any random changes that have crept into your docroot somehow.
  2. Have a post-update hook in the development repo do the above every time somebody pushes something. Unfortunately I don't have a working example of this at hand.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!