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


当前回答

更新:我现在使用Lloyd Moore解决方案与密钥代理ssh -A ....推入一个主回购,然后从所有机器上并行地从主回购中提取,这稍微快一些,在这些机器上需要的设置也更少。


这里没有解。如果服务器上安装了git,只需通过SSH推送即可。

在本地的.git/配置中需要以下条目

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

但是嘿,亚马逊是怎么回事?在你的本地~/。Ssh /config你需要添加以下条目:

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

现在你可以打电话

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

(顺便说一句:/ /项目/路径。Git与实际的工作目录/路径/到/项目不同)

其他回答

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

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

作者的更新后脚本只有一行长,并且他的解决方案不像其他一些解决方案那样需要.htaccess配置来隐藏Git回购。

如果您在Amazon EC2实例上部署它,则会遇到几个绊脚石;

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

2)如果你没有预先配置你的amazon-private-key的位置,推送会失败。可以在/etc/ssh/ssh_config中作为IdentityFile参数,也可以在~/。ssh/config使用“[主机]- HostName - IdentityFile - User”布局在这里描述…

...但是,如果在~/中配置了Host。ssh/config和不同于HostName的Git推送将失败。(这可能是Git的bug)

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

更新:我现在使用Lloyd Moore解决方案与密钥代理ssh -A ....推入一个主回购,然后从所有机器上并行地从主回购中提取,这稍微快一些,在这些机器上需要的设置也更少。


这里没有解。如果服务器上安装了git,只需通过SSH推送即可。

在本地的.git/配置中需要以下条目

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

但是嘿,亚马逊是怎么回事?在你的本地~/。Ssh /config你需要添加以下条目:

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

现在你可以打电话

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

(顺便说一句:/ /项目/路径。Git与实际的工作目录/路径/到/项目不同)

部署场景

在我们的场景中,我们将代码存储在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 /\..*$