git 网站更新策略 - 如何同步开发和实时存储库?
以下是我构建 git 支持的网站更新和备份策略的方法:
我可以通过 SSH 访问托管该网站的 Linux VPS。这是我所做的:
1)在网站服务器上 - 在正确的网站文件夹(公共根之前的一级)创建了一个 git 存储库:
cd /path/to/website
git init
git add -A
git commit -m "Website as of today."
2)在备份服务器上 - 在另一个 VPS 上创建了一个镜像存储库,用于备份目的:
git clone --mirror ssh://[email protected]/path/to/website website_backup
请注意,镜像存储库也是一个裸存储库(没有签出工作树)。
3) 设置 CRONJOBS - 网站服务器上的一个,用于吸收 wesbite 文件系统更改(更改可以通过脚本、FTP 等来完成)。它每天运行以下 bash 脚本:
#!/bin/bash
date=$(date +%d/%m/%Y)
cd /path/to/website
git add -A -v
git commit -m "Changes done at to website at ${date}"
exit 0
这样,实时网站更改将提交到存储库主分支。
在备份服务器上设置了另一个 cronjob。它每天运行以下脚本,就在上面的另一个脚本之后:
#!/bin/bash
cd /path/to/website_backup
git fetch -u ssh://[email protected]/path/to/website
exit 0
这样我就可以在备份服务器上每天更新一个“备份”,它也是一个 git 存储库,允许我在必要时及时向后移动。我不需要太担心因意外覆盖或删除而丢失内容......并且该过程是自动化的!
我每天都会收到几封来自 cronjobs 的电子邮件。它允许我检查网站中的更改,并确认两个 cronjobs 都正确运行。 (设置另一个 cronjob 来执行数据库备份。)
4)设置开发(本地存储库 + 工作树)- 我直接从网站上签出一个副本,然后创建一个名为“dev”的新本地分支:
git clone ssh://[email protected]/path/to/website website_local
git checkout -b dev
现在,我可以参与开发分支并完成我的工作。
从这一点来看,我想知道:
- 如何将我的更改推送回实时网站?
- 如何从网站取回更改并合并到我的开发分支?
简而言之:如何正确同步实时站点与开发分支而不把事情弄乱?
Here is how I have been constructing my git-powered-website update and backup strategy:
I have SSH access to the Linux VPS where the website is hosted. Here is what I did:
1) AT THE WEBSITE SERVER - Created a git repo, at the proper website folder (one level before public root):
cd /path/to/website
git init
git add -A
git commit -m "Website as of today."
2) AT THE BACKUP SERVER - Created a mirror repo, for backup purposes, at another VPS:
git clone --mirror ssh://[email protected]/path/to/website website_backup
Note that a mirror repo is also a bare repository (no checked out working tree).
3) SET UP CRONJOBS - One at the website server, to absorb wesbite file system changes (changes can be done by the scripts, by FTP, etc). It runs the following bash script, daily:
#!/bin/bash
date=$(date +%d/%m/%Y)
cd /path/to/website
git add -A -v
git commit -m "Changes done at to website at ${date}"
exit 0
This way, the live website changes are committed to the repository master branch.
Another cronjob is set up at the backup server. It runs the following script daily, right after the other one above:
#!/bin/bash
cd /path/to/website_backup
git fetch -u ssh://[email protected]/path/to/website
exit 0
This way I have at the backup server a daily updated "backup", which is also a git repo, allowing me to move backwards in time if necessary. I don't need to be much afraid of losing stuff by accidental overwrites or deletions... and the process is automated!
I receive daily a couple of e-mails from the cronjobs. It allows me to check what has been changed in the website, and to acknowledge that both cronjobs are running correctly. (Another cronjob is set up to perform the database backup.)
4) SET UP DEVELOPMENT (LOCAL REPO + WORKING TREE) - I checked out a copy directly from the website, and then created a new local branch called "dev":
git clone ssh://[email protected]/path/to/website website_local
git checkout -b dev
Now, I can play with the development branch, and do my work.
From this point, I'd like to know:
- How to push my changes back to the live website?
- How to get changes from the website back and merged to my development branch?
In short: how to properly sync live site with dev branch without messing things up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我实现的解决方案,以满足将我的开发工作推向“生产”(实时网站)的需求,并使我的本地存储库与实时网站上发生的更改保持同步......
更新网站只是一个将我的本地开发分支推送到网站存储库...
...然后将更改合并到实时网站树中。我需要 SSH 登录到网站服务器,并在网站文件夹中运行以下命令:
这会将“dev”分支的推送更改带到“master”分支(这是实时站点的当前分支)。
* 改进更新过程 *
为了自动运行合并,而不需要从服务器命令行登录并运行合并命令,我向实时网站存储库添加了一个 post-receive 挂钩。首先,我创建了挂钩文件,使其可执行,然后编辑该文件:
我的接收后挂钩文件的内容是:
请注意添加到合并命令中的 --ff-only 选项。为什么它在那里?它之所以存在,是因为作为一个自动化过程,我不想将合并冲突存储到我的实时网站文件中。因此,使用此选项,仅当我有干净的快进上下文时,我才会强制执行合并。如果这种干净的合并无法发生,那么我可以登录到服务器,并手动解决该问题,或者使用其他方法解决问题。
* 避免冲突和同步 *
为了避免服务器上的合并冲突,即确保在那里成功进行快进合并,最好使用远程的最新更改来更新本地存储库回购。换句话说:在推送我们的更改之前,使用最新的实时网站更改(远程主分支)更新本地开发分支。这可以这样完成:
更好的是:让我们首先更新本地主分支,然后将其合并到本地开发分支(听起来像变基):
这样,我们的本地主分支与远程实时网站保持同步master分支,并且100%在本地进行合并。
* 回归简单 *
我创建了一个别名来方便操作:
现在,我可以使用“deploy”别名执行实时站点更新,如下所示:
它将:
我已经使用了这个设置,并且它满足了我当前的需求,这很简单。
Here is the solution I achieved to satisfy the needs of pushing my development work to "production" (live website) and also keep my local repository up to date with the changes occurring at the live website...
To update the website is simply a matter of pushing my local development branch to the website repository...
...and then, merge the changes into the live website tree. I need to SSH log in to the website server, and run the following command at the website folder:
This will bring the pushed changes, at "dev" branch, to the "master" branch (which is the live site current branch).
* IMPROVING THE UPDATE PROCESS *
To automatically run the merge, without needing to login and run the merge command from the server command line, I added a post-receive hook to the live website repository. First, I created the hook file, made it executable, and then edited the file:
The contents of my post-receive hook file are:
Note the --ff-only option added to the merge command. Why is it there? It is there because, being an automated process, I don't want to have merge conflicts stored into my live website files. So, using this option, I enforce the merge to happen only if I have a clean fast-forward context. If this clean merge can't happen, then I may log in to the server, and manually resolve the case, or solve the problem using another approach.
* AVOIDING CONFLICTS AND SYNCHRONIZING *
To avoid merge conflicts at the server, i.e., to ensure a successfull fast-forward merge there, it is a good idea to update the local repo with the latest changes from the remote repo. In other words: update the local development branch with latest live website changes (remote master branch), prior to pushing our changes. This could be done like this:
Better yet: let's first update the local master branch, and then merge it into the local development branch (sounds like a rebase):
This way, our local master branch is kept in sync with the remote live website master branch, and the merge is performed 100% locally.
* BACK TO SIMPLICITY *
I have created an alias to facilitate things:
Now, I can perform the live site update by using the "deploy" alias, like this:
It will:
I have this setup working, and it is satisfying my current needs, which are simple.
您可能想查看 http://joemaller.com/990/a- web-focused-git-workflow/ 和 http://toroid.org/ams/git-website-howto 了解有关集成 git 和 Web 部署系统的更多信息。
请记住,git 不是一个 Web 部署系统(尽管使用一些简单的脚本,它可以为有简单需求的人提供这种方式)。
You might want to look at http://joemaller.com/990/a-web-focused-git-workflow/ and http://toroid.org/ams/git-website-howto for further information on integrating git and web deployment systems.
Remember, git is not a web deployment system (though with some simple scripts it can work that way for people with simple needs).
或者,您可以像 Github 一样使用 Git 和 Jekyll
Or, you could just use Git and Jekyll as Github does