Using GIT to deploy website

别来无恙 提交于 2019-12-28 06:24:13

问题


I have followed this excellent write up http://toroid.org/ams/git-website-howto to deploy code to my server using Git's post-hooks strategy.

I have a post-update file that looks like this:

GIT_WORK_TREE=/home/rajat/webapps/<project name> git checkout -f

Everytime I push code to master branch, it gets auto deployed. What I want to do now is to make this support multiple branches, so that:

  1. git push origin master -----> deploys code to production (/home/rajat/webapps/production)
  2. git push origin staging ----> deploys code to staging (/home/rajat/webapps/staging)
  3. git push origin test ----> deploys code to test (/home/rajat/webapps/test)

For this, the post-update hook needs to understand which branch got updated. Is this possible ?


回答1:


It is possible to write a post-update hook which detect the branch name.
See for inspiration:

  • "Writing a git post-receive hook to deal with a specific branch"
  • "Find Git branch name in post-update hook"

As an example (all those hooks are based on git rev-parse):

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        # Do something
    fi
done



回答2:


What I usually do is add two bare git repos in different locations on my web server; one for test, one for production. Both repos have post-hooks to checkout to the correct directory. Then I add both as remotes on my (single) local repo.

Using this method I can push any branch to my test remote or my production remote at any time. Not sure if this is the right way but it's worked well for me.



来源:https://stackoverflow.com/questions/18804552/using-git-to-deploy-website

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