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


当前回答

使用下面的更新后文件:

复制你的。git目录到你的web服务器 在你的本地副本上,修改你的.git/config文件,并添加你的web服务器作为远程服务器: (远程“生产”) Url = username@webserver:/path/to/htdocs/.git 在服务器上,用下面的文件替换。git/hooks/post-update 添加对文件的执行访问(同样是在服务器上): Chmod +x .git/hooks/post-update 现在,只要本地推送到你的web服务器,它就会自动更新工作副本: Git推送生产

#!/bin/sh
#
# This hook does two things:
#
#  1. update the "info" files that allow the list of references to be
#     queries over dumb transports such as http
#
#  2. if this repository looks like it is a non-bare repository, and
#     the checked-out branch is pushed to, then update the working copy.
#     This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update". 
git-update-server-info 
is_bare=$(git-config --get --bool core.bare) 
if [ -z "$is_bare" ]
then
      # for compatibility's sake, guess
      git_dir_full=$(cd $GIT_DIR; pwd)
      case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi 
update_wc() {
      ref=$1
      echo "Push to checked out branch $ref" >&2
      if [ ! -f $GIT_DIR/logs/HEAD ]
      then
             echo "E:push to non-bare repository requires a HEAD reflog" >&2
             exit 1
      fi
      if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null)
      then
             wc_dirty=0
      else
             echo "W:unstaged changes found in working copy" >&2
             wc_dirty=1
             desc="working copy"
      fi
      if git diff-index --cached HEAD@{1} >/dev/null
      then
             index_dirty=0
      else
             echo "W:uncommitted, staged changes found" >&2
             index_dirty=1
             if [ -n "$desc" ]
             then
                   desc="$desc and index"
             else
                   desc="index"
             fi
      fi
      if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
      then
             new=$(git rev-parse HEAD)
             echo "W:stashing dirty $desc - see git-stash(1)" >&2
             ( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
             git-update-ref --no-deref HEAD HEAD@{1}
             cd $GIT_WORK_TREE
             git stash save "dirty $desc before update to $new";
             git-symbolic-ref HEAD "$ref"
             )
      fi 
      # eye candy - show the WC updates :)
      echo "Updating working copy" >&2
      (cd $GIT_WORK_TREE
      git-diff-index -R --name-status HEAD >&2
      git-reset --hard HEAD)
} 
if [ "$is_bare" = "false" ]
then
      active_branch=`git-symbolic-ref HEAD`
      export GIT_DIR=$(cd $GIT_DIR; pwd)
      GIT_WORK_TREE=${GIT_WORK_TREE-..}
      for ref
      do
             if [ "$ref" = "$active_branch" ]
             then
                   update_wc $ref
             fi
      done
fi

其他回答

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文档。

听起来你应该在你的服务器上有两个副本。一个裸拷贝,你可以推/拉,当你完成时,你会推你的改变,然后你会把这个克隆到你的web目录,并设置一个cronjob来每天从你的web目录更新git pull。

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

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更改到服务器 从临时文件夹中删除部署目录。

部署场景

在我们的场景中,我们将代码存储在github/bitbucket上,并希望将其部署到活动服务器上。 在这种情况下,以下组合对我们来说是有效的(这是这里高度好评的答案的混合):

复制你的。git目录到你的web服务器 在本地复制git远程添加live ssh://user@host:port/文件夹 在远程:git配置receive.denyCurrentBranch忽略 在远程:nano .git/hooks/post-receive并添加以下内容: #!/bin/sh GIT_WORK_TREE=/var/www/vhosts/example.org 远程:chmod +x .git/hooks/post-receive 现在你可以用git push live来推送

笔记

此解决方案适用于较旧的git版本(使用1.7和1.9进行测试) 你需要确保先推送到github/bitbucket,这样你就会有一个一致的实时回购 如果你的。git文件夹在根目录中,请确保通过添加.htaccess (source)来隐藏它: RedirectMatch 404 /\..*$

我最终创建了我自己的基本部署工具,它会自动从repo - https://github.com/jesalg/SlimJim -下拉新的更新,基本上它会监听github的接收后钩子,并使用代理来触发更新脚本。