使用git将网站部署到多个环境
目前,我使用 git 将项目部署到生产环境,使用 git push production master
到具有以下接收后挂钩的存储库:
#!/bin/bash
GIT_WORK_TREE=/home/username/www.example.com/myproject/ git checkout -f
product
是远程的,通过 添加git 远程添加生产 ssh://server/home/username/projects/myproject.git
。
但现在我需要将一个单独的分支部署到服务器上的单独路径。我确实想出了一个解决方案,但我想还有更好的方法。
我所做的是在服务器上创建一个新的存储库 myproject-alternate.git
,并使用类似的 post-receive 挂钩(将 /myproject/
替换为 /myproject -alternate/
),使用 git remote add replacement ssh://server/home/username/projects/myproject-alternate.git 添加了这个新存储库。现在我可以使用 git Push Alternative Branchname:master
部署到备用路径。
这可行,但我有一些问题:
- 部署到备用服务器的命令不是我所期望的 - 我不止一次忘记了最后的
:master
并且服务器的存储库收到了一个新分支并且接收后挂钩没有被触发。 - 我不确定在服务器上创建新存储库是否是最佳解决方案,并且我想知道更大的项目会发生什么。
是否有其他方法可以完成此部署流程而不会出现上述问题?也许有一个更好的接收后挂钩,可以使用收到的分支名称部署到正确的路径? (这可能吗?)
Currently I deploy a project to production with git, using git push production master
to a repository with the following post-receive hook:
#!/bin/bash
GIT_WORK_TREE=/home/username/www.example.com/myproject/ git checkout -f
production
is a remote, added via git remote add production ssh://server/home/username/projects/myproject.git
.
But now I need to deploy a separate branch to a separate path on the server. I did come up with a solution, but I suppose there's a better way to do it.
What I did was create a new repository on the server, myproject-alternate.git
, with a similar post-receive hook (replacing /myproject/
with /myproject-alternate/
), added this new repository with git remote add alternate ssh://server/home/username/projects/myproject-alternate.git
. Now I can deploy to the alternate path with git push alternate branchname:master
.
This works, but I have some issues:
- The command to deploy to the alternate server is not what I was expecting—more than once I forgot the
:master
at the end and the server's repository received a new branch and the post-receive hook wasn't triggered. - I'm not sure if creating a new repository on the server was the best solution, and I wonder what would happen with a larger project.
Are there other ways to accomplish this deploy flow without the mentioned issues? Maybe a better post-receive hook that uses the received branchname to deploy to the right path? (is this even possible?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我写了一篇 博客文章 关于我用来将网站部署到临时服务器和实时服务器的设置。你可以做类似的事情。关键是配置您要在本地存储库的 .git/config 文件中推送哪些分支,如下所示:
这将对其进行设置,以便每当您键入时
都会自动将本地
branchname
分支推送到备用存储库中的远程master
分支。但是,由于您的备用工作树和生产工作树位于同一台计算机上,因此您可能只需创建一个存储库并将其签出到两个不同的位置即可。为此,请忽略上一段,而是在 post-receive 挂钩中添加类似的内容:(
显然您没有 使用
if
语句)。如果您这样做,我建议将存储库设为裸露,以便它不会存储自己的工作副本,只是为了简单起见。这样,您只需要一个远程,每当您推送master
分支时,它都会更新生产工作树,每当您推送branchname
分支时,它都会更新生产工作树。将更新备用工作树。免责声明:我还没有实际测试过这个,所以首先在测试文件夹中尝试一下。如果我发现任何错误,我会回来编辑。
I've written a blog post about a setup I use to deploy my website to a staging server and the live server. You could do something similar. The key is to configure which branches you're going to be pushing in the
.git/config
file of your local repository, something like this:This will set it up so that whenever you type
it will automatically push the local
branchname
branch to the remotemaster
branch in the alternate repository.However, since your alternate and production work trees are on the same computer, you can probably get away with only creating one repository and just checking it out to two different places. To do that, ignore the previous paragraph, and instead put something like this in your post-receive hook:
(obviously you don't have to use an
if
statement). If you do this, I suggest making the repository bare so that it doesn't store its own working copy, just for simplicity. This way, you only need to have one remote, and whenever you push themaster
branch, it will update the production work tree, and whenever you push thebranchname
branch, it will update the alternate work tree.Disclaimer: I haven't actually tested this, so try it out in a test folder first. If I find any errors I'll come back and edit.
我还就我使用的设置撰写了一篇博客文章,这允许我将项目中的每个分支
推送
到我的远程,将每个分支部署到各自的目录中。例如:
因此,要执行此操作,请将以下函数添加到您的服务器点文件中:
在远程上,创建一个存储库:
在本地上,推送一个分支
将您的主分支推送到远程,进入
/www/foo.com /<分支>/
。I've also written a blog post on the set up I use, which allows me to
git push
each branch in my project up to my remote, deploying each into their own respective directories.For example:
So, to do this, add the following function to your server dotfiles:
On Remote, create a repository:
On Local, push a branch
Will push your master branch up to remote, into
/www/foo.com/<BRANCH>/
.