使用 Git push 部署项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/279169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:57:22  来源:igfitidea点击:

Deploy a project using Git push

gitdeploymentwebservergithooks

提问by Kyle Cronin

Is it possible to deploy a website using git push? I have a hunch it has something to do with using git hooksto perform a git reset --hardon the server side, but how would I go about accomplishing this?

是否可以使用 部署网站git push?我有一种预感,这与使用git hooksgit reset --hard在服务器端执行 a有关,但我将如何实现这一点?

采纳答案by Kyle Cronin

I found this scripton this siteand it seems to work quite well.

我在这个网站上找到了这个脚本,它似乎工作得很好。

  1. Copy over your .git directory to your web server
  2. On your local copy, modify your .git/config file and add your web server as a remote:

    [remote "production"]
        url = username@webserver:/path/to/htdocs/.git
    
  3. On the server, replace .git/hooks/post-update with this file(in the answer below)

  4. Add execute access to the file (again, on the server):

    chmod +x .git/hooks/post-update
    
  5. Now, just locally push to your web server and it should automatically update the working copy:

    git push production
    
  1. 将您的 .git 目录复制到您的 Web 服务器
  2. 在您的本地副本上,修改您的 .git/config 文件并将您的 Web 服务器添加为远程服务器:

    [remote "production"]
        url = username@webserver:/path/to/htdocs/.git
    
  3. 在服务器上,用这个文件替换 .git/hooks/post-update (在下面的答案中)

  4. 添加对文件的执行访问权限(再次,在服务器上):

    chmod +x .git/hooks/post-update
    
  5. 现在,只需在本地推送到您的 Web 服务器,它就会自动更新工作副本:

    git push production
    

回答by Darío Javier Cravero

Using the post-updatefile below:

使用下面的更新后文件:

  1. Copy over your .git directory to your web server
  2. On your local copy, modify your .git/config file and add your web server as a remote:

    [remote "production"]
        url = username@webserver:/path/to/htdocs/.git
    
  3. On the server, replace .git/hooks/post-update with file below

  4. Add execute access to the file (again, on the server):

    chmod +x .git/hooks/post-update
    
  5. Now, just locally push to your web server and it should automatically update the working copy:

    git push production
    
  1. 将您的 .git 目录复制到您的 Web 服务器
  2. 在您的本地副本上,修改您的 .git/config 文件并将您的 Web 服务器添加为远程服务器:

    [remote "production"]
        url = username@webserver:/path/to/htdocs/.git
    
  3. 在服务器上,用下面的文件替换 .git/hooks/post-update

  4. 添加对文件的执行访问权限(再次,在服务器上):

    chmod +x .git/hooks/post-update
    
  5. 现在,只需在本地推送到您的 Web 服务器,它就会自动更新工作副本:

    git push production
    
#!/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=
      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

回答by Earl Zedd

After many false starts and dead ends, I'm finally able to deploy website code with just "git push remote" thanks to this article.

在经历了许多错误的开始和死胡同之后,多亏了这篇文章,我终于能够只使用“git push remote”来部署网站代码。

The author's post-update script is only one line long and his solution doesn't require .htaccess configuration to hide the Git repo as some others do.

作者的更新后脚本只有一行,他的解决方案不需要像其他人那样需要 .htaccess 配置来隐藏 Git 存储库。

A couple of stumbling blocks if you're deploying this on an Amazon EC2 instance;

如果您在 Amazon EC2 实例上部署它,有几个绊脚石;

1) If you use sudo to create the bare destination repository, you have to change the owner of the repo to ec2-user or the push will fail. (Try "chown ec2-user:ec2-user repo.")

1)如果你使用sudo创建裸目标仓库,你必须将repo的所有者更改为ec2-user,否则推送将失败。(试试“chown ec2-user:ec2-user repo。”)

2) The push will fail if you don't pre-configure the location of your amazon-private-key.pem, either in /etc/ssh/ssh_config as an IdentityFile parameter or in ~/.ssh/config using the "[Host] - HostName - IdentityFile - User" layout described here...

2)如果你不预先配置推送会失败你的位置亚马逊私钥。质子交换膜,无论是在/ etc / SSH / ssh_config中作为IdentityFile参数或在〜/ .ssh / config中使用“[主机] - 主机名 - 身份文件 - 用户”布局在这里描述......

...HOWEVER if Host is configured in ~/.ssh/config and different than HostName the Git push will fail. (That's probably a Git bug)

...但是,如果 Host 在 ~/.ssh/config 中配置并且与 HostName 不同,则 Git 推送将失败。(这可能是一个 Git 错误)

回答by Christian

dont install git on a server or copy the .git folder there. to update a server from a git clone you can use following command:

不要在服务器上安装 git 或在那里复制 .git 文件夹。要从 git clone 更新服务器,您可以使用以下命令:

git ls-files -z | rsync --files-from - --copy-links -av0 . [email protected]:/var/www/project

you might have to delete files which got removed from the project.

您可能必须删除从项目中删除的文件。

this copies all the checked in files. rsync uses ssh which is installed on a server anyways.

这将复制所有签入的文件。rsync 使用无论如何都安装在服务器上的 ssh。

the less software you have installed on a server the more secure he is and the easier it is to manage it's configuration and document it. there is also no need to keep a complete git clone on the server. it only makes it more complex to secure everything properly.

你在服务器上安装的软件越少,它就越安全,管理它的配置和记录它就越容易。也无需在服务器上保留完整的 git clone。它只会使正确保护一切变得更加复杂。

回答by Lloyd Moore

In essence all you need to do are the following:

本质上,您需要做的就是以下几点:

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

I have those lines in my application as an executable called deploy.

我的应用程序中有这些行作为名为deploy.

so when I want to do a deploy I type ./deploy myserver mybranch.

所以当我想做一个部署时,我输入./deploy myserver mybranch.

回答by Greg Hewgill

The way I do it is I have a bare Git repository on my deployment server where I push changes. Then I log in to the deployment server, change to the actual web server docs directory, and do a git pull. I don't use any hooks to try to do this automatically, that seems like more trouble than it's worth.

我这样做的方式是在我的部署服务器上有一个裸 Git 存储库,我可以在其中推送更改。然后我登录到部署服务器,切换到实际的 Web 服务器 docs 目录,并执行 git pull。我不使用任何钩子来尝试自动执行此操作,这似乎比值得的麻烦更多。

回答by Karussell

Update: I'm now using Lloyd Mooresolution with the key agent ssh -A .... Pushing to a main repo and then pulling from it in parallel from all your machines is a bit faster and requires less setup on those machines.

更新:我现在将Lloyd Moore解决方案与 key agent 一起使用ssh -A ...。推送到主存储库,然后从所有机器并行提取它会更快一些,并且需要在这些机器上进行更少的设置。



Not seeing this solution here. just push via ssh if git is installed on the server.

在这里没有看到这个解决方案。如果服务器上安装了 git,只需通过 ssh 推送。

You'll need the following entry in your local .git/config

您需要在本地 .git/config 中包含以下条目

[remote "amazon"]
    url = amazon:/path/to/project.git
    fetch = +refs/heads/*:refs/remotes/amazon/*

But hey, whats that with amazon:? In your local ~/.ssh/config you'll need to add the following entry:

但是,嘿,那有什么用amazon:?在您本地的 ~/.ssh/config 中,您需要添加以下条目:

Host amazon
    Hostname <YOUR_IP>
    User <USER>
    IdentityFile ~/.ssh/amazon-private-key

now you can call

现在你可以打电话

git push amazon master
ssh <USER>@<YOUR_IP> 'cd /path/to/project && git pull'

(BTW: /path/to/project.git is different to the actual working directory /path/to/project)

(顺便说一句:/path/to/project.git 与实际工作目录 /path/to/project 不同)

回答by Attila Fulop

For Deployment Scenario

部署场景

In our scenario we're storing the code on github/bitbucket and want to deploy to live servers. In this case the following combination works for us (that is a remix of the highly upvoted answers here):

在我们的场景中,我们将代码存储在 github/bitbucket 上并希望部署到实时服务器。在这种情况下,以下组合对我们有用(这是这里高度赞成的答案的混合)

  1. Copy over your .gitdirectory to your web server
  2. On your local copy git remote add live ssh://user@host:port/folder
  3. On remote: git config receive.denyCurrentBranch ignore
  4. On remote: nano .git/hooks/post-receiveand add this content:

    #!/bin/sh GIT_WORK_TREE=/var/www/vhosts/example.org git checkout -f

  5. On remote: chmod +x .git/hooks/post-receive

  6. Now you can push there with git push live
  1. 将您的.git目录复制到您的 Web 服务器
  2. 在您的本地副本上 git remote add live ssh://user@host:port/folder
  3. 在远程: git config receive.denyCurrentBranch ignore
  4. 在远程:nano .git/hooks/post-receive并添加以下内容:

    #!/bin/sh GIT_WORK_TREE=/var/www/vhosts/example.org git checkout -f

  5. 在远程: chmod +x .git/hooks/post-receive

  6. 现在你可以用 git push live

Notes

笔记

  • This solution works with older git versions (tested with 1.7 and 1.9)
  • You need to make sure pushing to github/bitbucket first, so you'll have a consistent repo on live
  • If your .gitfolder is within document root make sure you hide it from the outside by adding to .htaccess(source):

    RedirectMatch 404 /\..*$

  • 此解决方案适用于较旧的 git 版本(使用 1.7 和 1.9 测试)
  • 你需要确保先推送到 github/bitbucket,这样你就可以在 live 上获得一致的 repo
  • 如果您的.git文件夹位于文档根目录中,请确保通过添加到.htaccess( source)将其隐藏起来:

    RedirectMatch 404 /\..*$

回答by Supernini

We use capistranofor managing deploy. We build capistrano to deploy on a staging server, and then running a rsync with all of ours server.

我们使用capistrano来管理部署。我们构建了 capistrano 以部署在临时服务器上,然后与我们所有的服务器运行 rsync。

cap deploy
cap deploy:start_rsync (when the staging is ok)

With capistrano, we can made easy rollback in case of bug

使用 capistrano,我们可以在出现错误时轻松回滚

cap deploy:rollback
cap deploy:start_rsync

回答by Artur Bodera

Giddyupare language-agnostic just-add-watergit hooks to automate deployment via git push. It also allows you to have custom start/stop hooks for restarting web server, warming up cache etc.

Giddyup是与语言无关的just-add-watergit hooks,用于通过 git push 自动部署。它还允许您使用自定义启动/停止挂钩来重新启动 Web 服务器、预热缓存等。

https://github.com/mpalmer/giddyup

https://github.com/mpalmer/giddyup

Check out examples.

查看示例